Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
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

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?
[Discipulus]: passed choroba but with one hole more
[ambrus]: Corion, moritz: haha. I moved to different rooms like eight times in the five year's working here so far.
[ambrus]: Discipulus: ouch
[Discipulus]: eh, at the end i cannot compete with brother choroba, i just profited he is at lunch
[pra]: What is the best way to determine if a given string is a valid color?

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (13)
As of 2017-10-17 11:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (226 votes). Check out past polls.

    Notices?