http://www.perlmonks.org?node_id=859515

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