Constructs a pretty-printer object.
|new_for_html(%options)||Constructs a pretty printer object pre-configured to be suitable for HTML and XHTML. The <B>indent_stringB> and <B>new_lineB> options are supported.|
If you just need to use a default configuration (no options passed to the constructor, then you can call these as class methods, unless otherwise stated.
strip_whitespace($node) Strips superfluous whitespace from an XML::LibXML::Document or XML::LibXML::Element.
Whitespace just before, just after or leading/trailing within an inline element is not considered superfluous. Runs of multiple whitespace characters are replaced with a single space. Whitespace is not changed within an element that preserves whitespace.
The node is modified in place.
indent($node, $level) Indents the node to a certain indentation level, and its direct children to $level + 1, grandchildren to $level + 2, etc. Typically youd just want to indent the root node to level 0.
The node is modified in place.
Elements that preserve whitespace are not changed.
pretty_print($node, $level) Strip whitespace and indent. The node is modified in place and returned.
Example use as a class method:
indent_string($level) Returns the string that would be used to indent something to a particular level. Descendent classes could override this method to do funky indentation, such as having varying levels of indentation. new_line Returns the string that would be used to begin a new line. element_category($node) Returns EL_INLINE, EL_BLOCK, EL_COMPACT or undef. element_preserves_whitespace($node) Boolean indicating whether the contents of the element have significant whitespace that needs preserving.
print_xml $xml Given an XML string or an XML::LibXML::Node object, prints it nicely.
This function is not exported by default, but can be requested:
use XML::LibXML::PrettyPrint 0.001 qw(print_xml);
Use like this:
print_xml <foo> <bar> </bar> </foo>;
IO::Handle::print_xml($handle, $xml) Partly experimental, partly mental. You can enable this feature like this:
use XML::LibXML::PrettyPrint 0.001 qw(-io);
And that will allow stuff like this to work:
These can be exported:
use XML::LibXML::PrettyPrint 0.001 qw(:constants);
EL_BLOCK EL_COMPACT EL_INLINE
There are three categories of element: inline, block and compact.
For inline elements the presence of whitespace (though not the amount of whitespace) is considered significant just before the element, just after the element, or just within the element.
In XHTML, consider the difference between the block element <div>:
and the inline element <span>:
The space or lackthereof between <div> elements does not matter one whit. The lack of spaces between the first two <span> elements allows them to be read as a single (in this case, hyphenated) word, whereas the space before the third <span> separates out the word lives.
In terms of indentation, inline elements do not start a new indented line, unless they are the first element within their block, or are preceded by a block or compact element.
Block elements always start a new line, and cause their child nodes to be indented to the next level.
Compact elements are somewhere in-between. When it comes to whitespace stripping, theyre treated as block elements. In terms of indentation, they always start a new line, but they only cause their child nodes to be indented to the next level if they have block descendents. If we imagine that in HTML, <ul> is a block element, <i> is an inline element, and <li> is a compact element:
<ul> <li>Will Smith - Will Smith</li> <li>Carlton Banks - Alfonso Ribeiro</li> <li> Vivian Banks: <ul> <li>Janet Hubert-Whitten <i>(seasons 1-3)</i></li> <li>Daphne Maxwell Reid <i>(seasons 3-6)</i></li> </ul> </li> </ul>
The third <li> element is indented like a block element because it contains a block <ul> element. The other <li> elements do not have their contents indented, because they contain only inline content.
Elements default to being block, but you can specify particular elements as inline or compact by passing node names or callbacks to the constructor. Elements default to not preserving whitespace unless they have an xml:space="preserve" attribute, but again you can use the constructor to change this.
Comments and processing instructions default to being compact, but you can make particular comments or PIs inline or block by passing appropriate callbacks to the constructor. Whitespace within comments and PIs is always preserved. (There is rarely any reason to make comments and processing instructions block, but making them inline can occasionally be useful, as it will mean that the presence of whitespace just before or just after the comment is treated as significant.)
Text nodes are always inline.
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=XML-LibXML-PrettyPrint>.
Related: XML::LibXML, HTML::HTML5::Writer.
XML::Tidy - similar, but based on XML::XPath. Doesnt differentiate between inline and block elements.
Sermon: <http://www.derkarl.org/why_to_tabs.html>. Read it.
Toby Inkster <firstname.lastname@example.org>.
This software is copyright (c) 2011-2014 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.
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.
|perl v5.20.3||XML::LIBXML::PRETTYPRINT (3)||2014-09-07|