Created
July 24, 2011 15:29
-
-
Save fracai/1102728 to your computer and use it in GitHub Desktop.
nanoc snippet for calculating "true" page parents
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
def output_parent(page) | |
nil == page and return nil | |
page[:x_parent] and return page[:x_parent] | |
(page.identifier == '/' or page.binary? or page[:is_hidden]) and return page[:x_parent] = 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 page[:x_parent] = i | |
end | |
end | |
raise "error finding parent" # should never reach this point | |
end | |
def output_children(page) | |
nil == page and return [] | |
page[:x_children] and return page[:x_children] | |
page.binary? and return page[:x_children] = nil | |
return page[:x_children] = @items.select { |i| output_parent(i) == page } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment