View on
MetaCPAN
David Helkowski > HTML-Bare-0.02 > HTML::Bare

Download:
HTML-Bare-0.02.tar.gz

Dependencies

Annotate this POD

View/Report Bugs
Module Version: 0.02   Source  

NAME ^

HTML::Bare - Minimal HTML parser implemented via a C state engine

VERSION ^

0.02

SYNOPSIS ^

  use HTML::Bare;
  
  my $ob = new HTML::Bare( text => '<html><name>Bob</name></html>' );
  
  # Parse the html into a hash tree
  my $root = $ob->parse();
  
  # Print the content of the name node
  print $root->{html}->{name}->{value};
  
  ---
  
  # Load html from a file ( assume same contents as first example )
  my $ob2 = new HTML::Bare( file => 'test.html' );
  
  my $root2 = $ob2->parse();
  
  $root2->{html}->{name}->{value} = 'Tim';
  
  # Save the changes back to the file
  $ob2->save();
  
  ---
  
  # Load html and verify against XBS ( HTML Bare Schema )
  my $html_text = '<html><item name=bob/></html>''
  my $schema_text = '<html><item* name=[a-z]+></item*></html>'
  my $ob = new HTML::Bare( text => $html_text, schema => { text => $schema_text } );
  $ob->parse(); # this will error out if schema is invalid

DESCRIPTION ^

This module is a 'Bare' HTML parser. It is implemented in C. The parser itself is a simple state engine that is less than 500 lines of C. The parser builds a C struct tree from input text. That C struct tree is converted to a Perl hash by a Perl function that makes basic calls back to the C to go through the nodes sequentially.

The parser itself will only cease parsing if it encounters tags that are not closed properly. All other inputs will parse, even invalid inputs. To allowing checking for validity, a schema checker is included in the module as well.

The schema format is custom and is meant to be as simple as possible. It is based loosely around the way multiplicity is handled in Perl regular expressions.

Supported HTML

To demonstrate what sort of HTML is supported, consider the following examples. Each of the PERL statements evaluates to true.

Schema Checking

Schema checking is done by providing the module with an XBS (HTML::Bare Schema) to check the HTML against. If the HTML checks as valid against the schema, parsing will continue as normal. If the HTML is invalid, the parse function will die, providing information about the failure.

The following information is provided in the error message:

XBS ( HTML::Bare Schema ) Format

Parsed Hash Structure

The hash structure returned from HTML parsing is created in a specific format. Besides as described above, the structure contains some additional nodes in order to preserve information that will allow that structure to be correctly converted back to HTML.

Nodes may contain the following 3 additional subnodes:

Parsing Limitations / Features

Module Functions

Functions not yet documented

Functions Used Internally

Controversy

Since the creation of this module there has been a fair amount of controvesy surrounding it. A number of authors of other HTML parsers have gone so far as to publicly attack this module and claim that it 'does not parse HTML', and 'it is not HTML compliant'. Some of the same people seem to be angered by the inclusion of a benchmark, claiming that it is an unfair comparison, and that if the proper options and setup are used, that other HTML parsers are better.

The module should parse any HTML document that conforms to the standardized HTML specifications, there is no need for alarm and fear that the module will corrupt your HTML documents on reading.

To be blunt about how the parser works, very little has been done to make the parser follow the specification known as 'HTML'. The parser is meant to be flexibile and somewhat resilient, and will parse HTML like garbage that would cause other parsers to error out.

As far as I am concerned, as the author of the module, the 'HTML' in 'HTML::Bare' should be thought of to mean 'eXtremely Mad Language', because the module was written from scratch without referring to the specification known as 'HTML'.

In regard to the complaints about the unfairness of the included benchmarks, please make your own intelligent decision as to what module you like by trying multiple modules and/or running the performance tests yourself. If you like some other module, use that module. If you like HTML::Bare and think it is the fastest thing on the planet, that is cool too.

If you hate HTML::Bare and want to go around on the internet trashing it and telling people to use something else, I think perhaps you may want to seek counseling.

Performance

In comparison to other available perl html parsers that create trees, HTML::Bare is extremely fast. In order to measure the performance of loading and parsing compared to the alternatives, a templated speed comparison mechanism has been created and included with HTML::Bare.

The include makebench.pl file runs when you make the module and creates perl files within the bench directory corresponding to the .tmpl contained there.

Currently there are three types of modules that can be tested against, executable parsers ( exe.tmpl ), tree parsers ( tree.tmpl ), and parsers that do not generated trees ( notree.tmpl ).

A full list of modules currently tested against is as follows:

  EzHTML (exe)
  Tiny HTML (exe)
  HTML::Descent (notree)
  HTML::DOM
  HTML::Fast
  HTML::Grove::Builder
  HTML::Handler::Trees
  HTMLIO (exe)
  HTML::LibHTML (notree)
  HTML::LibHTML::Simple
  HTML::Parser (notree)
  HTML::Parser::EasyTree
  HTML::Parser::Expat (notree)
  HTML::SAX::Simple
  HTML::Simple using HTML::Parser
  HTML::Simple using HTML::SAX::PurePerl
  HTML::Simple using HTML::LibHTML::SAX::Parser
  HTML::Simple using HTML::Bare::SAX::Parser
  HTML::Smart
  HTML::Twig
  HTML::TreePP
  HTML::Trivial
  HTML::XPath::HTMLParser

To run the comparisons, run the appropriate perl file within the bench directory. ( exe.pl, tree.pl, or notree.pl )

The script measures the milliseconds of loading and parsing, and compares the time against the time of HTML::Bare. So a 7 means it takes 7 times as long as HTML::Bare.

Here is a combined table of the script run against each alternative using the included test.html:

  -Module-                   load     parse    total
  HTML::Bare                  1        1        1
  HTML::TreePP                2.3063   33.1776  6.1598
  HTML::Parser::EasyTree      4.9405   25.7278  7.4571
  HTML::Handler::Trees        7.2303   26.5688  9.6447
  HTML::Trivial               5.0636   12.4715  7.3046
  HTML::Smart                 6.8138   78.7939  15.8296
  HTML::Simple (HTML::Parser)  2.3346   50.4772  10.7455
  HTML::Simple (PurePerl)     2.361    261.4571 33.6524
  HTML::Simple (LibHTML)       2.3187   163.7501 23.1816
  HTML::Simple (HTML::Bare)    2.3252   59.1254  10.9163
  HTML::SAX::Simple           8.7792   170.7313 28.3634
  HTML::Twig                  27.8266  56.4476  31.3594
  HTML::Grove::Builder        7.1267   26.1672  9.4064
  HTML::XPath::HTMLParser      9.7783   35.5486  13.0002
  HTML::LibHTML (notree)       11.0038  4.5758   10.6881
  HTML::Parser (notree)       4.4698   17.6448  5.8609
  HTML::Parser::Expat(notree) 3.7681   50.0382  6.0069
  HTML::Descent (notree)      6.0525   37.0265  11.0322
  Tiny HTML (exe)                               1.0095
  EzHTML (exe)                                  1.1284
  HTMLIO (exe)                                  1.0165

Here is a combined table of the script run against each alternative using the included feed2.html:

  -Module-                   load     parse    total
  HTML::Bare                  1        1        1
  HTML::Bare (simple)         1        0.7238   ?
  HTML::Bare (unsafe simple)  1       ~0.5538   ?
  HTML::Fast                  1.516    0.9733   1.4783
  HTML::TreePP                0.6393   30.5951  2.6874
  HTML::MyHTML                 1.8266   14.2571  2.7113 
  HTML::Parser::EasyTree      1.5208   22.8283  2.9748 
  HTML::Trivial               2.007    25.742   3.615  
  HTML::Tiny                  0.1665   61.4918  4.3234  
  HTML::XPath::HTMLParser      2.5762   33.2567  4.6742  
  HTML::Smart                 1.702    59.4907  5.7566
  HTML::Simple (HTML::Parser)  0.5838   64.7243  5.0006  
  HTML::DOM::Lite             4.5207   17.4617  5.4033
  HTML::Simple (LibHTML)       0.5904   161.7544 11.5731
  HTML::Twig                  8.553    56.9034  11.8805 
  HTML::Grove::Builder        7.2021   30.7926  12.9334
  HTML::Handler::Trees        6.8545   33.1007  13.0575
  HTML::LibHTML::Simple        14.0204  11.8482  13.8707
  HTML::Simple (PurePerl)     0.6176   321.3422 23.0465 
  HTML::Simple                2.7168   90.7203  26.7525
  HTML::SAX::Simple           8.7386   94.8276  29.2166
  HTML::LibHTML (notree)       11.0023  5.022    10.5214
  HTML::Parser (notree)       4.3748   25.0213  5.9803
  HTML::Parser::Expat(notree) 3.6555   51.6426  7.4316
  HTML::Descent (notree)      5.9206   155.0289 18.7767
  Tiny HTML (exe)                               1.2212
  EzHTML (exe)                                  1.3618
  HTMLIO (exe)                                  1.0145

These results show that HTML::Bare is, at least on the test machine, running all tests within cygwin, faster at loading and parsing than everything being tested against.

The following things are shown as well: - HTML::Bare can parse HTML and create a hash tree in less time than it takes LibHTML just to parse. - HTML::Bare can parse HTML and create a tree in less time than all three binary parsers take just to parse. - HTML::Fast is theoretically faster at parsing than the default 'full' mode of HTML::Bare. Despite that, the 'simple' mode of HTML::Bare is even faster.

Note that the executable parsers are not perl modules and are timed using dummy programs that just uses the library to load and parse the example files. The executables are not included with this program. Any source modifications used to generate the shown test results can be found in the bench/src directory of the distribution

LICENSE ^

  Copyright (C) 2008 David Helkowski
  
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation; either version 2 of the
  License, or (at your option) any later version.  You may also can
  redistribute it and/or modify it under the terms of the Perl
  Artistic License.
  
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
syntax highlighting: