As I understand it, the question is how to parse that listing into an appropriate html structure to render it as a nested set of unordered lists. This should be fairly simple, given that the input list is already sorted as shown in your example:
(updated to fix one of the comments in the code; also, looking more closely at the OP, I realize that I left out some of the desired formatting in the LI elements: having an href attribute containing the full path, and using just the last component of the path as the displayed text. I'll leave that as an exercise... it should be easy enough to work out.)use strict; # let's assume you read the list from stdin: my @paths = <>; chomp @paths; s/:$// for @paths; # don't want the punctuation my $prev_path = shift @paths; my $prev_depth = ( $prev_path =~ tr{/}{/} ); # count /'s my $indent = 1; print "<UL>\n <LI> $prev_path: </LI>\n"; for ( @paths ) { my $depth_now = ( tr{/}{/} ); if ( index( $_, "$prev_path/" ) == 0 ) { # $prev_path is contained within this one, so indent print ' ' x $indent, "<UL>\n"; $indent++; } elsif ( $depth_now < $prev_depth ) { # need to back off the indentation while ( $depth_now < $prev_depth ) { $indent--; print ' ' x $indent, "</UL>\n"; $prev_depth--; } } print ' ' x $indent, "<LI> $_: </LI>\n"; # put ":" back in $prev_path = $_; $prev_depth = $depth_now; } while ( $indent ) { $indent--; print ' ' x $indent, "</UL>\n"; } __OUTPUT__ <UL> <LI> examples/html: </LI> <UL> <LI> examples/html/bars: </LI> <LI> examples/html/headers: </LI> <LI> examples/html/links: </LI> <LI> examples/html/lists: </LI> <LI> examples/html/menus: </LI> <LI> examples/html/rgb: </LI> <LI> examples/html/tables: </LI> </UL> <LI> examples/ps: </LI> <UL> <LI> examples/ps/marks: </LI> </UL> <LI> examples/splash: </LI> <UL> <LI> examples/splash/dropbox: </LI> <LI> examples/splash/frame: </LI> <LI> examples/splash/hair: </LI> <LI> examples/splash/icon: </LI> <LI> examples/splash/menu: </LI> <LI> examples/splash/menubar: </LI> </UL> </UL>
In reply to Re: Directory Tree Structure
by graff
in thread Directory Tree Structure
by rupesh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |