|
NAMEHTML::GenerateUtil - Routines useful when generating HTML output SYNOPSIS use HTML::GenerateUtil qw(escape_html generate_attributes generate_tag escape_uri :consts $H div);
my $Html = "text < with > things & that need \x{1234} escaping";
$Html = escape_html($Html);
Or
escape_html($Html, EH_INPLACE);
Also
my $Attr = generate_attributes({ href => 'http://...', title => 'blah' });
$Html = "<a $Attr>$Html</a>";
But even better
$Html = generate_tag('a', { href => 'http://...', title => 'blah' }, $Html, 0);
Also you might want something like
my $URI = 'http://host/?' . join ";", map { $_ => escape_uri($Params{$_}) } keys %Params;
$Html = generate_tag('a', { href => $URI }, $Html, 0);
You can shortcut that by importing a function, or using the autoloading $H object
div({ class => [ qw(a b) ] }, "div content");
$H->a({ href => $URI }, "text", GT_ADDNEWLINE);
Or the newer (>= 1.20) $E object with more smarts
$E->tr(
[
\$E->th("row 1 heading with <>& nasties"),
\$E->td( { class => "someclassforeachrow" },
"column 1",
\"column <b>2</b> with trusted html",
)
], [
\$E->th(\"row 2 heading with <b>trusted</b> html"),
\$E->td(
"column 1",
\"column <b>2</b> with trusted html",
)
]
)
DESCRIPTIONProvides a number of functions that make generating HTML output easier and faster. All written in XS for speed. CONTEXTWhen creating a web application in perl, you've got a couple of main choices on how to actually generate the HTML that gets output:
Your actual application, experience and environment will generally determine which is the best way to. If you go the programatic route, then you generally need some way of generating the actual HTML output in perl. Again, there's generally a couple of ways of doing this.
The first seems easy, but it gets harder when you have to manually escape each string to avoid placing special HTML chars (eg <, etc) in strings like $text above. With the CGI, most of this is automatically taken care of, and most strings are automatically escaped to replace special HTML chars with their entity equivalents. While this is nice, CGI is written in pure perl, and can end up being a bit slow, especially if you already have a fast system that generates pages very heavy in tags (eg lots of table elements, links, etc) That's where this module comes it. It provides functions useful for escaping html and generating HTML tags, but it's all written in XS to be very fast. It's also fully UTF-8 aware. FUNCTIONS
Useful for turning text into similar to <pre> form without actually being in <pre> tags
$Mode is a bit field with the additional options:
$EscapeChars is optional characters to escape instead of default set If supplied, only these characters (and always any ctrl or 8-bit characters) are escaped rather than the unreserved set above. OBJECTS
To pass flags to generate_tag or escape_html, add _gtflags or _ehflags item to initial attributes hash. Examples: $E->tag() -> '<tag>'
$E->tag({a=>"b"}) -> '<tag a="b">'
$E->tag("text") -> '<tag>text</tag>'
$E->tag("t<>t") -> '<tag>t<>t</tag>'
$E->tag("t<",\"t<boo>") -> '<tag>t<</tag><tag>t<boo></tag>'
$H->tag({a=>"b"},"c","<") -> '<tag a="b">c</tag><tag a="b"><</tag>'
$H->tag(["a","2"]) -> '<tag>a2</tag>'
$H->tag(["t<", \"t<boo>"]) -> '<tag>t<t<boo></tag>'
BUGS AND LIMITATIONSThe EH_LEAVEKNOWN option is just heuristic, and accepts anything that even looks like an entity reference, even if it isn't a correct one. I'm not sure if this is a security issue or not. SEE ALSOApache::Util, HTML::Entities, CGI Latest news/details can also be found at: <http://cpan.robm.fastmail.fm/htmlgenerateutil/> Available on github at: <https://github.com/robmueller/html-generateutil/> AUTHORRob Mueller <cpan@robm.fastmail.fm> COPYRIGHT AND LICENSECopyright (C) 2004-2014 by FastMail Pty Ltd This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
|