
<html><head>
<title>htmlparse - HTML Parser</title>
<style type="text/css"><!--
    HTML {
	background: 	#FFFFFF;
	color: 		black;
    }
    BODY {
	background: 	#FFFFFF;
	color:	 	black;
    }
    DIV.doctools {
	margin-left:	10%;
	margin-right:	10%;
    }
    DIV.doctools H1,DIV.doctools H2 {
	margin-left:	-5%;
    }
    H1, H2, H3, H4 {
	margin-top: 	1em;
	font-family:	sans-serif;
	font-size:	large;
	color:		#005A9C;
	background: 	transparent;
	text-align:		left;
    }
    H1.title {
	text-align: center;
    }
    UL,OL {
	margin-right: 0em;
	margin-top: 3pt;
	margin-bottom: 3pt;
    }
    UL LI {
	list-style: disc;
    }
    OL LI {
	list-style: decimal;
    }
    DT {
	padding-top: 	1ex;
    }
    UL.toc,UL.toc UL, UL.toc UL UL {
	font:		normal 12pt/14pt sans-serif;
	list-style:	none;
    }
    LI.section, LI.subsection {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding: 	0em;
    }
    PRE {
	display: 	block;
	font-family:	monospace;
	white-space:	pre;
	margin:		0%;
	padding-top:	0.5ex;
	padding-bottom:	0.5ex;
	padding-left:	1ex;
	padding-right:	1ex;
	width:		100%;
    }
    PRE.example {
	color: 		black;
	background: 	#f5dcb3;
	border:		1px solid black;
    }
    UL.requirements LI, UL.syntax LI {
	list-style: 	none;
	margin-left: 	0em;
	text-indent:	0em;
	padding:	0em;
    }
    DIV.synopsis {
	color: 		black;
	background: 	#80ffff;
	border:		1px solid black;
	font-family:	serif;
	margin-top: 	1em;
	margin-bottom: 	1em;
    }
    UL.syntax {
	margin-top: 	1em;
	border-top:	1px solid black;
    }
    UL.requirements {
	margin-bottom: 	1em;
	border-bottom:	1px solid black;
    }
--></style>
</head>
<! -- Generated from file 'htmlparse.man' by tcllib/doctools with format 'html'
   -->
<! -- htmlparse.n
   -->
<body><div class="doctools">
<hr> [
 <div class='fossil-doc' data-title='htmlparse - HTML Parser'>
<br>
  <a href="../../../../../../../../home">Tcllib Home</a>
| <a href="../../../../toc.html">Main Table Of Contents</a>
| <a href="../../../toc.html">Table Of Contents</a>
| <a href="../../../../index.html">Keyword Index</a>
| <a href="../../../../toc0.html">Categories</a>
| <a href="../../../../toc1.html">Modules</a>
| <a href="../../../../toc2.html">Applications</a>
 ] <hr>
<h1 class="title">htmlparse(n) 1.2.1 tcllib &quot;HTML Parser&quot;</h1>
<div id="name" class="section"><h2><a name="name">Name</a></h2>
<p>htmlparse - Procedures to parse HTML strings</p>
</div>
<div id="toc" class="section"><h2><a name="toc">Table Of Contents</a></h2>
<ul class="toc">
<li class="section"><a href="#toc">Table Of Contents</a></li>
<li class="section"><a href="#synopsis">Synopsis</a></li>
<li class="section"><a href="#section1">Description</a></li>
<li class="section"><a href="#section2">Bugs, Ideas, Feedback</a></li>
<li class="section"><a href="#see-also">See Also</a></li>
<li class="section"><a href="#keywords">Keywords</a></li>
<li class="section"><a href="#category">Category</a></li>
</ul>
</div>
<div id="synopsis" class="section"><h2><a name="synopsis">Synopsis</a></h2>
<div class="synopsis">
<ul class="requirements">
<li>package require <b class="pkgname">Tcl 8.2</b></li>
<li>package require <b class="pkgname">struct::stack 1.3</b></li>
<li>package require <b class="pkgname">cmdline 1.1</b></li>
<li>package require <b class="pkgname">htmlparse <span class="opt">?1.2.1?</span></b></li>
</ul>
<ul class="syntax">
<li><a href="#1"><b class="cmd">::htmlparse::parse</b> <span class="opt">?-cmd <i class="arg">cmd</i>?</span> <span class="opt">?-vroot <i class="arg">tag</i>?</span> <span class="opt">?-split <i class="arg">n</i>?</span> <span class="opt">?-incvar <i class="arg">var</i>?</span> <span class="opt">?-queue <i class="arg">q</i>?</span> <i class="arg">html</i></a></li>
<li><a href="#2"><b class="cmd">::htmlparse::debugCallback</b> <span class="opt">?<i class="arg">clientdata</i>?</span> <i class="arg">tag slash param textBehindTheTag</i></a></li>
<li><a href="#3"><b class="cmd">::htmlparse::mapEscapes</b> <i class="arg">html</i></a></li>
<li><a href="#4"><b class="cmd">::htmlparse::2tree</b> <i class="arg">html tree</i></a></li>
<li><a href="#5"><b class="cmd">::htmlparse::removeVisualFluff</b> <i class="arg">tree</i></a></li>
<li><a href="#6"><b class="cmd">::htmlparse::removeFormDefs</b> <i class="arg">tree</i></a></li>
</ul>
</div>
</div>
<div id="section1" class="section"><h2><a name="section1">Description</a></h2>
<p>The <b class="package">htmlparse</b> package provides commands that allow libraries
and applications to parse HTML in a string into a representation of
their choice.</p>
<p>The following commands are available:</p>
<dl class="definitions">
<dt><a name="1"><b class="cmd">::htmlparse::parse</b> <span class="opt">?-cmd <i class="arg">cmd</i>?</span> <span class="opt">?-vroot <i class="arg">tag</i>?</span> <span class="opt">?-split <i class="arg">n</i>?</span> <span class="opt">?-incvar <i class="arg">var</i>?</span> <span class="opt">?-queue <i class="arg">q</i>?</span> <i class="arg">html</i></a></dt>
<dd><p>This command is the basic parser for HTML. It takes an HTML string,
parses it and invokes a command prefix for every tag encountered. It
is not necessary for the HTML to be valid for this parser to
function. It is the responsibility of the command invoked for every
tag to check this. Another responsibility of the invoked command is
the handling of tag attributes and character entities (escaped
characters). The parser provides the un-interpreted tag attributes to
the invoked command to aid in the former, and the package at large
provides a helper command, <b class="cmd">::htmlparse::mapEscapes</b>, to aid in
the handling of the latter. The parser <em>does</em> ignore leading
DOCTYPE declarations and all valid HTML comments it encounters.</p>
<p>All information beyond the HTML string itself is specified via
options, these are explained below.</p>
<p>To help understand the options, some more background information about
the parser.</p>
<p>It is capable of detecting incomplete tags in the HTML string given to
it. Under normal circumstances this will cause the parser to throw an
error, but if the option <i class="arg">-incvar</i> is used to specify a global (or
namespace) variable, the parser will store the incomplete part of the
input into this variable instead. This will aid greatly in the
handling of incrementally arriving HTML, as the parser will handle
whatever it can and defer the handling of the incomplete part until
more data has arrived.</p>
<p>Another feature of the parser are its two possible modes of
operation. The normal mode is activated if the option <i class="arg">-queue</i> is
not present on the command line invoking the parser. If it is present,
the parser will go into the incremental mode instead.</p>
<p>The main difference is that a parser in normal mode will immediately
invoke the command prefix for each tag it encounters. In incremental
mode however the parser will generate a number of scripts which invoke
the command prefix for groups of tags in the HTML string and then
store these scripts in the specified queue. It is then the
responsibility of the caller of the parser to ensure the execution of
the scripts in the queue.</p>
<p><em>Note</em>: The queue object given to the parser has to provide the
same interface as the queue defined in tcllib -&gt; struct. This means,
for example, that all queues created via that tcllib module can be
immediately used here. Still, the queue doesn't have to come from
tcllib -&gt; struct as long as the same interface is provided.</p>
<p>In both modes the parser will return an empty string to the caller.</p>
<p>The <i class="arg">-split</i> option may be given to a parser in incremental mode to
specify the size of the groups it creates. In other words, -split 5
means that each of the generated scripts will invoke the command
prefix for 5 consecutive tags in the HTML string. A parser in normal
mode will ignore this option and its value.</p>
<p>The option <i class="arg">-vroot</i> specifies a virtual root tag. A parser in
normal mode will invoke the command prefix for it immediately before
and after it processes the tags in the HTML, thus simulating that the
HTML string is enclosed in a &lt;vroot&gt; &lt;/vroot&gt; combination. In
incremental mode however the parser is unable to provide the closing
virtual root as it never knows when the input is complete. In this
case the first script generated by each invocation of the parser will
contain an invocation of the command prefix for the virtual root as
its first command.
The following options are available:</p>
<dl class="definitions">
<dt><b class="option">-cmd</b> <i class="arg">cmd</i></dt>
<dd><p>The command prefix to invoke for every tag in the HTML
string. Defaults to <i class="arg">::htmlparse::debugCallback</i>.</p></dd>
<dt><b class="option">-vroot</b> <i class="arg">tag</i></dt>
<dd><p>The virtual root tag to add around the HTML in normal mode. In
incremental mode it is the first tag in each chunk processed by the
parser, but there will be no closing tags. Defaults to
<i class="arg">hmstart</i>.</p></dd>
<dt><b class="option">-split</b> <i class="arg">n</i></dt>
<dd><p>The size of the groups produced by an incremental mode parser. Ignored
when in normal mode. Defaults to 10. Values &lt;= 0 are not allowed.</p></dd>
<dt><b class="option">-incvar</b> <i class="arg">var</i></dt>
<dd><p>The name of the variable where to store any incomplete HTML into. This
makes most sense for the incremental mode. The parser will throw an
error if it sees incomplete HTML and has no place to store it to. This
makes sense for the normal mode. Only incomplete tags are detected,
not missing tags.  Optional, defaults to 'no variable'.</p></dd>
</dl>
<dl class="definitions">
<dt><em>Interface to the command prefix</em></dt>
<dd><p>In normal mode the parser will invoke the command prefix with four
arguments appended. See <b class="cmd">::htmlparse::debugCallback</b> for a
description.</p>
<p>In incremental mode, however, the generated scripts will invoke the
command prefix with five arguments appended. The last four of these
are the same which were mentioned above. The first is a placeholder
string (<b class="const">@win@</b>) for a clientdata value to be supplied later
during the actual execution of the generated scripts. This could be a
tk window path, for example. This allows the user of this package to
preprocess HTML strings without committing them to a specific window,
object, whatever during parsing. This connection can be made
later. This also means that it is possible to cache preprocessed
HTML. Of course, nothing prevents the user of the parser from
replacing the placeholder with an empty string.</p></dd>
</dl></dd>
<dt><a name="2"><b class="cmd">::htmlparse::debugCallback</b> <span class="opt">?<i class="arg">clientdata</i>?</span> <i class="arg">tag slash param textBehindTheTag</i></a></dt>
<dd><p>This command is the standard callback used by the parser in
<b class="cmd">::htmlparse::parse</b> if none was specified by the user. It simply
dumps its arguments to stdout.  This callback can be used for both
normal and incremental mode of the calling parser. In other words, it
accepts four or five arguments. The last four arguments are described
below. The optional fifth argument contains the clientdata value
passed to the callback by a parser in incremental mode. All callbacks
have to follow the signature of this command in the last four
arguments, and callbacks used in incremental parsing have to follow
this signature in the last five arguments.</p>
<p>The first argument, <i class="arg">clientdata</i>, is optional and present only if
this command is invoked by a parser in incremental mode. It contains
whatever the user of this package wishes.</p>
<p>The second argument, <i class="arg">tag</i>, contains the name of the tag which is
currently processed by the parser.</p>
<p>The third argument, <i class="arg">slash</i>, is either empty or contains a slash
character. It allows the callback to distinguish between opening
(slash is empty) and closing tags (slash contains a slash character).</p>
<p>The fourth argument, <i class="arg">param</i>, contains the un-interpreted list of
parameters to the tag.</p>
<p>The fifth and last argument, <i class="arg">textBehindTheTag</i>, contains the text
found by the parser behind the tag named in <i class="arg">tag</i>.</p></dd>
<dt><a name="3"><b class="cmd">::htmlparse::mapEscapes</b> <i class="arg">html</i></a></dt>
<dd><p>This command takes a HTML string, substitutes all escape sequences
with their actual characters and then returns the resulting string.
HTML strings which do not contain escape sequences are returned
unchanged.</p></dd>
<dt><a name="4"><b class="cmd">::htmlparse::2tree</b> <i class="arg">html tree</i></a></dt>
<dd><p>This command is a wrapper around <b class="cmd">::htmlparse::parse</b> which takes
an HTML string (in <i class="arg">html</i>) and converts it into a tree containing
the logical structure of the parsed document. The name of the tree is
given to the command as its second argument (<i class="arg">tree</i>). The command
does <b class="cmd">not</b> generate the tree by itself but expects that the caller
provided it with an existing and empty tree. It also expects that the
specified tree object follows the same interface as the tree object in
tcllib -&gt; struct. It doesn't have to be from tcllib -&gt; struct, but it
must provide the same interface.</p>
<p>The internal callback does some basic checking of HTML validity and
tries to recover from the most basic errors. The command returns the
contents of its second argument. Side effects are the creation and
manipulation of a tree object.</p>
<p>Each node in the generated tree represent one tag in the input. The
name of the tag is stored in the attribute <em>type</em> of the
node. Any html attributes coming with the tag are stored unmodified in
the attribute <em>data</em> of the tag. In other words, the command does
<em>not</em> parse html attributes into their names and values.</p>
<p>If a tag contains text its node will have children of type
<em>PCDATA</em> containing this text. The text will be stored in the
attribute <em>data</em> of these children.</p></dd>
<dt><a name="5"><b class="cmd">::htmlparse::removeVisualFluff</b> <i class="arg">tree</i></a></dt>
<dd><p>This command walks a tree as generated by <b class="cmd">::htmlparse::2tree</b> and
removes all the nodes which represent visual tags and not structural
ones. The purpose of the command is to make the tree easier to
navigate without getting bogged down in visual information not
relevant to the search. Its only argument is the name of the tree to
cut down.</p></dd>
<dt><a name="6"><b class="cmd">::htmlparse::removeFormDefs</b> <i class="arg">tree</i></a></dt>
<dd><p>Like <b class="cmd">::htmlparse::removeVisualFluff</b> this command is here to cut
down on the size of the tree as generated by
<b class="cmd">::htmlparse::2tree</b>. It removes all nodes representing forms and
form elements. Its only argument is the name of the tree to cut down.</p></dd>
</dl>
</div>
<div id="section2" class="section"><h2><a name="section2">Bugs, Ideas, Feedback</a></h2>
<p>This document, and the package it describes, will undoubtedly contain
bugs and other problems.
Please report such in the category <em>htmlparse</em> of the
<a href="http://core.tcl.tk/tcllib/reportlist">Tcllib Trackers</a>.
Please also report any ideas for enhancements you may have for either
package and/or documentation.</p>
</div>
<div id="see-also" class="section"><h2><a name="see-also">See Also</a></h2>
<p><a href="../struct/struct_tree.html">struct::tree</a></p>
</div>
<div id="keywords" class="section"><h2><a name="keywords">Keywords</a></h2>
<p><a href="../../../../index.html#key616">html</a>, <a href="../../../../index.html#key26">parsing</a>, <a href="../../../../index.html#key50">queue</a>, <a href="../../../../index.html#key300">tree</a></p>
</div>
<div id="category" class="section"><h2><a name="category">Category</a></h2>
<p>Text processing</p>
</div>
</div></body></html>
