Mojito
view release on metacpan or search on metacpan
bench/selectors.pl view on Meta::CPAN
<div style="margin:2px">
<table style="margin-left:auto;margin-right:auto">
<tr><td>Open </td><td style="text-align:right"> 0</td></tr>
</table>
<a href="https://rt.cpan.org/Public/Bug/Report.html?Queue=HTML-Zoom">Report a bug</a>
</div>
</div>
</div>
</div>
Module Version: 0.009003
<span class="noprint">
<a href="/src/MSTROUT/HTML-Zoom-0.009003/lib/HTML/Zoom.pm">Source</a>
</span>
<a name="___top"></a>
<div class=pod>
<div class=toc>
<div class='indexgroup'>
<ul class='indexList indexList1'>
<li class='indexItem indexItem1'><a href='#NAME'>NAME</a>
<li class='indexItem indexItem1'><a href='#SYNOPSIS'>SYNOPSIS</a>
<li class='indexItem indexItem1'><a href='#DANGER_WILL_ROBINSON'>DANGER WILL ROBINSON</a>
<li class='indexItem indexItem1'><a href='#DESCRIPTION'>DESCRIPTION</a>
<ul class='indexList indexList2'>
<li class='indexItem indexItem2'><a href='#JQUERY_ENVY'>JQUERY ENVY</a>
<li class='indexItem indexItem2'><a href='#CLEAN_TEMPLATES'>CLEAN TEMPLATES</a>
<li class='indexItem indexItem2'><a href='#PUTTING_THE_FUN_INTO_FUNCTIONAL'>PUTTING THE FUN INTO FUNCTIONAL</a>
<li class='indexItem indexItem2'><a href='#LAZINESS_IS_A_VIRTUE'>LAZINESS IS A VIRTUE</a>
<li class='indexItem indexItem2'><a href='#STOCKTON_TO_DARLINGTON_UNDER_STREAM_POWER'>STOCKTON TO DARLINGTON UNDER STREAM POWER</a>
<li class='indexItem indexItem2'><a href='#POP!_GOES_THE_WEASEL'>POP! GOES THE WEASEL</a>
<li class='indexItem indexItem2'><a href='#A_FISTFUL_OF_OBJECTS'>A FISTFUL OF OBJECTS</a>
<li class='indexItem indexItem2'><a href='#SEMANTIC_DIDACTIC'>SEMANTIC DIDACTIC</a>
<li class='indexItem indexItem2'><a href='#GET_THEE_TO_A_SUMMARY!'>GET THEE TO A SUMMARY!</a>
</ul>
<li class='indexItem indexItem1'><a href='#METHODS'>METHODS</a>
<ul class='indexList indexList2'>
<li class='indexItem indexItem2'><a href='#new'>new</a>
<li class='indexItem indexItem2'><a href='#zconfig'>zconfig</a>
<li class='indexItem indexItem2'><a href='#from_html'>from_html</a>
<li class='indexItem indexItem2'><a href='#from_file'>from_file</a>
<li class='indexItem indexItem2'><a href='#to_stream'>to_stream</a>
<li class='indexItem indexItem2'><a href='#to_fh'>to_fh</a>
<li class='indexItem indexItem2'><a href='#run'>run</a>
<li class='indexItem indexItem2'><a href='#apply'>apply</a>
<li class='indexItem indexItem2'><a href='#to_html'>to_html</a>
<li class='indexItem indexItem2'><a href='#memoize'>memoize</a>
<li class='indexItem indexItem2'><a href='#with_filter'>with_filter</a>
<li class='indexItem indexItem2'><a href='#select'>select</a>
<li class='indexItem indexItem2'><a href='#then'>then</a>
</ul>
<li class='indexItem indexItem1'><a href='#AUTHORS'>AUTHORS</a>
<li class='indexItem indexItem1'><a href='#LICENSE'>LICENSE</a>
</ul>
</div>
</div>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="NAME"
>NAME <img alt='^' src='http://st.pimg.net/tucs/img/up.gif'></a></h1>
<p>HTML::Zoom - selector based streaming template engine</p>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="SYNOPSIS"
>SYNOPSIS <img alt='^' src='http://st.pimg.net/tucs/img/up.gif'></a></h1>
<pre class="prettyprint"> use HTML::Zoom;
my $template = <<HTML;
<html>
<head>
<title>Hello people</title>
</head>
<body>
<h1 id="greeting">Placeholder</h1>
<div id="list">
<span>
<p>Name: <span class="name">Bob</span></p>
<p>Age: <span class="age">23</span></p>
</span>
<hr class="between" />
</div>
</body>
</html>
HTML
my $output = HTML::Zoom
->from_html($template)
->select('title, #greeting')->replace_content('Hello world & dog!')
->select('#list')->repeat_content(
[
sub {
$_->select('.name')->replace_content('Matt')
->select('.age')->replace_content('26')
},
sub {
$_->select('.name')->replace_content('Mark')
->select('.age')->replace_content('0x29')
},
sub {
$_->select('.name')->replace_content('Epitaph')
->select('.age')->replace_content('<redacted>')
},
],
{ repeat_between => '.between' }
)
->to_html;</pre>
<p>will produce:</p>
<pre class="prettyprint"> <html>
<head>
<title>Hello world &amp; dog!</title>
</head>
<body>
<h1 id="greeting">Hello world &amp; dog!</h1>
<div id="list">
<span>
<p>Name: <span class="name">Matt</span></p>
<p>Age: <span class="age">26</span></p>
</span>
<hr class="between" />
<span>
<p>Name: <span class="name">Mark</span></p>
<p>Age: <span class="age">0x29</span></p>
</span>
<hr class="between" />
<span>
<p>Name: <span class="name">Epitaph</span></p>
<p>Age: <span class="age">&lt;redacted&gt;</span></p>
</span>
</div>
</body>
</html></pre>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="DANGER_WILL_ROBINSON"
>DANGER WILL ROBINSON <img alt='^' src='http://st.pimg.net/tucs/img/up.gif'></a></h1>
<p>This is a 0.9 release. That means that I'm fairly happy the API isn't going to change in surprising and upsetting ways before 1.0 and a real compatibility freeze. But it also means that if it turns out there's a mistake the size of a p...
<p>Worse still, the rest of the distribution isn't documented yet. I'm sorry. I suck. But lots of people have been asking me to ship this, docs or no, so having got this class itself at least somewhat documented I figured now was a good time ...
<h1><a class='u' href='#___top' title='click to go to top of document'
name="DESCRIPTION"
>DESCRIPTION <img alt='^' src='http://st.pimg.net/tucs/img/up.gif'></a></h1>
<p>HTML::Zoom is a lazy, stream oriented, streaming capable, mostly functional, CSS selector based semantic templating engine for HTML and HTML-like document formats.</p>
<p>Which is, on the whole, a bit of a mouthful. So let me step back a moment and explain why you care enough to understand what I mean:</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="JQUERY_ENVY"
>JQUERY ENVY</a></h2>
<p>HTML::Zoom is the cure for JQuery envy. When your javascript guy pushes a piece of data into a document by doing:</p>
<pre class="prettyprint"> $('.username').replaceAll(username);</pre>
<p>In HTML::Zoom one can write</p>
<pre class="prettyprint"> $zoom->select('.username')->replace_content($username);</pre>
<p>which is, I hope, almost as clear, hampered only by the fact that Zoom can't assume a global document and therefore has nothing quite so simple as the $() function to get the initial selection.</p>
<p><a href="/perldoc?HTML%3A%3AZoom%3A%3ASelectorParser" class="podlinkpod"
>HTML::Zoom::SelectorParser</a> implements a subset of the JQuery selector specification, and will continue to track that rather than the W3C standards for the forseeable future on grounds of pragmatism. Also on grounds of their spec is written in EN...
<p>I am happy to admit that it's very, very much a subset at the moment - see the <a href="/perldoc?HTML%3A%3AZoom%3A%3ASelectorParser" class="podlinkpod"
>HTML::Zoom::SelectorParser</a> POD for what's currently there, and expect more and more to be supported over time as we need it and patch it in.</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="CLEAN_TEMPLATES"
>CLEAN TEMPLATES</a></h2>
<p>HTML::Zoom is the cure for messy templates. How many times have you looked at templates like this:</p>
<pre class="prettyprint"> <form action="/somewhere">
[% FOREACH field IN fields %]
<label for="[% field.id %]">[% field.label %]</label>
<input name="[% field.name %]" type="[% field.type %]" value="[% field.value %]" />
[% END %]
</form></pre>
<p>and despaired of the fact that neither the HTML structure nor the logic are remotely easy to read? Fortunately, with HTML::Zoom we can separate the two cleanly:</p>
<pre class="prettyprint"> <form class="myform" action="/somewhere">
<label />
<input />
</form>
$zoom->select('.myform')->repeat_content([
map { my $field = $_; sub {
$_->select('label')
->add_to_attribute( for => $field->{id} )
->then
->replace_content( $field->{label} )
->select('input')
->add_to_attribute( name => $field->{name} )
->then
->add_to_attribute( type => $field->{type} )
->then
->add_to_attribute( value => $field->{value} )
} } @fields
]);</pre>
bench/selectors.pl view on Meta::CPAN
<p>Because HTML::Zoom doesn't try and evaluate everything up front, you can generally put things together in whatever order is most appropriate. This means that:</p>
<pre class="prettyprint"> my $start = HTML::Zoom->from_html($html);
my $zoom = $start->select('div')->replace_content('THIS IS A DIV!');</pre>
<p>and:</p>
<pre class="prettyprint"> my $start = HTML::Zoom->select('div')->replace_content('THIS IS A DIV!');
my $zoom = $start->from_html($html);</pre>
<p>will produce equivalent final $zoom objects, thus proving that there can be more than one way to do it without one of them being a <a href="/perldoc?Switch" class="podlinkpod"
>bait and switch</a>.</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="STOCKTON_TO_DARLINGTON_UNDER_STREAM_POWER"
>STOCKTON TO DARLINGTON UNDER STREAM POWER</a></h2>
<p>HTML::Zoom's execution always happens in terms of streams under the hood - that is, the basic pattern for doing anything is -</p>
<pre class="prettyprint"> my $stream = get_stream_from_somewhere
while (my ($evt) = $stream->next) {
# do something with the event
}</pre>
<p>More importantly, all selectors and filters are also built as stream operations, so a selector and filter pair is effectively:</p>
<pre class="prettyprint"> sub next {
my ($self) = @_;
my $next_evt = $self->parent_stream->next;
if ($self->selector_matches($next_evt)) {
return $self->apply_filter_to($next_evt);
} else {
return $next_evt;
}
}</pre>
<p>Internally, things are marginally more complicated than that, but not enough that you as a user should normally need to care.</p>
<p>In fact, an HTML::Zoom object is mostly just a container for the relevant information from which to build the final stream that does the real work. A stream built from a Zoom object is a stream of events from parsing the initial HTML, wrapped in a...
<p>The upshot of this is that the application of filters works just as well on streams as on the original Zoom object - in fact, when you run a <a href="#repeat_content" class="podlinkpod"
>"repeat_content"</a> operation your subroutines are applied to the stream for that element of the repeat, rather than constructing a new zoom per repeat element as well.</p>
<p>More concretely:</p>
<pre class="prettyprint"> $_->select('div')->replace_content('I AM A DIV!');</pre>
<p>works on both HTML::Zoom objects themselves and HTML::Zoom stream objects and shares sufficient of the implementation that you can generally forget the difference - barring the fact that a stream already has state attached so things like to_fh are...
<h2><a class='u' href='#___top' title='click to go to top of document'
name="POP!_GOES_THE_WEASEL"
>POP! GOES THE WEASEL</a></h2>
<p>... and by Weasel, I mean layout.</p>
<p>HTML::Zoom's filehandle object supports an additional event key, 'flush', that is transparent to the rest of the system but indicates to the filehandle object to end a getline operation at that point and return the HTML so far.</p>
<p>This means that in an environment where streaming output is available, such as a number of the <a href="/perldoc?Plack" class="podlinkpod"
>Plack</a> PSGI handlers, you can add the flush key to an event in order to ensure that the HTML generated so far is flushed through to the browser right now. This can be especially useful if you know you're about to call a web service or a poten...
<p>This is currently exposed by the 'flush_before' option to the collect filter, which incidentally also underlies the replace and repeat filters, so to indicate we want this behaviour to happen before a query is executed we can write somethi...
<pre class="prettyprint"> $zoom->select('.item')->repeat(sub {
if (my $row = $db_thing->next) {
return sub { $_->select('.item-name')->replace_content($row->name) }
} else {
return
}
}, { flush_before => 1 });</pre>
<p>which should have the desired effect given a sufficiently lazy $db_thing (for example a <a href="/perldoc?DBIx%3A%3AClass%3A%3AResultSet" class="podlinkpod"
>DBIx::Class::ResultSet</a> object).</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="A_FISTFUL_OF_OBJECTS"
>A FISTFUL OF OBJECTS</a></h2>
<p>At the core of an HTML::Zoom system lurks an <a href="/perldoc?HTML%3A%3AZoom%3A%3AZConfig" class="podlinkpod"
>HTML::Zoom::ZConfig</a> object, whose purpose is to hang on to the various bits and pieces that things need so that there's a common way of accessing shared functionality.</p>
<p>Were I a computer scientist I would probably call this an "Inversion of Control" object - which you'd be welcome to google to learn more about, or you can just imagine a computer scientist being suspended upside down over a pit. Either...
<p>The ZConfig object hangs on to one each of the following for you:</p>
<ul>
<li>An HTML parser, normally <a href="/perldoc?HTML%3A%3AZoom%3A%3AParser%3A%3ABuiltIn" class="podlinkpod"
>HTML::Zoom::Parser::BuiltIn</a></li>
<li>An HTML producer (emitter), normally <a href="/perldoc?HTML%3A%3AZoom%3A%3AProducer%3A%3ABuiltIn" class="podlinkpod"
>HTML::Zoom::Producer::BuiltIn</a></li>
<li>An object to build event filters, normally <a href="/~mstrout/HTML-Zoom-0.009003/lib/HTML/Zoom/FilterBuilder.pm" class="podlinkpod"
>HTML::Zoom::FilterBuilder</a></li>
<li>An object to parse CSS selectors, normally <a href="/perldoc?HTML%3A%3AZoom%3A%3ASelectorParser" class="podlinkpod"
>HTML::Zoom::SelectorParser</a></li>
<li>An object to build streams, normally <a href="/perldoc?HTML%3A%3AZoom%3A%3AStreamUtils" class="podlinkpod"
>HTML::Zoom::StreamUtils</a></li>
</ul>
<p>In theory you could replace any of these with anything you like, but in practice you're probably best restricting yourself to subclasses, or at least things that manage to look like the original if you squint a bit.</p>
<p>If you do something more clever than that, or find yourself overriding things in your ZConfig a lot, please please tell us about it via one of the means mentioned under <a href="#SUPPORT" class="podlinkpod"
>"SUPPORT"</a>.</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="SEMANTIC_DIDACTIC"
>SEMANTIC DIDACTIC</a></h2>
<p>Some will argue that overloading CSS selectors to do data stuff is a terrible idea, and possibly even a step towards the "Concrete Javascript" pattern (which I abhor) or Smalltalk's Morphic (which I ignore, except for the part where it...
<p>To which I say, "eh", "meh", and possibly also "feh". If it really upsets you, either use extra classes for this (and remove them afterwards) or use special fake elements or, well, honestly, just use something different. <a...
>Template::Semantic</a> provides a similar idea to zoom except using XPath and XML::LibXML transforms rather than a lightweight streaming approach - maybe you'd like that better. Or maybe you really did want <a href="/perldoc?Template" class="pod...
>Template Toolkit</a> after all. It is still damn good at what it does, after all.</p>
<p>So far, however, I've found that for new sites the designers I'm working with generally want to produce nice semantic HTML with classes that represent the nature of the data rather than the structure of the layout, so sharing them as a com...
<p>In the absence of any evidence that overloading CSS selectors has killed children or unexpectedly set fire to grandmothers - and given microformats have been around for a while there's been plenty of opportunity for octagenarian combustion - I...
<h2><a class='u' href='#___top' title='click to go to top of document'
name="GET_THEE_TO_A_SUMMARY!"
>GET THEE TO A SUMMARY!</a></h2>
<p>Erm. Well.</p>
<p>HTML::Zoom is a lazy, stream oriented, streaming capable, mostly functional, CSS selector based semantic templating engine for HTML and HTML-like document formats.</p>
<p>But I said that already. Although hopefully by now you have some idea what I meant when I said it. If you didn't have any idea the first time. I mean, I'm not trying to call you stupid or anything. Just saying that maybe it wasn't tota...
<p>Er.</p>
<p>Maybe we should just move on to the method docs.</p>
<h1><a class='u' href='#___top' title='click to go to top of document'
name="METHODS"
>METHODS <img alt='^' src='http://st.pimg.net/tucs/img/up.gif'></a></h1>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="new"
>new</a></h2>
<pre class="prettyprint"> my $zoom = HTML::Zoom->new;
my $zoom = HTML::Zoom->new({ zconfig => $zconfig });</pre>
<p>Create a new empty Zoom object. You can optionally pass an <a href="/perldoc?HTML%3A%3AZoom%3A%3AZConfig" class="podlinkpod"
>HTML::Zoom::ZConfig</a> instance if you're trying to override one or more of the default components.</p>
<p>This method isn't often used directly since several other methods can also act as constructors, notable <a href="#select" class="podlinkpod"
>"select"</a> and <a href="#from_html" class="podlinkpod"
>"from_html"</a></p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="zconfig"
>zconfig</a></h2>
<section id=view_area></section>
<pre class="prettyprint"> my $zconfig = $zoom->zconfig;</pre>
<p>Retrieve the <a href="/perldoc?HTML%3A%3AZoom%3A%3AZConfig" class="podlinkpod"
>HTML::Zoom::ZConfig</a> instance used by this Zoom object. You shouldn't usually need to call this yourself.</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="from_html"
>from_html</a></h2>
<pre class="prettyprint"> my $zoom = HTML::Zoom->from_html($html);
my $z2 = $z1->from_html($html);</pre>
<p>Parses the HTML using the current zconfig's parser object and returns a new zoom instance with that as the source HTML to be transformed.</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="from_file"
>from_file</a></h2>
<pre class="prettyprint"> my $zoom = HTML::Zoom->from_file($file);
my $z2 = $z1->from_file($file);</pre>
<p>Convenience method - slurps the contents of $file and calls from_html with it.</p>
<h2><a class='u' href='#___top' title='click to go to top of document'
name="to_stream"
>to_stream</a></h2>
<pre class="prettyprint"> my $stream = $zoom->to_stream;
( run in 0.473 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )