Module: Nanoc::Toolbox::Helpers::Navigation
- Includes:
- Helpers::Breadcrumbs, Helpers::LinkTo, HtmlTag
- Defined in:
- lib/nanoc/toolbox/helpers/navigation.rb
Overview
NANOC Helper for the Navigation related stuff.
This module contains functions for generating navigation menus for your pages, like navigation menu, breadcrumbs or a table of content for a given Item
Instance Method Summary collapse
-
#breadcrumb_for(identifier, options = {}) ⇒ String
Generate a Breadcrumb for a given item.
-
#navigation_for(identifier, options = {}) ⇒ String
Generate a navigation menu for a given item.
-
#render_menu(items, options = {}) ⇒ String
Render a Hash to a HTML List by default.
-
#toc_for(item_rep, options = {}) ⇒ String
Generate a Table of Content for a given item.
Methods included from HtmlTag
#content_tag, #tag, #tag_options
Instance Method Details
#breadcrumb_for(identifier, options = {}) ⇒ String
Generate a Breadcrumb for a given item. The breadcrumbs, is starting with the root item and ending with the item itself.
Requires the Helper: Nanoc::Helpers::Breadcrumbs
78 79 80 81 82 83 84 85 |
# File 'lib/nanoc/toolbox/helpers/navigation.rb', line 78 def (identifier, ={}) [:collection_tag] ||= 'ul' [:collection_class] ||= 'breadcrumb' # Retreive the breadcrumbs trail and format them sections = (identifier) (sections, ) end |
#navigation_for(identifier, options = {}) ⇒ String
Generate a navigation menu for a given item. The menu will be generated form the identifier of the desired root element. The root itself will not be rendered. It generate the menu by parsing all the descendent of the passed item.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/nanoc/toolbox/helpers/navigation.rb', line 26 def (identifier, ={}) # Get root item for which we need to draw the navigation root = @items.find { |i| i.identifier == identifier } # Do not render if there is no child return nil unless root.children # Find all sections, and render them sections = find_item_tree(root, ) (sections, ) end |
#render_menu(items, options = {}) ⇒ String
Render a Hash to a HTML List by default
Hash structure should be construct like this:
Link: is an hash with the following key
- :title => The content of the link
- :link => The link
- :subsections => nil or an Array of Links
[{:title => 'Title', :link => 'http://example.com', :subsections => [{}, {}, ...]},{...}]
Results to an output like the following (by default):
<ul>
<li>
<a href="http://example.com">Title</a>
<ul>
<li><a href="">Title</a></li>
</ul>
</li>
<li><a href="http://example.com">Title</a></li>
<li><a href="http://example.com">Title</a></li>
</ul>
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/nanoc/toolbox/helpers/navigation.rb', line 120 def (items, ={}) [:depth] ||= 3 [:collection_tag] ||= 'ol' [:collection_class] ||= 'menu' [:item_tag] ||= 'li' [:title_tag] ||= 'h2' [:title] ||= nil [:separator] ||= '' # Parse the title and remove it from the options title = [:title] ? content_tag([:title_tag], [:title]) : '' .delete(:title_tag) .delete(:title) # Decrease the depth level [:depth] -= 1 = items.map do |item| # Render only if there is depth left if [:depth].to_i > 0 && item[:subsections] output = (item[:subsections], ) [:depth] += 1 # Increase the depth level after the call of navigation_for end output ||= "" content_tag([:item_tag], link_to_unless_current(item[:title], item[:link]) + [:separator] + output) end.join() title + content_tag([:collection_tag], , :class => [:collection_class]) unless .strip.empty? end |
#toc_for(item_rep, options = {}) ⇒ String
Generate a Table of Content for a given item. The toc will be generated form the item content. The parsing is done with Nokogiri through XPath.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/nanoc/toolbox/helpers/navigation.rb', line 49 def toc_for(item_rep, ={}) require 'nokogiri' item_rep = item_rep.rep_named(:default) if item_rep.is_a? Nanoc::Item [:path] ||= 'div[@class="section"]' # Retreive the parsed content and init nokogiri compiled_content = item_rep.instance_eval { @content[:pre] } doc = Nokogiri::HTML(compiled_content) doc_root = doc.xpath('/html/body').first return "" if doc_root.nil? # Find all sections, and render them sections = find_toc_sections(doc_root, [:path]) (sections, ) || "" end |