Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
converts 4-space (or tab) indented text into a html list of lists
#!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; use HTML::TreeBuilder; Main(@ARGV); exit(0); sub Main { my $list = Blahblah( \*DATA ); { my $tree = HTML::TreeBuilder->new(); $tree->eof; #~ $tree->look_down(qw!_tag body!)->push_content( @$list ); $tree->look_down(qw!_tag body!)->push_content($list); print $tree->as_HTML( '><&' => "\t" ), "\n"; # tab indent $tree->delete; undef $tree; } #~ warn Dumper($list), "\n"; } ## end sub Main sub Blahblah { my $fh = shift; my $root_ref = ['ul']; #~ my $root_ref = [ ]; my $curr_ref = $root_ref; my @prev_ref; my $curr = 0; my $last = 0; while ( readline $fh ) { next if !/\w/; # skip blank lines chomp; $last = $curr; $curr = /^((?: |\t)+)/ ? length($1) / 4 : 0; if ( $curr > $last ) { my $new = ['ul']; push @{ ref $curr_ref->[-1] ? $curr_ref->[-1] : $curr_ref }, $new; push @prev_ref, $curr_ref; $curr_ref = $new; } elsif ( $curr < $last ) { while ( $last != $curr ) { $last--; $curr_ref = pop @prev_ref; } } ## end elsif ( $curr < $last ) s/^\s+//; push @$curr_ref, [ 'li', $_ ]; } ## end while ( readline $fh ) return $root_ref; } ## end sub Blahblah __DATA__ a 1 2 3 b 1 2 a b 1 2 a b c 3 c 3 c d 7 7a 7b 7b1 7b2 7b2a 7b2b 7b2c 7b3 7c 8
output
  • a
    • 1
    • 2
    • 3
  • b
    • 1
    • 2
      • a
      • b
        • 1
        • 2
          • a
          • b
          • c
        • 3
      • c
    • 3
  • c
  • d
    • 7
      • 7a
      • 7b
        • 7b1
        • 7b2
          • 7b2a
          • 7b2b
          • 7b2c
        • 7b3
      • 7c
    • 8
not the first time i wrote something like this, hopefully I won't reinvent again

In reply to tabs to html list by Anonymous Monk

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others taking refuge in the Monastery: (7)
    As of 2014-07-24 23:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (167 votes), past polls