Reindent(3pm)

πŸ’– Reindent(3pm) β€” the Perl doc page, but make it cute πŸ’–

ok so bestie, this is XML::Filter::Reindent from the User Contributed Perl Documentation. it lives in section 3pm of the manual, and it's here to make your XML pretty. yes, actually pretty. let's get into it πŸŽ€


✨ NAME ✨

XML::Filter::Reindent β€” reformats whitespace so your XML prints all pretty πŸ’…


🌸 SYNOPSIS 🌸

use XML::Handler::Composer;
use XML::Filter::Reindent;

my $composer = new XML::Handler::Composer (%OPTIONS);
my $indent = new XML::Filter::Reindent (Handler => $composer, %OPTIONS);

that's it. that's the whole vibe. you stack a reindent filter in front of a composer and let them cook πŸ”₯


πŸ¦‹ DESCRIPTION πŸ¦‹

XML::Filter::Reindent is a sub class of XML::Filter::DetectWS. like, it inherits from it, it's giving family tree energy 🌳

you can use XML::Filter::Reindent as a PerlSAX filter to reformat an XML document before you hand it off to a PerlSAX handler that prints it β€” think XML::Handler::Composer. so the filter does its little makeover backstage and the printer gets a glow-up document. ✨

like XML::Filter::DetectWS, it detects ignorable whitespace and blocks of whitespace characters in certain places. then it takes that info, plus info you supplied, and figures out where whitespace is allowed to be modified, deleted, or inserted. based on the indent settings, it then modifies, inserts and deletes characters and ignorable_whitespace events accordingly. no cap, that's literally the job.

⚠️ heads up bestie: this is just a first stab at the implementation. it may be buggy and it may change completely! πŸ’€

πŸ‘‘ Constructor Options πŸ‘‘

  • Handler β€” the PerlSAX handler (or filter) that will receive the PerlSAX events from this filter. it's the person who gets the messages after the glow-up πŸ’Œ
  • Tab (Default: one space) β€” the number of spaces per indent level for elements etc. in document content.
  • Newline (Default: "\x0A") β€” the newline to use when re-indenting. the default is the internal newline used by XML::Parser, XML::DOM etc., and should be fine when used in combination with XML::Handler::Composer. 🫢

🧚 $self->indent_children ($start_element_event) 🧚

this method determines whether children of a certain element may be reformatted. the default implementation checks the PreserveWS parameter of the specified start_element event and returns 0 if it is set, or MAYBE otherwise. the value MAYBE (2) means further investigation is needed β€” like, by examining the element contents, iykyk. a value of 1 means yes, indent the child nodes, no further investigation is needed. periodt. βœ…

πŸ“ NOTE: the PreserveWS parameter is set by the parent class, XML::Filter::DetectWS, when the element or one of its ancestors has the attribute xml:space="preserve".

override this method to tweak the behavior of this class. it's your house, decorate it πŸ’…


🌈 $self->indent_element ($start_element_event, $parent_says_indent) 🌈

this method determines whether a certain element may be re-indented. the default implementation returns the value of the $parent_says_indent parameter, which was set to the value returned by indent_children for the parent element. in other words: the element will be re-indented if the parent element allows it. mom said yes so you can go πŸ₯³

override this method to tweak the behavior of this class. honestly, the author isn't sure how useful this hook is β€” "Please provide feedback!" they said, and we love a humble icon πŸ™ˆ


πŸ“ Current Implementation πŸ“

the current implementation puts all incoming Perl SAX events in a queue for further processing. when determining which nodes should be re-indented, it sometimes needs information from previous events, hence the use of the queue. it's a whole to-do list situation πŸ“‹

the parameter (Compress => 1) is added to matching start_element and end_element events with no events in between. this indicates to an XML printer that a compressed notation can be used, e.g. <foo/>. short and sweet, we love her πŸŽ€

if an element allows reformatting of its contents (xml:space="preserve" was not active and indent_children returned MAYBE), the element contents will be reformatted β€” unless it only has one child node and that child is a regular text node (a characters event). in that case, the element will be printed as <foo>text contents</foo>. one text baby = keep it cozy, no breaking it up.

if you want element nodes with just one text child to be reindented as well, simply override indent_children to return 1 instead of MAYBE (2). easy.

this behavior may be changed or extended in the future. things evolve, we grow, we glow 🌱✨


πŸ’€ CAVEATS πŸ’€

  • this code is highly experimental! it has not been tested well and the API may change. it's giving "we're so back" and also "it's so over" at the same time 😭
  • the code that detects blocks of whitespace at potential indent positions may need some work. it's a work in progress, be nice to her 🫢

πŸ’œ AUTHOR πŸ’œ

Original Author is Enno Derksen.

send bug reports, hints, tips, suggestions to T.J. Mather at <tjmather@tjmather.com>. slide into the inbox, they asked for it fr fr πŸ“¬


and that's the whole page bestie, tysm for reading πŸ’– this was perl v5.32.0, dated 2021-01-06, straight from the User Contributed Perl Documentation β€” slay, mwah, periodt πŸ’…πŸŽ€πŸŒˆ

πŸ“¦ where this page comes from