Given that you already have the listing of directories (presumably as created by a command line like
find examples -type d), I think it's pointless to talk about using File::Find (which is certainly not an improvement over the unix "find" utility).
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:
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>
(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.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.