class String
  # Wrapper for HTMLEntities gem which falls back on CGI.unescapeHTML
  def unescape
    begin
      require 'htmlentities'
      @@coder ||= HTMLEntities.new
      @@coder.decode(self)
    rescue LoadError
      logger.warn "HTMLEntities gem not found.  Using (substantially inferior) CGI.unescapeHTML"
      CGI.unescapeHTML(self)
    end
  end

  # Wrapper for HTMLEntities gem which falls back on CGI.escapeHTML
  def escape
    begin
      require 'htmlentities'
      @@coder ||= HTMLEntities.new
      @@coder.encode(self, :basic, :decimal)
    rescue
      logger.warn "HTMLEntities gem not found.  Using (substantially inferior) CGI.escapeHTML"
      CGI.escapeHTML(self)
    end
  end
end