Skip to content

Instantly share code, notes, and snippets.

@timjb
Created August 15, 2010 12:43
Show Gist options
  • Save timjb/525456 to your computer and use it in GitHub Desktop.
Save timjb/525456 to your computer and use it in GitHub Desktop.
include Nanoc3::Helpers::LinkTo
# COPIED FROM http://github.com/ddfreyne/nanoc/blob/master/lib/nanoc3/base/compilation/compiler_dsl.rb
# Converts the given identifier, which can contain the '*' or '+'
# wildcard characters, matching zero or more resp. one or more
# characters, to a regex. For example, 'foo/*/bar' is transformed
# into /^foo\/(.*?)\/bar$/ and 'foo+' is transformed into /^foo(.+?)/.
def identifier_to_regex(identifier)
if identifier.is_a? String
# Add leading/trailing slashes if necessary
new_identifier = identifier.dup
new_identifier[/^/] = '/' if identifier[0,1] != '/'
new_identifier[/$/] = '/' unless [ '*', '/' ].include?(identifier[-1,1])
/^#{new_identifier.gsub('*', '(.*?)').gsub('+', '(.+?)')}$/
else
identifier
end
end
def menu(parent, options={})
if parent.children.empty?
''
else
children = parent.children
# exclude
if options[:exclude]
exclude_regex = identifier_to_regex options[:exclude]
children.reject! { |child| (child.identifier =~ exclude_regex) != nil }
end
# sort
if options[:sort]
children.sort! do |a, b|
a[options[:sort]].to_i <=> b[options[:sort]].to_i
end
end
# reverse
if options[:descending]
children.reverse!
end
childoptions = options.reject { | key, value | key == :display_parent }
list = children.inject('') do |memo, child|
memo + "<li>#{link_to_unless_current child[:title], child}#{menu child, childoptions}</li>"
end
# display_parent
if options[:display_parent]
list = "<li>#{link_to_unless_current parent[:title], parent}</li>" + list
end
"<ul>#{list}</ul>"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment