Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello BrowserUK.

I know this is not terminal one but html output is handy for me.

#!/usr/bin/perl #HTML version use strict;use warnings; use ArrayRef2HTMLTree; my $t=[ [ [[[[["a", "b"], "c"], ["d", "e"]], [[["f", "g"], "h"], [["i", + "j"], ["k", ["l", "m"]]]]], ["n", [[["o", "p"], "q"], ["r", "s"]]]], ["t", ["u +", "v"]] ], [["w", ["x", "y"]], "z"] ]; my $ht = ArrayRef2HTMLTree->new(font_size=>'8pt',line_height=>'10px'); print $ht->to_html($t);
And my package.
# package ArrayRef2HTMLTree; use strict; use warnings; our $HTML; sub new{ my $class=shift; my %args=@_; $args{line_height}='10px' if(! exists $args{line_height}); $args{font_size}='8pt' if(! exists $args{font_size}); return bless \%args ,$class; } sub to_html{ my $self=shift; my $t=shift; my $tree=join('', '<ul class="tree">' , "\n" , traverse($t,0) , "\ +n", '</ul>', "\n"); my $html=$HTML; $html =~ s/#TREE#/$tree/s; $html =~ s/#LINE-HEIGHT#/$self->{line_height}/s; $html =~ s/#FONT-SIZE#/$self->{font_size}/s; return $html; } sub traverse { my ($t,$depth)=@_; my $tab="\t"x$depth; if (ref($t) eq 'ARRAY'){ return $tab , "<li>($depth)<ul>" , "\n" , (map{ traverse($_,$depth+1) } @$t) , $tab, "</ul></li>" , "\n"; } else { return "$tab<li>$t</li>\n"; } } $HTML=<<'HTML'; <html><head><style> body{ background-color: #FFFFFF; /*&#30333;*/ } ul.tree, ul.tree ul{ list-style-type: none; background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA +AAAKAQMAAABPHKYJAAAAA1BMVEWIiIhYZW6zAAAACXB padding:0; margin:0; } ul.tree ul{ margin-left: 10px; border: lightgreen 0px solid; } ul.tree li{ margin:0px; padding: 0 10px; border: green 0px solid; line-height: #LINE-HEIGHT#; background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgA +AAAUAQMAAACK1e4oAAAABlBMVEUAAwCIiIgd2JB2AAA color: #696969; font-size: #FONT-SIZE#; } ul.tree li:last-child{ background: #fff url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUg +AAAAgAAAAUAQMAAACK1e4oAAAABlBMVEUAAwCIiIgd2 } </style></head> <body> #TREE# </body> </html> HTML 1;

In reply to Re: Challenge: Dumping trees. by remiah
in thread Challenge: Dumping trees. by BrowserUk

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (4)
    As of 2017-02-21 00:46 GMT
    Find Nodes?
      Voting Booth?
      Before electricity was invented, what was the Electric Eel called?

      Results (304 votes). Check out past polls.