HTML-Seamstress
view release on metacpan or search on metacpan
lib/HTML/Seamstress.html view on Meta::CPAN
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML::Seamstress - HTML::Tree subclass for HTML templating via tree rewriting</title>
<link rev="made" href="mailto:root@localhost" />
</head>
<body style="background-color: white">
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<ul>
<li><a href="#first_generation_dynamic_html_production">First generation dynamic HTML production</a></li>
<li><a href="#second_generation_dynamic_html_production">Second generation dynamic HTML production</a></li>
<li><a href="#third_generation_dynamic_html_production">Third generation dynamic HTML production</a></li>
<li><a href="#talkin__bout_them_generations___">Talkin' bout them generations...</a></li>
<li><a href="#enter_fourth_generation_dynamic_html_production">Enter fourth generation dynamic HTML production</a></li>
</ul>
<li><a href="#philosophy_and_motivation_of_html__seamstress">PHILOSOPHY and MOTIVATION of HTML::Seamstress</a></li>
<ul>
<li><a href="#reap_4th_generation_dynamic_html_generation_benefits">Reap 4th generation dynamic HTML generation benefits</a></li>
<ul>
<li><a href="#separate_html_development_and_its_programmatic_modification">Separate HTML development and its programmatic modification</a></li>
<li><a href="#reduced_learning_curve">Reduced learning curve</a></li>
<li><a href="#static_validation_and_formatting">Static validation and formatting</a></li>
<li><a href="#two_fullstrength_programming_languages__html_and_perl">Two full-strength programming languages: HTML and Perl</a></li>
<li><a href="#multiple_views_and_reuses_of_the_same_element">Multiple views and reuses of the same element</a></li>
</ul>
<li><a href="#reap_the_benefits_of_using_html__tree">Reap the benefits of using HTML::Tree</a></li>
<ul>
<li><a href="#pragmatic_html_instead_of_strict_x_ht_ml">Pragmatic HTML instead of strict X(HT)ML</a></li>
<li><a href="#rich_api_and_user_contributions">Rich API and User Contributions</a></li>
</ul>
</ul>
<li><a href="#usage">USAGE</a></li>
<ul>
<li><a href="#understand_that_html_is_a_tree">Understand that HTML is a tree</a></li>
<li><a href="#install_and_setup_seamstress">Install and Setup Seamstress</a></li>
<li><a href="#text_substitution____node_mutation">Text substitution == node mutation</a></li>
<li><a href="#ifthenelse____node_s__deletion">If-then-else == <code>node(s)</code> deletion</a></li>
<li><a href="#looping____child_sibling_proliferation">Looping == child/sibling proliferation</a></li>
<li><a href="#what_seamstress_offers">What Seamstress offers</a></li>
<li><a href="#what_seamstress_works_with">What Seamstress works with</a></li>
<ul>
<li><a href="#class__cache">Class::Cache</a></li>
<li><a href="#the_family_of_html__tree_contributions">The family of HTML::Tree contributions</a></li>
lib/HTML/Seamstress.html view on Meta::CPAN
for tampering with the HTML. There are a wealth of XML-based modules
which provide this approach (<a href="/XML/Twig.html">the XML::Twig manpage</a>, <a href="/XML/LibXML.html">the XML::LibXML manpage</a>,
<a href="/XML/TreeBuilder.html">the XML::TreeBuilder manpage</a>, <a href="/XML/DOM.html">the XML::DOM manpage</a>). HTML::Seamstress is the one CPAN
module based around HTML and <a href="/HTML/Tree.html">the HTML::Tree manpage</a> for this approach.</p>
<p>
</p>
<hr />
<h1><a name="philosophy_and_motivation_of_html__seamstress">PHILOSOPHY and MOTIVATION of HTML::Seamstress</a></h1>
<p>When looking at HTML::Seamstress, we are looking at a uniquely
positioned 4th-generation HTML generator. Seamstress offers two sets
of advantages: those common to all 4th generation htmlgens and those
common to a subclass of <a href="/HTML/Tree.html">the HTML::Tree manpage</a>.</p>
<p>
</p>
<h2><a name="reap_4th_generation_dynamic_html_generation_benefits">Reap 4th generation dynamic HTML generation benefits</a></h2>
<p>What advantages does this fourth way of HTML manipulation offer? Let's
take a look:</p>
<p>
</p>
<h3><a name="separate_html_development_and_its_programmatic_modification">Separate HTML development and its programmatic modification</a></h3>
<p>The contents of the document remain legal HTML/XML that can be be
developed using standard interactive design tools. The flow of control
of the code remains separate from the page. Technologies that mix
content and data in a single file result in code that is often
difficult to understand and has trouble taking full advantage of the
object oriented programming paradigm.</p>
<p>
</p>
<h3><a name="reduced_learning_curve">Reduced learning curve</a></h3>
<p>If you have a strong hold on
object-oriented Perl and a solid understand of the tree-based nature
of HTML, then all you need to do is read the manual pages showing how
Seamstress and related modules offer tree manipulation routines and
you are done.</p>
<p>Extension just requires writing new Perl methods - a snap for any
object oriented Perler.</p>
<p>
</p>
<h3><a name="static_validation_and_formatting">Static validation and formatting</a></h3>
<p>Mixing Perl and HTML (by any of the generation 1-3 approaches)
makes it impossible to use standard validation and formatting tools
for either Perl or HTML.</p>
<p>
</p>
<h3><a name="two_fullstrength_programming_languages__html_and_perl">Two full-strength programming languages: HTML and Perl</a></h3>
<p>Perl and HTML are solid technologies with years of effort behind
making them robust and flexible enough to meet real-world
technological demands.</p>
<p>
</p>
<h3><a name="multiple_views_and_reuses_of_the_same_element">Multiple views and reuses of the same element</a></h3>
<p>Because manipulator and manipulated are separate, we can choose
manipulators and/or stack them at will.</p>
<p>
</p>
<h2><a name="reap_the_benefits_of_using_html__tree">Reap the benefits of using HTML::Tree</a></h2>
<p>
</p>
<h3><a name="pragmatic_html_instead_of_strict_x_ht_ml">Pragmatic HTML instead of strict X(HT)ML</a></h3>
<p>The real world is unfortunately more about getting HTML to work with
IE and maybe 1 or 2 other browsers. Strict XHTML may not be acceptable
under time and corporate pressures to get things to work with quirky
browsers.</p>
<p>
</p>
<h3><a name="rich_api_and_user_contributions">Rich API and User Contributions</a></h3>
<p><a href="/HTML/Tree.html">the HTML::Tree manpage</a> has a nice large set of accessor/modifier functions. If
that is not enough, then take a gander at Matthew Sisk's
contributions: <a href="http://search.cpan.org/~msisk/">http://search.cpan.org/~msisk/</a> as well as
<a href="/HTML/Element/Library.html">the HTML::Element::Library manpage</a>.</p>
<p>
</p>
<hr />
<h1><a name="usage">USAGE</a></h1>
<p>Now it's time to look at some examples. Before doing so, it is
imperative that you understand the tree structure of HTML.</p>
<p>
</p>
<h2><a name="understand_that_html_is_a_tree">Understand that HTML is a tree</a></h2>
<p>The best representation of this fact is this slide right here:</p>
<p><a href="http://xmlc.objectweb.org/doc/xmlcSlides/xmlcSlides.html#de">http://xmlc.objectweb.org/doc/xmlcSlides/xmlcSlides.html#de</a></p>
<p>If you understand this (and maybe the rest of the slides), then you
have a good grip on seeing HTML as a tree.</p>
<p><a href="/HTML/AboutTrees.html">the HTML::AboutTrees manpage</a> does also teach this, but it takes a while before
he gets to what matters to us. It's a fun read nonetheless.</p>
<p>Now that we've got this concept under our belts let's try some full examples.</p>
<p>
</p>
<h2><a name="install_and_setup_seamstress">Install and Setup Seamstress</a></h2>
<p>The first thing to remember is that Seamstress is really just
convenience functions for <a href="/HTML/Tree.html">HTML::Tree</a>. You can do
entirely without
Seamstress. It's just that my daily real-world obligations have lead
to a set of library functions (HTML::Element::Library) and a
convenient way to locate ``templates'' (<code>spkg.pl</code>) that work well on
top of <a href="/HTML/Tree.html">HTML::Tree</a></p>
<ul>
<li><strong><a name="item_move_spkg_2epl_and_sbase_2epl_onto_your_execution_">move spkg.pl and sbase.pl onto your execution <code>$PATH</code></a></strong><br />
</li>
<code>sbase.pl</code> and <code>spkg.pl</code> are used to simplify the process of
parsing an HTML file into HTML::Treebuilder object. In other words
instead of having to do this in your Perl programs:
<pre>
use HTML::TreeBuilder;</pre>
<pre>
my $tree = HTML::TreeBuilder->new_from_file('/usr/htdocs/hello.html');</pre>
<p>You can do this:</p>
<pre>
use htdocs::hello;</pre>
<pre>
my $tree = htdocs::hello->new;</pre>
<p>The lines of code is not much different, but abstracting away absolute
paths is important in production environments where the absolute path
may come from who knows where via who knows how.</p>
<p></p>
<li><strong><a name="item_run_sbase_2epl">run sbase.pl</a></strong><br />
</li>
sbase.pl will ask you 2 very simple questions. Just answer them.
When it is finished, it will have installed a package named
<code>HTML::Seamstress::Base</code> on your <code>@INC</code>. This module contains one
function, <code>comp_root()</code> which points to a place you wouldn't
( run in 1.134 second using v1.01-cache-2.11-cpan-13bb782fe5a )