Skip to content

Instantly share code, notes, and snippets.

@fracai
Created July 24, 2011 15:29
Show Gist options
  • Save fracai/1102728 to your computer and use it in GitHub Desktop.
Save fracai/1102728 to your computer and use it in GitHub Desktop.
nanoc snippet for calculating "true" page parents
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