Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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.)

In reply to Re: Directory Tree Structure by graff
in thread Directory Tree Structure by rupesh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-25 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found