# nokogumbo1 **Repository Path**: hkk2016/nokogumbo ## Basic Information - **Project Name**: nokogumbo1 - **Description**: :smile: :laughing: :blush: :smiley: - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-10-10 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Nokogumbo - a Nokogiri interface to the Gumbo HTML5 parser. Nokogumbo provides the ability for a Ruby program to invoke the [Gumbo HTML5 parser](https://github.com/google/gumbo-parser#readme) and to access the result as a [Nokogiri::HTML::Document](http://rdoc.info/github/sparklemotion/nokogiri/Nokogiri/HTML/Document). [](https://travis-ci.org/rubys/nokogumbo) [](https://ci.appveyor.com/project/rubys/nokogumbo/branch/master) ## Usage ```ruby require 'nokogumbo' doc = Nokogiri.HTML5(string) ``` To parse an HTML fragment, a `fragment` method is provided. ```ruby require 'nokogumbo' doc = Nokogiri::HTML5.fragment(string) ``` Because HTML is often fetched via the web, a convenience interface to HTTP get is also provided: ```ruby require 'nokogumbo' doc = Nokogiri::HTML5.get(uri) ``` ## Parsing options The document and fragment parsing methods, - `Nokogiri.HTML5(html, url = nil, encoding = nil, options = {})` - `Nokogiri::HTML5.parse(html, url = nil, encoding = nil, options = {})` - `Nokogiri::HTML5::Document.parse(html, url = nil, encoding = nil, options = {})` - `Nokogiri::HTML5.fragment(html, encoding = nil, options = {})` - `Nokogiri::HTML5::DocumentFragment.parse(html, encoding = nil, options = {})` support options that are different from Nokogiri's. The two currently supported options are `:max_errors` and `:max_tree_depth`, described below. ### Error reporting Nokogumbo contains an experimental parse error reporting facility. By default, no parse errors are reported but this can be configured by passing the `:max_errors` option to `::parse` or `::fragment`. ```ruby require 'nokogumbo' doc = Nokogiri::HTML5.parse('Hi there!', max_errors: 10) doc.errors.each do |err| puts(err) end ``` This prints the following. ``` 1:1: ERROR: Expected a doctype token Hi there! ^ 1:1: ERROR: Start tag of nonvoid HTML element ends with '/>', use '>'. Hi there! ^ 1:17: ERROR: End tag ends with '/>', use '>'. Hi there! ^ 1:17: ERROR: End tag contains attributes. Hi there! ^ ``` Using `max_errors: -1` results in an unlimited number of errors being returned. The errors returned by `#errors` are instances of [`Nokogiri::XML::SyntaxError`](https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/SyntaxError). The [HTML standard](https://html.spec.whatwg.org/multipage/parsing.html#parse-errors) defines a number of standard parse error codes. These error codes only cover the "tokenization" stage of parsing HTML. The parse errors in the "tree construction" stage do not have standardized error codes (yet). As a convenience to Nokogumbo users, the defined error codes are available via the [`Nokogiri::XML::SyntaxError#str1`](https://www.rubydoc.info/github/sparklemotion/nokogiri/Nokogiri/XML/SyntaxError#str1-instance_method) method. ```ruby require 'nokogumbo' doc = Nokogiri::HTML5.parse('Hi there!', max_errors: 10) doc.errors.each do |err| puts("#{err.line}:#{err.column}: #{err.str1}") end ``` This prints the following. ``` 1:1: generic-parser 1:1: non-void-html-element-start-tag-with-trailing-solidus 1:17: end-tag-with-trailing-solidus 1:17: end-tag-with-attributes ``` Note that the first error is `generic-parser` because it's an error from the tree construction stage and doesn't have a standardized error code. For the purposes of semantic versioning, the error messages, error locations, and error codes are not part of Nokogumbo's public API. That is, these are subject to change without Nokogumbo's major version number changing. These may be stabilized in the future. ### Maximum tree depth The maximum depth of the DOM tree parsed by the various parsing methods is configurable by the `:max_tree_depth` option. If the depth of the tree would exceed this limit, then an [ArgumentError](https://ruby-doc.org/core-2.5.0/ArgumentError.html) is thrown. This limit (which defaults to `Nokogumbo::DEFAULT_MAX_TREE_DEPTH = 400`) can be removed by giving the option `max_tree_depth: -1`. ``` ruby html = '' + '
ContentEOF puts doc.at('/html/body/pre').serialize # Prints:
Content``` In this case, the original HTML is semantically equivalent to the serialized version. If the `pre`, `listing`, or `textarea` content starts with two newlines, the first newline will be stripped on the first parse and the second newline will be stripped on the second, leading to semantically different DOMs. Passing the parameter `preserve_newline: true` will cause two or more newlines to be preserved. (A single leading newline will still be removed.) ``` ruby doc = Nokogiri::HTML5(<<-EOF)
gumbo_parse_with_options method, using the default options.
The resulting Gumbo parse tree is then walked.
* If the necessary Nokogiri and [libxml2](http://xmlsoft.org/html/) headers
can be found at installation time then an
[xmlDoc](http://xmlsoft.org/html/libxml-tree.html#xmlDoc) tree is produced
and a single Nokogiri Ruby object is constructed to wrap the xmlDoc
structure. Nokogiri only produces Ruby objects as necessary, so all
searching is done using the underlying libxml2 libraries.
* If the necessary headers are not present at installation time, then
Nokogiri Ruby objects are created for each Gumbo node. Other than
memory usage and CPU time, the results should be equivalent.
* The `Nokogiri::HTML5.get` function takes care of following redirects,
https, and determining the character encoding of the result, based on the
rules defined in the HTML5 specification for doing so.
* Instead of uppercase element names, lowercase element names are produced.
* Instead of returning `unknown` as the element name for unknown tags, the
original tag name is returned verbatim.
# Flavors of Nokogumbo
Nokogumbo uses libxml2, the XML library underlying Nokogiri, to speed up
parsing. If the libxml2 headers are not available, then Nokogumbo resorts to
using Nokogiri's Ruby API to construct the DOM tree.
Nokogiri can be configured to either use the system library version of libxml2
or use a bundled version. By default (as of Nokogiri version 1.8.4), Nokogiri
will use a bundled version.
To prevent differences between versions of libxml2, Nokogumbo will only use
libxml2 if the build process can find the exact same version used by Nokogiri.
This leads to three possibilities
1. Nokogiri is compiled with the bundled libxml2. In this case, Nokogumbo will
(by default) use the same version of libxml2.
2. Nokogiri is compiled with the system libxml2. In this case, if the libxml2
headers are available, then Nokogumbo will (by default) use the system
version and headers.
3. Nokogiri is compiled with the system libxml2 but its headers aren't
available at build time for Nokogumbo. In this case, Nokogumbo will use the
slower Ruby API.
Using libxml2 can be required by passing `-- --with-libxml2` to `bundle exec
rake` or to `gem install`. Using libxml2 can be prohibited by instead passing
`-- --without-libxml2`.
Functionally, the only difference between using libxml2 or not is in the
behavior of `Nokogiri::XML::Node#line`. If it is used, then `#line` will
return the line number of the corresponding node. Otherwise, it will return 0.
# Installation
git clone https://github.com/rubys/nokogumbo.git
cd nokogumbo
bundle install
rake gem
gem install pkg/nokogumbo*.gem
# Related efforts
* [ruby-gumbo](https://github.com/nevir/ruby-gumbo#readme) -- a ruby binding
for the Gumbo HTML5 parser.
* [lua-gumbo](https://gitlab.com/craigbarnes/lua-gumbo) -- a lua binding for
the Gumbo HTML5 parser.