Skip to content

Instantly share code, notes, and snippets.

@garethrees
Created March 26, 2012 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garethrees/2204347 to your computer and use it in GitHub Desktop.
Save garethrees/2204347 to your computer and use it in GitHub Desktop.
How I got to automated html nav
require "pathname"
require 'set'
# @base_path = Pathname.new(File.dirname(__FILE__))
# def collect_paths(path)
# result = Set.new
# result.add?(path)
# puts "entering method"
# paths = []
# path.children.each do |p|
# puts "entering submethod"
# paths << p
# if p.directory?
# puts "FOUND A DIR #{p}"
# result.add?(path)
# else
# puts "stop! #{p}"
# end
# collect_paths p if p.directory?
# end
# return result
# end
#
# nav_items = collect_paths(@base_path).inspect
#
# nav_items.each do |nav|
# puts nav
# end
# collected.each do |c|
# puts c.to_s
# end
#
# paths = Dir.glob("/Users/gareth/Code/docs/public/contents/**/*").select { |f| File.directory?(f) }.map{ |path| path.split '/' }.inject({}) { |acc, path| path.inject(acc) do |acc2,dir|
# acc2[dir] ||= {} # and pass it as new current node
# end
# acc
# }
# require 'find'
#
#
# Find.find("/Users/gareth/Code/docs/public/contents/") do |path|
# if FileTest.directory?(path)
# if File.basename(path)[0] == ?.
# Find.prune # Don't look any further into this directory.
# else
# next
# end
# else
# total_size += FileTest.size(path)
# end
# end
# Pathname.glob("/Users/gareth/Code/docs/public/contents/") do |path|
# dirs = path.children.group_by { |c| c.parent }
#
# dirs.each do |d|
# first.children.group_by { |c| c.parent }
# end
# end
# root_path = Pathname.new(File.dirname(__FILE__))
#
#
# def recursive_dirs(root_path)
# Pathname.glob(root_path) do |path|
# return path.children.group_by { |c| c.parent }
# end
# end
#
# nav = recursive_dirs(root_path)
#
# puts nav.inspect
#
#
htmlfiles = File.join("**", "*.html")
nav = Pathname.glob(htmlfiles).group_by { |p| p.parent }
# puts nav.inspect
nav.each do |key, value|
puts key
value.each do |v|
puts "--> #{v}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment