in reply to Directory Tree Structure
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>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Directory Tree Structure
by rupesh (Hermit) on Oct 21, 2005 at 13:13 UTC | |
|
Re^2: Directory Tree Structure
by Lady_Aleena (Priest) on Oct 03, 2009 at 21:46 UTC | |
by graff (Chancellor) on Oct 04, 2009 at 21:10 UTC | |
by almut (Canon) on Oct 05, 2009 at 03:11 UTC | |
by Anonymous Monk on Oct 05, 2009 at 12:38 UTC | |
by Anonymous Monk on Oct 05, 2009 at 06:06 UTC | |
by almut (Canon) on Oct 05, 2009 at 07:46 UTC | |
by Lady_Aleena (Priest) on Oct 05, 2009 at 02:12 UTC | |
by graff (Chancellor) on Oct 06, 2009 at 09:12 UTC | |
by Lady_Aleena (Priest) on Oct 19, 2009 at 19:42 UTC | |
| |
by Lady_Aleena (Priest) on Oct 25, 2009 at 20:21 UTC | |
| |
by Lady_Aleena (Priest) on Oct 22, 2009 at 18:20 UTC |