* disappears
'div.foo' => {
'a' => undef,
},
# same as above
'div.foo a' => undef,
# xpath '.' = current node (itself)
'a#bar' => {
'.' => 'foobar',
'./@href' => 'foo.html',
},
# same as above
'a#bar' => 'foobar',
'a#bar/@href' => 'foo.html',
});
Loop
* selector => [ \%row, \%row, ... ]
*Array-ref of Hash-refs:* Loop the part as template. Each item of
the array-ref should be hash-ref.
$ts->process(\*DATA, {
'table.list tr' => [
{ 'th' => 'aaa', 'td' => '001' },
{ 'th' => 'bbb', 'td' => '002' },
{ 'th' => 'ccc', 'td' => '003' },
],
});
__DATA__
Output:
Callback
* selector => \&foo
*Code-ref:* Callback subroutine. The callback receives
$_ => innerHTML
$_[0] => XML::LibXML::Node object (X::L::Element, X::L::Attr, ...)
Its return value is handled per this list of value types (scalar to
replace content, undef to delete, etc.).
$ts->process($template, {
# samples
'h1' => sub { "bar" }, #
foo
=>
bar
'h1' => sub { undef }, #
foo
=> disappears
# sample: use $_
'h1' => sub { uc }, #
foo
=>
FOO
# sample: use $_[0]
'h1' => sub {
my $node = shift;
$node->nodeName; #
foo
=>
h1
},
});
Filter
* selector => [ $value, filter, filter, ... ]
*Array-ref of Scalars:* Value and filters. Filters may be
A) Callback subroutine (code reference)
B) Defined filter name
C) Object like Text::Pipe ("it->can('filter')")
$ts->process($template, {
'h1' => [ 'foo', sub { uc }, sub { "$_!" } ], # =>
FOO!
'h2' => [ ' foo ', 'trim', sub { "$_!" } ], # =>
FOO!
'h3' => [ 'foo', PIPE('UppercaseFirst') ], # =>
Foo
});
Defined basic filters
Some basic filters included. See Template::Semantic::Filter.
$ts->define_filter($filter_name, \&code)
You can define your own filters using "define_filter()".
use Text::Markdown qw/markdown/;
$ts->define_filter(markdown => sub { \ markdown($_) })
$ts->process($template, {
'div.content' => [ $text, 'markdown' ],
});
$code = $ts->call_filter($filter_name)
Accessor to defined filter.
$ts->process($template, {
'div.entry' => ...,
'div.entry-more' => ...,
})->process({
'div.entry, div.entry-more' => $ts->call_filter('markdown'),
});
SEE ALSO
Template::Semantic::Cookbook
Template::Semantic::Document
XML::LibXML, HTML::Selector::XPath
I got a lot of ideas from Template, Template::Refine, Web::Scraper.
thanks!
AUTHOR
Naoki Tomita
Feedback, patches, POD English check are always welcome!
LICENSE
This library is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.