简体   繁体   中英

construct file tree from path components

I have an array of file path components like this:

[ ['some', 'dir', 'file.txt'],
  ['other', 'folder', 'here.txt'],
  ['this', 'one', 'is', 'deeper', 'file.txt'],
  ['some', 'dir', 'second.txt'
]

So the array contains arrays each consisting of path components to a file. The last element in an inner array is always the file itself, with the preceding elements being directories leading to the file.

What I'm trying to figure out is how to transform the above data so that I can easily generate a file tree with it using <ul> and <li> tags such that folders are nested within each other and files within the same folder show up together. All sorted alphabetically.

From the above I would like to generate the following. The file <li> themselves have to be links to the path to that file:

<ul>
  <li>some/
    <ul>
      <li>dir/
        <ul>
          <li><a href="some/dir/file.txt">file.txt</a></li>
          <li><a href="some/dir/second.txt">second.txt</a></li>
        </ul>
      </li>
    </ul>
  </li>
  <li>other/
    <ul>
      <li>folder/
        <ul>
          <li><a href="other/folder/here.txt">here.txt<a/></li>
        </ul>
      </li>
    </ul>
  </li>
  <li>this/
    <ul>
      <li>one/
        <ul>
          <li>is/
            <ul>
              <li>deeper/
                <ul>
                  <li><a href="this/one/is/deeper/file.txt">file.txt</a></li>
                </ul>
              </li>
            </ul>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

Thanks, I'd appreciate any ideas.

Rough outline; as simple as possible (ie, no tricks to keep things simple :)

require 'pp'

dir = {}

files = [
  ['some', 'dir', 'file.txt'],
  ['other', 'folder', 'here.txt'],
  ['this', 'one', 'is', 'deeper', 'file.txt'],
  ['some', 'dir', 'second.txt']
]

def merge_paths(h, paths)
  top = paths[0]

  if paths.size == 1
    h[top] = top
  else
    h[top] ||= {}
    merge_paths h[top], paths[1..-1]
  end
end

files.each do |paths|
  merge_paths dir, paths
end

pp dir

Outputs:

{"some"=>{"dir"=>{"file.txt"=>"file.txt", "second.txt"=>"second.txt"}},
 "other"=>{"folder"=>{"here.txt"=>"here.txt"}},
 "this"=>{"one"=>{"is"=>{"deeper"=>{"file.txt"=>"file.txt"}}}}}

Creating the lists is essentially the same process; recurse over hash keys. You're at the last level when a hash value isn't another hash. You might want to also sort by name and/or type, eg, put directories (key values that are hashes) first, and so on.

There are a number of games you can play with this, including turning it into just a few lines of code, combined with gems like deep_merge to reduce the amount of busy-work you have to do manually.

This also doesn't do any "sanity checking" to make sure the data isn't pathological, eg, you could construct an array that would turn a filename into a directory, wipe out a directory with a filename, and so on--an exercise left for the reader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM