Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

tabs to html list

by Anonymous Monk
on Sep 09, 2010 at 14:07 UTC ( #859515=CUFP: 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

Comment on tabs to html list
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://859515]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-09-21 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (172 votes), past polls