Link to home
Start Free TrialLog in
Avatar of SonicVoom
SonicVoomFlag for United States of America

asked on

generate html from directory and file structure in bash or javascript

I have a directory containing several other directories that contain files. I need to turn these into HTML.

How could I wrap each sub-folder in a <UL> and each filename in <li> ?

I'd like this to be in bash or javascript.
Avatar of dexion432
dexion432

in bash yes, in js you need to pass the folder structure to the script
Avatar of SonicVoom

ASKER

Bash then. or PHP...

If you can get the structure into JSON format, as in the root variable below, then it should be fairly simple:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

var root = [
  {
    name: "DiectoryA"
  }, 
  {
    name: "DiectoryB",
    children: [
      { name: "FileA" },
      {
        name: "DiectoryB2",
        children: [
          { name: "FileA-B2" },
          { name: "FileB-B2" }
        ]
      },
      { name: "FileC" }
    ],
  }, 
  {
    name: "DiectoryC"
  }
];

jQuery(document).ready( function() {

  $("#container").append( render( root ) );
  
});

function render( parent ) {
  var result = $( "<ul/>" );
  for( var directory in parent ) {
    $(result).append( $( "<li/>" ).text( parent[directory].name ).append( render( parent[directory].children ) ) );
  };
  return result;
}

</script>
</head>
<body>

<div id="container"></div>

</body>
</html>

Open in new window

I don't know how to read it all into JSON. It's more the reading part that I'm confused by than the formatting.
Avatar of leakim971
ASKER CERTIFIED SOLUTION
Avatar of SonicVoom
SonicVoom
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No submitted solution solved my question, this was the result of my googling.