diff options
author | Connor Shea <connor.james.shea@gmail.com> | 2016-10-27 08:04:36 +0300 |
---|---|---|
committer | Connor Shea <connor.james.shea@gmail.com> | 2016-10-27 08:04:36 +0300 |
commit | 8945965d5c1428011c366d2db929e0b717def722 (patch) | |
tree | c12b0029cc6935b137bcbe1c71f23f8114e15481 /lib | |
parent | 280cdc29c34beece0dadb41a462baf2b74c29789 (diff) |
Simplify helper, style improvements.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helpers/child_parent_better.rb | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/lib/helpers/child_parent_better.rb b/lib/helpers/child_parent_better.rb index 0a4ed86e..351a6f24 100644 --- a/lib/helpers/child_parent_better.rb +++ b/lib/helpers/child_parent_better.rb @@ -1,27 +1,25 @@ module Nanoc::Helpers module ChildParentBetter - def parent_path_array(item) + # Returns an array of ancestor pages for the given page. + def ancestor_path_array(item) parent_array = Array.new current_item = item - until (parent_path(current_item).nil?) do - if (parent_array.length == 0) - parent = parent_path(item) - parent_array.push(parent) if parent - else - parent = parent_path(parent_array.last) - current_item = parent_array.last - parent_array.push(parent) if parent - end + # Until the current item has no parent, keep running. + until (get_nearest_parent(current_item.identifier.to_s).nil?) do + # Set the current item to the last item in the array + # unless this is the first item. + current_item = parent_array.last unless parent_array.empty? + # Get the nearest parent of the current item. + parent = get_nearest_parent(current_item.identifier.to_s) + # Push the parent of the current item into the array. + parent_array.push(parent) if parent end - parent_array - end - def parent_path(item) - parent = get_nearest_parent(item.identifier.to_s) - parent + parent_array end - # Recursion! + # A recursive function which returns the nearest parent item for the + # given path. def get_nearest_parent(item_identifier) if (item_identifier.nil? || (item_identifier.to_s.end_with?('README.md') && item_identifier.to_s.split('/').length == 3)) return |