Skip to content

Instantly share code, notes, and snippets.

@akahigeg
Created September 21, 2011 12:36
Show Gist options
  • Save akahigeg/1231925 to your computer and use it in GitHub Desktop.
Save akahigeg/1231925 to your computer and use it in GitHub Desktop.
なんちゃってtoctree

toctree from /

<%= toctree :from => '/' %>

toctree from current item

<%= toctree %>

toctree include only specified directories

<%= toctree '/dir_a/', '/dir_b/' %>

except option

<%= toctree '/dir_a/', '/dir_b/', :except => ['dir_a/dir_x', 'dir_a/dir_y'] %>

maxdepth option

<%= toctree :maxdepth => 2 %>

module Nanoc3
module Helpers
module TocTree
def toctree(*args)
ids = args.select{|arg| arg.is_a?(String) }
ids = ['*'] if ids.empty?
opts = args.reject{|arg| arg.is_a?(String) }.first || {}
opts[:from] ||= @item.identifier
items = titled_items
.select{|item| item.identifier =~ /^#{ids.join('|')}/ }
.select{|item| !item.parent.nil? && item.parent.identifier == opts[:from] }
.sort{|i1, i2| i1.identifier <=> i2.identifier }
to_list(items, opts)
end
private
def titled_items
@items.reject{|item| item[:title].nil? }
end
def to_list(items, opts, depth = 0)
return "" if !opts[:maxdepth].nil? && opts[:maxdepth] <= depth
html = '<ul>'
items.each do |item|
next if opts[:except] && opts[:except].include?(item.identifier)
html += "<li>#{link_to(item[:title], item)}</li>"
# html += "<li>#{link_to(item.identifier, item)}</li>"
if not item.children.empty?
html += to_list(item.children, opts, depth + 1)
end
end
html += '</ul>'
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment