/usr/share/perl5/HTML/HTML5/Builder.pm is in libhtml-html5-builder-perl 0.004-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 | package HTML::HTML5::Builder;
use 5.010;
use base qw[Exporter];
use common::sense;
use constant { FALSE => 0, TRUE => 1 };
use constant XHTML_NS => 'http://www.w3.org/1999/xhtml';
use overload;
use utf8;
use warnings::register;
BEGIN {
$HTML::HTML5::Builder::AUTHORITY = 'cpan:TOBYINK';
}
BEGIN {
$HTML::HTML5::Builder::VERSION = '0.004';
}
use Carp 0 qw();
use HTML::HTML5::Builder::Document;
use HTML::HTML5::Entities 0.001 qw();
use Scalar::Util 0 qw(blessed);
use XML::LibXML 1.60 qw();
my (@elements, @uc_elements, @conforming);
our (@EXPORT_OK, @EXPORT, %EXPORT_TAGS);
BEGIN
{
@elements = qw{
a abbr acronym address applet area article aside audio b base
basefont bb bdo bgsound big blink blockquote body br button canvas
caption center cite code col colgroup command datagrid datalist
dd del details dfn dialog dir div dl dt em embed fieldset figure
figcaption font footer form frame frameset h1 h2 h3 h4 h5 h6
head header hgroup hr html i iframe img input ins isindex kbd
keygen label legend li link listing map mark marquee menu meta
meter nav nobr noembed noframes noscript object ol optgroup
option output p param plaintext pre progress q rp rt ruby s
samp script select section small source spacer span strike
strong style sub sup summary table tbody td textarea tfoot th
thead time title tr track tt u ul var video wbr xmp
};
@uc_elements = qw{Q Sub Time Map Meta Link S};
@conforming = qw{
a abbr address area article aside audio b base bb bdo blockquote
body br button canvas caption cite code col colgroup command
datagrid datalist dd del details dfn dialog div dl dt em embed
fieldset figure footer form h1 h2 h3 h4 h5 h6 head header hr html
i iframe img input ins kbd label legend li mark menu
meter nav noscript object ol optgroup option output p param
pre progress rp rt ruby samp script section select small source
span strong style sup table tbody td textarea tfoot th thead
title tr ul var video
};
my @cool_stuff = qw{COMMENT CHUNK XML_CHUNK RAW_CHUNK ELEMENT TEXT};
my @boilerplate = qw{JQUERY CREATIVE_COMMONS OPENGRAPH};
@EXPORT_OK = (@elements, @cool_stuff, @uc_elements, @boilerplate);
@EXPORT = ();
%EXPORT_TAGS = (
all => \@EXPORT_OK,
standard => [@conforming, @cool_stuff, qw{Q Sub Time Map Meta Link}],
default => \@EXPORT,
metadata => [qw(head title base Link Meta style)],
sections => [qw(body div section nav article aside h1 h2 h3 h4 h5 h6 header footer address)],
grouping => [qw(p hr br pre dialog blockquote ol ul li dl dt dd)],
text => [qw(a cite em strong small mark dfn abbr progress
meter code var samp kbd sup span i b bdo ruby rt rp Q Sub Time)],
embedded => [qw(figure img iframe embed object param video audio source
canvas area Map)],
tabular => [qw(table thead tbody tfoot th td colgroup col caption)],
form => [qw(form fieldset label input button select datalist
optgroup option textarea output)],
);
}
sub new
{
my ($class, %options) = @_;
bless \%options, $class;
}
sub ELEMENT
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
my ($el, @params) = @_;
if (warnings::enabled())
{
Carp::carp("Non-standard HTML element <$el>")
unless grep { lc $el eq $_ } @elements;
}
my $EL = XML::LibXML::Element->new($el);
$EL->setNamespace(XHTML_NS, undef, TRUE);
if ($el eq 'time' and blessed($params[0]) and $params[0]->isa('DateTime'))
{
my $dt = shift @params;
my $string = $dt->strftime('%FT%T');
if ($dt->time_zone->is_utc)
{
$string .= 'Z';
}
elsif (!$dt->time_zone->is_floating)
{
my $zone = $dt->strftime('%z');
$zone =~ s/^(.\d{2})(\d{2})$/$1:$2/;
$string .= $zone;
}
$EL->setAttribute('datetime', $string);
if (!@params)
{
push @params, "$dt";
}
}
PARAM: while (@params)
{
my $thing = shift @params;
if (blessed($thing) and $thing->isa('XML::LibXML::Element'))
{
$EL->appendChild($thing);
}
elsif (blessed($thing) and $thing->isa('XML::LibXML::Text'))
{
$EL->appendChild($thing);
}
elsif (blessed($thing) and $thing->isa('XML::LibXML::Comment'))
{
$EL->appendChild($thing);
}
elsif (blessed($thing) and $thing->isa('XML::LibXML::PI'))
{
$EL->appendChild($thing);
}
elsif (blessed($thing) and $thing->isa('XML::LibXML::NodeList'))
{
$EL->appendChild($_) foreach $thing->get_nodelist;
}
elsif (blessed($thing) and $thing->isa('XML::LibXML::Attr'))
{
$EL->setAttribute($thing->nodeName, $thing->getValue);
}
elsif (ref $thing eq 'IO')
{
local $/ = undef;
my $string = <$thing>;
$EL->appendText($string);
}
elsif (ref $thing eq 'SCALAR')
{
$$thing = $EL;
}
elsif (ref $thing eq 'ARRAY')
{
unshift @params, map
{ ref $_ ? $_ : XML::LibXML::Text->new($_); }
@$thing;
redo PARAM;
}
elsif (ref $thing eq 'HASH')
{
while (my ($k, $v) = each %$thing)
{
$k =~ s/^-//g;
$EL->setAttribute($k, "$v");
}
}
elsif (!ref $thing and $thing =~ /^-(\S+)$/ and @params)
{
my $attr = $1;
my $value = shift @params;
$EL->setAttribute($attr, "$value");
}
elsif (defined $thing)
{
if (warnings::enabled())
{
if (defined ref($thing) and ref($thing) =~ /^(CODE|REF|GLOB|LVALUE|FORMAT|Regexp)$/)
{ Carp::carp(sprintf("Passed a %s reference", ref($thing))); }
elsif (blessed($thing) and !overload::Method($thing, '""'))
{ Carp::carp(sprintf("Passed a blessed reference (%s) that does not overload stringification", ref($thing))); }
}
$EL->appendText("$thing");
}
}
if ($el eq 'html')
{
my $doc = HTML::HTML5::Builder::Document->new('1.0', 'utf-8');
$doc->adoptNode($EL);
$doc->setDocumentElement($EL);
return $doc;
}
return $EL;
}
sub _mksub
{
no strict 'refs';
my ($function, $element) = @_;
$element = lc $function unless defined $element and length $element;
my $sub = sub
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
return ELEMENT($element, @_);
};
return $sub;
}
sub AUTOLOAD
{
my ($func) = our $AUTOLOAD =~ /::(\w+)$/;
Carp::croak("Undefined function") unless $func =~ /^(([A-Za-z][a-z]*)|([Hh][1-6]))$/;
Carp::croak("Undefined function") unless grep { lc $func eq $_ } @elements;
my $sub = *{$func} = _mksub($func);
return $sub->(@_);
}
sub DESTROY {} # not AUTOLOAD
# Thanks to AUTOLOAD, UNIVERSAL::can is a little broken here, so instead
# we define out own can.
sub can
{
my ($class, $func) = @_;
my $answer = UNIVERSAL::can(__PACKAGE__, $func)
|| __PACKAGE__->SUPER::can($func);
return $answer if $answer;
if ($func =~ /^(([A-Za-z][a-z]*)|([Hh][1-6]))$/
and grep { lc $func eq $_ } @elements)
{
return _mksub($func);
}
return;
}
sub TEXT
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
return XML::LibXML::TextNode->new($_[0]);
}
sub COMMENT
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
return XML::LibXML::Comment->new($_[0]);
}
{
my $parser = undef;
sub CHUNK
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
unless ($parser)
{
eval 'use HTML::HTML5::Parser; 1;'
or Carp::croak("This feature requires HTML::HTML5::Parser.\n");
$parser = HTML::HTML5::Parser->new;
}
my $dom = $parser->parse_string($_[0]);
my @kids = $dom->getElementsByTagName('body')->shift->childNodes;
return @kids;
}
}
{
my $parser = undef;
sub XML_CHUNK
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
unless ($parser)
{
$parser = XML::LibXML->new;
}
my $dom = $parser->parse_balanced_chunk($_[0]);
my @kids = $dom->childNodes;
return @kids;
}
}
{
my $dummyDoc = XML::LibXML::Document->new;
sub RAW_CHUNK
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
return $dummyDoc->createPI('decode', HTML::HTML5::Entities::encode_numeric($_[0]));
}
}
sub _find_version
{
my ($req, @versions) = @_;
return $versions[-1] unless defined $req;
my $requested = do {
my ($maj, $min, $rev) = split /\./, $req;
($maj||0)*1_000_000 + ($min||0)*1_000 + ($rev||0);
};
foreach my $v (@versions)
{
my $thisver = do {
my ($maj, $min, $rev) = split /\./, $v;
($maj||0)*1_000_000 + ($min||0)*1_000 + ($rev||0);
};
if ($thisver >= $requested)
{
return $v;
}
}
return $req;
}
sub JQUERY
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
my (%opts) = (scalar @_==1) ? (-version => $_[0]) : @_;
my @rv;
my $templates = {
official => {
jQuery => 'http://code.jquery.com/jquery-%s.%s',
jQuery_v => '1.2.1, 1.2.2, 1.2.3, 1.2.4, 1.2.5, 1.2.6, 1.3.0, 1.3.1, 1.3.2, 1.4.0, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.5.0, 1.5.1, 1.5.2, 1.6.0, 1.6.1, 1.6.2, 1.6.3, 1.6.4',
jQueryUI => 'http://code.jquery.com/ui/%s/jquery-ui.%s',
jQueryUI_v => '1.7.0, 1.7.1, 1.7.2, 1.7.3, 1.8.0, 1.8.1, 1.8.2, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, 1.8.10, 1.8.11, 1.8.12, 1.8.13, 1.8.14, 1.8.15, 1.8.16',
Style => 'http://code.jquery.com/ui/%s/themes/%s/jquery-ui.css',
Themes => 'base black-tie blitzer cupertino dark-hive dot-luv eggplant excite-bike flick hot-sneaks humanity le-frog mint-choc overcast pepper-grinder redmond smoothness south-street start sunny swanky-purse trontastic ui-darkness ui-lightness vader',
},
google => {
jQuery => 'https://ajax.googleapis.com/ajax/libs/jquery/%s/jquery.%s',
jQuery_v => '1.2.3, 1.2.6, 1.3.0, 1.3.1, 1.3.2, 1.4.0, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.5.0, 1.5.1, 1.5.2, 1.6.0, 1.6.1, 1.6.2, 1.6.3, 1.6.4',
jQueryUI => 'https://ajax.googleapis.com/ajax/libs/jqueryui/%s/jquery-ui.%s',
jQueryUI_v => '1.5.2, 1.5.3, 1.6.0, 1.7.0, 1.7.1, 1.7.2, 1.7.3, 1.8.0, 1.8.1, 1.8.2, 1.8.4, 1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, 1.8.10, 1.8.11, 1.8.12, 1.8.13, 1.8.14, 1.8.15, 1.8.16',
Style => 'http://ajax.googleapis.com/ajax/libs/jqueryui/%s/themes/%s/jquery-ui.css',
Themes => 'base black-tie blitzer cupertino dark-hive dot-luv eggplant excite-bike flick hot-sneaks humanity le-frog mint-choc overcast pepper-grinder redmond smoothness south-street start sunny swanky-purse trontastic ui-darkness ui-lightness vader',
},
microsoft => {
jQuery => 'http://ajax.aspnetcdn.com/ajax/jquery/jquery-%s.%s',
jQuery_v => '1.3.2, 1.4, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.5, 1.5.1, 1.5.2, 1.6, 1.6.1, 1.6.2, 1.6.3, 1.6.4',
jQueryUI => 'http://ajax.aspnetcdn.com/ajax/jquery.ui/%s/jquery-ui.%s',
jQueryUI_v => '1.8.5, 1.8.6, 1.8.7, 1.8.8, 1.8.9, 1.8.10, 1.8.11, 1.8.12, 1.8.13, 1.8.14, 1.8.15, 1.8.16',
Style => 'http://ajax.aspnetcdn.com/ajax/jquery.ui/%s/themes/%s/jquery-ui.css',
Themes => 'base black-tie blitzer cupertino dark-hive dot-luv eggplant excite-bike flick hot-sneaks humanity le-frog mint-choc overcast pepper-grinder redmond smoothness south-street start sunny swanky-purse trontastic ui-darkness ui-lightness vader',
},
};
my $template = $templates->{ lc $opts{-source}||'google' } || $templates->{'google'};
my @jquery_versions = split /\s*\,\s*/, $template->{jQuery_v};
my $url = sprintf($template->{jQuery},
_find_version($opts{-version}, @jquery_versions),
(!defined $opts{-min} or $opts{-min})?'min.js':'js',
);
push @rv, script(
-type => 'text/javascript',
-src => $url,
);
if ((defined $opts{-ui} or defined $opts{-ui_version} or defined $opts{-theme})
and not (defined $opts{-ui} and !$opts{-ui}))
{
my @jqueryui_versions = split /\s*\,\s*/, $template->{jQueryUI_v};
my $url = sprintf($template->{jQueryUI},
_find_version($opts{-ui_version}, @jqueryui_versions),
(!defined $opts{-min} or $opts{-min})?'min.js':'js',
);
push @rv, script(
-type => 'text/javascript',
-src => $url,
);
if (defined $opts{-theme})
{
my $theme = $opts{-theme};
$theme = 'base' unless $template->{Themes} =~ /\b$theme\b/;
my $url = sprintf($template->{Style},
_find_version($opts{-ui_version}, @jqueryui_versions),
$theme,
);
push @rv, Link(
-rel => 'stylesheet',
-media => 'screen',
-type => 'text/css',
-src => $url,
);
}
}
return wantarray ? @rv : XML::LibXML::NodeList->new_from_ref(\@rv, 1);
}
sub CREATIVE_COMMONS
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
my (%opts) = (scalar @_==1) ? (-licence => $_[0]) : @_;
$opts{-licence} ||= delete $opts{-license}; # be kind to Americans
$opts{-licence} ||= 'by-sa';
my @rv;
if (warnings::enabled())
{
Carp::carp("Unknown licence")
unless lc $opts{-licence} =~ /^by(-(sa|nd|nc|nc-sa|nc-nd))?$/;
}
my $size = {
'small' => '80x15',
'large' => '88x31',
'80x15' => '80x15',
'88x31' => '88x31',
}->{ lc $opts{-size} || 'large' } || '88x31';
push @rv, a(
-rel => 'license',
-href => sprintf("http://creativecommons.org/licenses/%s/3.0/", lc $opts{-licence}),
img(
-alt => ($opts{-alt} || sprintf("Creative Commons %s Licence", uc $opts{-licence})),
-style => 'border:0',
-src => sprintf("http://i.creativecommons.org/l/%s/3.0/%s.png", lc $opts{-licence}, $size),
),
);
if (defined $opts{-attributionName}
or defined $opts{-attributionURL}
or defined $opts{-type}
or defined $opts{-title})
{
my ($this_work, $attribution);
push @rv, br(), span(
span(\$this_work, $opts{-title}||'This work'),
defined $opts{-attributionName} ? [' by ', span(\$attribution, -property=>'cc:attributionName', [$opts{-attributionName}])] : [],
' is licensed under a ',
a(
-rel => 'license',
-href => sprintf("http://creativecommons.org/licenses/%s/3.0/", lc $opts{-licence}),
[$opts{-alt} || sprintf("Creative Commons %s Licence", uc $opts{-licence})],
),
);
if (defined $opts{-title})
{
$this_work->setAttribute(property => 'dc:title');
}
if (defined $opts{-type} and length $opts{-type})
{
$this_work->setAttribute(rel => 'dc:type');
$this_work->setAttribute(resource => sprintf('http://purl.org/dc/dcmitype/%s', ucfirst lc $opts{-type}));
}
if (defined $attribution and defined $opts{-attributionURL})
{
$this_work->setAttribute(rel => 'cc:attributionURL');
$this_work->setAttribute(resource => $opts{-attributionURL});
}
}
return span(@rv,
-class=>'creative_commons',
(defined $opts{-url} ? { -about => $opts{-url} } : {}),
);
}
sub OPENGRAPH
{
shift if blessed($_[0]) && $_[0]->isa(__PACKAGE__);
my (%opts) = (scalar @_==1) ? %{$_[0]} : @_;
my $map = {
title => 'og:title dc:title',
url => 'og:url dc:identifier',
description => 'og:description dc:description',
};
my @rv;
while (my ($key, $value) = each %opts)
{
$key = lc $key;
$key =~ s/^-//;
push @rv, Meta(-property => ($map->{$key}||sprintf('og:%s', $key)), -content => $value);
}
return wantarray ? @rv : XML::LibXML::NodeList->new_from_ref(\@rv, 1);
}
1;
__END__
=head1 NAME
HTML::HTML5::Builder - erect some scaffolding for your documents
=head1 SYNOPSIS
use HTML::HTML5::Builder qw[:standard JQUERY];
open my $fh, '<', 'inline-script.js';
print html(
-lang => 'en',
head(
title('Test'),
Meta(-charset => 'utf-8'),
),
body(
h1('Test'),
p('This is a test.'),
JQUERY(-version => '1.6.4'),
script(-type => 'text/javascript', $fh),
),
);
=head1 DESCRIPTION
This module can export function names corresponding to any HTML5 element.
Each function returns an XML::LibXML::Element with the same name as the
function. The arguments to each function are processed as a list, and
used to set the attributes and contents of that element.
For each item on the list:
=over
=item * if it's an XML::LibXML::Element, XML::LibXML::TextNode,
XML::LibXML::Comment, or XML::LibXML::PI, it's appended as a
child of the returned element.
=item * if it's an XML::LibXML::NodeList, each item on the list is
appended as a child of the returned element.
=item * if it's an XML::LibXML::Attr, it's set as an attribute on
the returned element
=item * if it's an IO::Handle, then it will be slurped and appended
to the returned element as a text node.
=item * if it's a scalar reference, then the returned element is also
assigned to it. (This feature is B<at risk>.)
=item * if it's a scalar (string) some guesswork is conducted to figure
out whether you're setting an attribute and value, or whether the string
should be used as a text node. The presence of a hyphen at the start of
the string is the main deciding factor.
p('-class', 'warning', '$LordLucan not found.');
In this example, a paragraph element is returned, with the class attribute
set to 'warning' and the textual contents '$LordLucan not found.'.
Sometimes it's necessary to protect values against this guesswork. By
passing a hashref, all the keys and values are interpreted as setting
attributes; by passing an arrayref, all values are interpreted as setting
the contents of the element.
p(['-class'], { warning => '$LordLucan not found.' });
In this example, a paragraph element is returned, with the warning attribute
set to '$LordLucan not found.' and the textual contents '-class'.
=item * Anything else is stringified and added as a text node. This is
useful for things with sensible stringification defined, such as C<DateTime>
and C<URI> objects, but less so for some other objects, so you will
sometimes get a warning if warnings are enabled. Warnings can be disabled
using:
no warnings 'HTML::HTML::Builder';
=back
=head2 Exceptional Cases
The C<html> function does not return an C<XML::LibXML::Element>, but rather a
C<HTML::HTML5::Builder::Document> object.
There is special handling for C<time> (or C<Time>). If the first parameter passed to
it is a L<DateTime> object, then that object is used to set its datetime attribute.
If there are no subsequent parameters, then the stringified form of the object is also
used to form the content of the <time> element.
Note that the functions that generate <meta>, <link>, <q>, <time>, <sub>, <s> and <map>
HTML elements are named C<< Meta() >>, C<< Link() >>, C<< Q() >>, C<< Time() >>, C<< Sub() >>,
C<< S() >> and C<< Map() >> respectively, with an upper-case first letter. This is
because each of these names corresponds to a built-in perl keyword (except meta,
which is used by Moose). The lower-case versions of these do exist, and can be
exported if you ask for them explicitly. The lower-case versions are also
available as methods using the object-oriented syntax. (In fact, lower case and ucfirst
versions exist for all HTML elements - they're just not always exportable.)
=head2 General Purpose Functions
=over
=item C<< ELEMENT($tagname, @arguments) >>
If you need to insert an element which doesn't have its own function.
=item C<< TEXT($string) >>
Produces a text node.
=item C<< COMMENT($string) >>
Produces an HTML comment.
=item C<< CHUNK($string) >>
Parses the string as HTML, and produces a list of elements, text nodes and
comments.
This should be a so-called "balanced chunk". Due to limitations in
L<HTML::HTML5::Parser>, this only works for body content. Croaks if
L<HTML::HTML5::Parser> is not installed.
=item C<< XML_CHUNK($string) >>
More useful version of C<CHUNK>, without the restriction on content,
but input needs to be a balanced and well-formed XML chunk.
=item C<< RAW_CHUNK($string) >>
This allows you to include stuff that isn't anything close to valid
HTML into the output document, such as a PHP block. e.g.
html(
head(
title('Funny test'),
),
body(
h1('Funny test'),
RAW_CHUNK("<p>Here's a fish: <=><"),
),
);
A processing instruction is used to represent this data in the DOM.
HTML::HTML5::Writer can detect that processing instruction and use it to
output the raw data. If you're not using HTML::HTML5::Writer to serialise
the document, then you may need to post-process the serialised document.
With great power comes great responsibility.
=back
=head2 Boiler-Plate Functions
There are also a number of functions that create lists of multiple HTML
elements, for boiler-plate code.
=over
=item C<< JQUERY(-version => $ver, %options) >>
Link to jQuery at a CDN.
Other options include B<-source>, to indicate where to link to jQuery (currently
allowed values are "Google", "Microsoft" and "official"); and B<-min>, a boolean
which indicates whether the minified version should be linked to (true by
default).
Setting option B<-ui> to true, also includes jQuery UI. A version number can be
indicated using B<-ui_version>. A theme can be included setting B<-theme>. Setting
either B<-ui_version> or B<-theme> will imply B<-ui>.
JQUERY(
-source => 'official',
-version => '1.6.4',
-ui_version => '1.8.16',
-theme => 'eggplant',
);
If versions aren't provided, defaults to the latest versions of the libraries
that the author of HTML::HTML5::Builder was aware of at the time of publication.
If you choose a version which is known to be unavailable at the selected CDN,
the function should automatically choose a slightly later version.
=item C<< CREATIVE_COMMONS($licence) >>
=item C<< CREATIVE_COMMONS(-licence => $licence, %options) >>
C<$licence> can be one of 'by', 'by-nd', 'by-nc', 'by-sa', 'by-sa-nc',
or 'by-sa-nd'.
Other options supported are:
=over
=item * B<-url> - URL of the thing being licensed (if not the page itself)
=item * B<-size> - 'large' or 'small' for the image
=item * B<-title> - title of the work
=item * B<-attributionName> - name people should use for attribution
=item * B<-attributionURL> - link people should use for attribution
=back
=item C<< OPENGRAPH(%data) >>
Returns a list of <meta> elements providing Open Graph Protocol data
for your page.
OPENGRAPH(
-title => "Hello World",
-type => "example",
-description => "A global greeting.",
);
=back
=head2 Exporting Functions
None by default. Pretty much anything can be exported on request.
Export tags:
=over
=item * C<:all> - everything
=item * C<:standard> - elements that are not obsolete in HTML5, plus ELEMENT, TEXT, COMMENT, CHUNK, XML_CHUNK and RAW_CHUNK
=item * C<:metadata> - head title base Link Meta style
=item * C<:sections> - body div section nav article aside h1 h2 h3 h4 h5 h6 header footer address
=item * C<:grouping> - p hr br pre dialog blockquote ol ul li dl dt dd
=item * C<:text> - a Q cite em strong small mark dfn abbr progress meter code var samp kbd Sub sup span i b bdo ruby rt rp Time
=item * C<:embedded> - figure img iframe embed object param video audio source canvas area
=item * C<:tabular> - table thead tbody tfoot th td colgroup col caption
=item * C<:form> - form fieldset label input button select datalist optgroup option textarea output
=back
=head2 Object Oriented Interface
You can also use these functions as methods of an object blessed into
the L<HTML::HTML5::Builder> package.
my $b = HTML::HTML5::Builder->new;
my $document = $b->html(
-lang => 'en',
$b->head(
$b->title('Test', \(my $foo)),
$b->meta(-charset => 'utf-8'),
),
$b->body(
$b->h1('Test'),
$b->p('This is a test.')
),
);
=head2 Using with RDF::RDFa::Generator
L<RDF::RDFa::Generator> has a C<nodes> method which returns a handy list of
C<XML::LibXML::Node> objects.
use DateTime;
use HTML::HTML5::Builder qw[:standard];
use RDF::RDFa::Generator;
use RDF::Trine;
my $url = 'http://dbpedia.org/data/Charles_Darwin';
my $model = RDF::Trine::Model->new;
RDF::Trine::Parser->parse_url_into_model($url, $model);
my $gen = RDF::RDFa::Generator->new(style=>'HTML::Pretty');
print html(
head(
title("Some Data About Charles Darwin"),
),
body(
h1("Some Data About Charles Darwin"),
$gen->nodes($model),
hr(),
address(
"Source: $url", br(),
"Generated: ", Time(DateTime->now),
),
),
);
Nice?
=head2 Using with XML::LibXML::PrettyPrint
HTML::HTML5::Builder doesn't nicely indent your markup, but
L<XML::LibXML::PrettyPrint> can.
use HTML::HTML5::Builder qw(:standard);
use XML::LibXML::PrettyPrint qw(print_xml);
print_xml html(
head(title("Test")),
body(h1("Test"), p("This is a test.")),
);
=head1 BUGS
Please report any bugs to
L<http://rt.cpan.org/Dist/Display.html?Queue=HTML-HTML5-Builder>.
=head1 SEE ALSO
L<XML::LibXML>,
L<HTML::HTML5::Writer>,
L<HTML::HTML5::Builder::Document>.
=head1 AUTHOR
Toby Inkster E<lt>tobyink@cpan.orgE<gt>.
=head1 COPYRIGHT AND LICENCE
This software is copyright (c) 2011 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=head1 DISCLAIMER OF WARRANTIES
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|