Module: Middleman::Sitemap::Extensions::Traversal
- Included in:
- Resource
- Defined in:
- middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb
Instance Method Summary collapse
-
#children ⇒ Array<Middleman::Sitemap::Resource>
This resource's child resources.
-
#directory_index? ⇒ Boolean
Whether this resource is either a directory index, or has the same name as an existing directory in the source.
-
#eponymous_directory? ⇒ Boolean
Whether the resource has the same name as a directory in the source (e.g., if the resource is named 'gallery.html' and a path exists named 'gallery/', this would return true).
-
#eponymous_directory_path ⇒ String
The path for this resource if it were a directory, and not a file (e.g., for 'gallery.html' this would return 'gallery/').
-
#parent(_recurse = false) ⇒ Middleman::Sitemap::Resource?
This resource's parent resource.
- #parent_helper(child_path, max_recursion) ⇒ Object
-
#siblings ⇒ Array<Middleman::Sitemap::Resource>
This resource's sibling resources.
- #traversal_root ⇒ Object
Instance Method Details
#children ⇒ Array<Middleman::Sitemap::Resource>
This resource's child resources
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb', line 94 def children return [] unless directory_index? base_path = if eponymous_directory? eponymous_directory_path else path.sub(@app.config[:index_file].to_s, '') end prefix = /^#{base_path.sub("/", "\\/")}/ @store.resources.select do |sub_resource| if sub_resource.path == path || sub_resource.path !~ prefix false else inner_path = sub_resource.path.sub(prefix, '') parts = inner_path.split('/') case parts.length when 1 true when 2 parts.last == @app.config[:index_file] else false end end end end |
#directory_index? ⇒ Boolean
Whether this resource is either a directory index, or has the same name as an existing directory in the source
133 134 135 |
# File 'middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb', line 133 def directory_index? path.include?(@app.config[:index_file]) || path =~ %r{/$} || eponymous_directory? end |
#eponymous_directory? ⇒ Boolean
Whether the resource has the same name as a directory in the source (e.g., if the resource is named 'gallery.html' and a path exists named 'gallery/', this would return true)
140 141 142 143 144 145 146 |
# File 'middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb', line 140 def eponymous_directory? return true if !path.end_with?("/#{@app.config[:index_file]}") && destination_path.end_with?("/#{@app.config[:index_file]}") @app.files.by_type(:source).watchers.any? do |source| (source.directory + Pathname(eponymous_directory_path)).directory? end end |
#eponymous_directory_path ⇒ String
The path for this resource if it were a directory, and not a file (e.g., for 'gallery.html' this would return 'gallery/')
151 152 153 |
# File 'middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb', line 151 def eponymous_directory_path "#{path.sub(ext, '/').sub(%r{/$}, '')}/" end |
#parent(_recurse = false) ⇒ Middleman::Sitemap::Resource?
This resource's parent resource
19 20 21 22 |
# File 'middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb', line 19 def parent(_recurse = false) max_recursion = @app.config[:max_traversal_recursion] || 99 parent_helper(path, max_recursion) end |
#parent_helper(child_path, max_recursion) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb', line 24 def parent_helper(child_path, max_recursion) # What is the configured format for index pages. index_file = @app.config[:index_file] parts = child_path.split('/') # Reduce the path by the current page to get the parent level path. current_page = parts.pop # Does the current page has the name of an index file? is_index = current_page == index_file # Is the `current_page` in the traversal root? # Note: `traversal_root` is `/` for non localized pages and `/[lang]/` for # localized pages. at_traversal_root = !(child_path =~ /^#{traversal_root}#{current_page}$/).nil? # Check that we have any path parts left after the pop because if we # don't, `current_page` is either root or another file under root. # Also, if we are `at_traversal_root`, we consider this root. if parts.empty? || at_traversal_root # If this `is_index`, the `current_page` is root and there is no parent. return nil if is_index # `current_page` must be a page under root, let's return the root # index page of the `traversal_root` (`/` or `/[lang]/`). return @store.by_path("#{traversal_root}#{index_file}") end # Get the index file for the parent path parts, e.g.: `/blog/index.html` # for `/blog/`. index_by_parts = proc do |subparts| found = @store.by_destination_path("#{subparts.join('/')}/#{index_file}") return found unless found.nil? end # Get a file that has the name of the parent path parts e.g.: # `/blog.html` for `/blog/`. file_by_parts = proc do |subparts| test_expr = Regexp.escape(subparts.join('/')) # eponymous reverse-lookup found = @store.resources.find do |candidate| candidate.path =~ %r{^#{test_expr}(?:\.[a-zA-Z0-9]+|\/)$} end return found unless found.nil? end # Try to find a file matching the parent path name and return it. # E.g. `parts == ['en', 'blog']`, we try to find: `/en/blog.html` file_by_parts.call(parts) # Try to find an non-localized parent instead if `traversal_root` # indicates the path is localized and there are still more parts # remaining, and return it. # E.g. `parts == ['en', 'blog']`, we try to find: `/blog.html` file_by_parts.call(parts[1..-1]) if traversal_root != '/' && parts.length > 1 # Now let's drop the last part of the path to try to find an index # file in the path above `current_page`'s path and return it. # E.g. `parts == ['en', 'blog']`, we try to find: `/en/index.html` parts.pop if is_index index_by_parts.call(parts) # Lastly, check for an non-localized index index file in the path # above `current_page`'s path and return it. # E.g. `parts == ['en', 'blog']`, we try to find: `/index.html` index_by_parts.call(parts[1..-1] || '') if traversal_root == "#{parts.first}/" return parent_helper(parts.join('/'), max_recursion - 1) if !parts.empty? && max_recursion.positive? nil end |
#siblings ⇒ Array<Middleman::Sitemap::Resource>
This resource's sibling resources
125 126 127 128 129 |
# File 'middleman-core/lib/middleman-core/sitemap/extensions/traversal.rb', line 125 def siblings return [] unless parent parent.children.reject { |p| p == self } end |