def parse_page(content, encoding = nil, options = nil, parser = :tidy)
      begin
        # Get the document encoding from the meta header.
        if meta = content.match(/(<meta\s*([^>]*)http-equiv=['"]?content-type['"]?([^>]*))/i)
          if meta = meta[0].match(/charset=([\w-]*)/i)
            encoding = meta[1]
          end
        end
        encoding ||= "utf8"
        case (parser || :tidy)
        when :tidy
          # Make sure the Tidy path is set and always apply the default
          # options (these only control things like errors, output type).
          find_tidy
          options = (options || {}).update(TIDY_OPTIONS)
          options[:input_encoding] = encoding.gsub("-", "").downcase
          html = TidyFFI::Tidy.with_options(options).clean(content)
          document = HTML::Document.new(html).find(:tag=>"html")
        when :html_parser
          document = HTML::HTMLParser.parse(content).root
        else
          raise HTMLParseError, "No parser #{parser || "unspecified"}"
        end
        return Parsed[document, encoding]
      rescue Exception=>error
        raise HTMLParseError.new(error)
      end
    end
def parse_page(content, encoding = nil, options = nil, parser = :tidy)
      begin
        # Get the document encoding from the meta header.
        if meta = content.match(/(<meta\s*([^>]*)http-equiv=['"]?content-type['"]?([^>]*))/i)
          if meta = meta[0].match(/charset=([\w-]*)/i)
            encoding = meta[1]
          end
        end
        encoding ||= "utf8"
        case (parser || :tidy)
        when :tidy
          # Make sure the Tidy path is set and always apply the default
          # options (these only control things like errors, output type).
          find_tidy
          options = (options || {}).update(TIDY_OPTIONS)
          options[:input_encoding] = encoding.gsub("-", "").downcase
          html = TidyFFI::Tidy.with_options(options).clean(content)
          document = HTML::Document.new(html).find(:tag=>"html")
        when :html_parser
          document = HTML::HTMLParser.parse(content).root
        else
          raise HTMLParseError, "No parser #{parser || "unspecified"}"
        end
        return Parsed[document, encoding]
      rescue Exception=>error
        raise HTMLParseError.new(error)
      end
    end
:call-seq:
   parse_page(html, encoding?, options?, parser) => html

 Parses an HTML page and returns the encoding and HTML element.
 Raises HTMLParseError exceptions if it cannot parse the HTML.

 Options are passed to the parser. For example, when using Tidy
 you can pass Tidy cleanup options in the hash.

 The last option specifies which parser to use (see PARSERS).
 By default Tidy is used.