It depends how much futureproofness you want. I assume the code works as it is, but is becoming hard to make more changes. I suggest that you write out a table of all the possible inputs into the decision as to how to write the node out.
has parent || is marked comment || node tag || etc...
On the right of the table, put all the possible results:
indent with tabs || newline before || etc.
Then, write out all the variations on the rows:
has parent is marked comment node tag indent with tabs newline before ...
yesyesayes...
yesyesbyes...
...
Once you've done this, you should be able to see what the main factors are which decide the differences in output, and refactor accordingly. (You can also use this technique to model what your future changes will do.)

For example, if you find out that there are only 3 main output styles, then you can rewrite the subroutine to look at the inputs, and then call one of 3 subroutines (you could put them in a dispatch table in case you need more).

Or, if you think the decision is more complex, you might want to create objects to decide how to output the code. For example, you could create NodeWriter::HasParent to write out nodes with parents. Maybe table cell nodes are handled slightly different, so NodeWriter::HasParent::Td could inherit but override some methods. Then you can decide which object to create:

sub prepareOutput { my $self = shift; my ($node) = @_; my $writer = $self->create_nodewriter($node); $self->[OUTPUT] .= $writer->write_output($node); } sub create_nodewriter { my $self = shift; my ($node) = @_; $subtype = $node->parent? 'HasParent':'NoParent'; $tagtype = ucfirst $node->tag; $class = "NodeWriter::$subtype" . "::$tagtype"; return $class->new(); }

In short what I am suggesting is: "separate policy from mechanism".

A massive flamewar beneath your chosen depth has not been shown here

In reply to Re: Output of HTML tree built with TreeBuilder by dash2
in thread Output of HTML tree built with TreeBuilder by simon.proctor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.