Created
August 8, 2011 22:05
-
-
Save fracai/1132866 to your computer and use it in GitHub Desktop.
nanoc helper for building path based relationshiips
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Nanoc3::Helpers | |
module Relationships | |
extend Nanoc3::Memoization | |
def output_parent(page) | |
nil == page and return nil | |
(page.identifier == '/' or page.binary? or page[:is_hidden]) and return nil | |
path = page.path.gsub(/[^\/]*$/,'') | |
@items.sort_by{ |i| i.path.count('/') }.reverse.each do |i| | |
(i == page or i.binary? or i[:is_hidden]) and next | |
if path =~ /^#{i.path.gsub(/[^\/]+$/,'')}/ | |
return i | |
end | |
end | |
raise "error finding parent" # should never reach this point | |
end | |
memoize :output_parent | |
def output_children(page) | |
nil == page and return [] | |
page.binary? and return nil | |
return @items.select { |i| output_parent(i) == page } | |
end | |
memoize :output_children | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment