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 byXML::Parser,XML::DOMetc., and should be fine when used in combination withXML::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: thePreserveWSparameter is set by the parent class,XML::Filter::DetectWS, when the element or one of its ancestors has the attributexml: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
- debian bookworm: libxml-filter-reindent-perl 0.03-8.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-filter-reindent-perl 0.03-8.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libxml-filter-reindent-perl 0.03-8.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-filter-reindent-perl 0.03-8.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-filter-reindent-perl 0.03-8.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-filter-reindent-perl 0.03-8.1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms