Class: Smithy::Liquid::Tags::Nav
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Smithy::Liquid::Tags::Nav
- Defined in:
- lib/smithy/liquid/tags/nav.rb
Constant Summary collapse
- Syntax =
/(#{::Liquid::Expression}+)?/
Instance Method Summary collapse
-
#initialize(tag_name, markup, tokens) ⇒ Nav
constructor
nav % is equivalent to nav site, depth: 1, id: ‘nav’, class: ”, wrapper: true, active_class: ‘on’, include_root: ‘true’ nav site|site-section|page|section %.
- #parse(tokens) ⇒ Object
- #render(context) ⇒ Object
- #render_children(parent, depth) ⇒ Object
- #render_list_item(item, depth) ⇒ Object
- #render_list_items(parent, depth = 1) ⇒ Object
- #render_wrapper(list_items, id = nil) ⇒ Object
- #root_node ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ Nav
nav % is equivalent to nav site, depth: 1, id: ‘nav’, class: ”, wrapper: true, active_class: ‘on’, include_root: ‘true’ nav site|site-section|page|section %
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/smithy/liquid/tags/nav.rb', line 10 def initialize(tag_name, markup, tokens) @options = { :id => 'nav', :depth => 1, :class => '', :active_class => 'on', :include_root => false } if markup =~ Syntax @source = ($1 || 'site').gsub(/"|'/, '') markup.scan(::Liquid::TagAttributes) do |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') end @options[:active_nested_class] = 'in' @options[:depth] = @options[:depth].to_i @options[:depth] = 100 if @options[:depth] == 0 @options[:wrapper] = @options[:wrapper] == "false" ? false : true @options[:include_root] = @options[:include_root] == "true" ? true : false @options[:root] = @options[:root_id].present? ? Smithy::Page.find(@options[:root_id]) : Smithy::Page.root else raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav <site|page|section> <options>") end super end |
Instance Method Details
#parse(tokens) ⇒ Object
29 30 31 |
# File 'lib/smithy/liquid/tags/nav.rb', line 29 def parse(tokens) @tokens = tokens end |
#render(context) ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/smithy/liquid/tags/nav.rb', line 33 def render(context) @site = context.registers[:site] @page = context.registers[:page] @controller = context.registers[:controller] list_items = render_list_items(root_node) @options[:wrapper] ? render_wrapper(list_items, @options[:id]) : list_items end |
#render_children(parent, depth) ⇒ Object
41 42 43 44 45 |
# File 'lib/smithy/liquid/tags/nav.rb', line 41 def render_children(parent, depth) list_items = render_list_items(parent, depth) return unless list_items.present? render_wrapper(list_items) end |
#render_list_item(item, depth) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/smithy/liquid/tags/nav.rb', line 47 def render_list_item(item, depth) item_id = "#{@options[:id]}-#{item.permalink}" href = item.url label = item.title css_class = " class=\"#{@options[:active_class]}\"" if (@page && @page.id == item.id) || (@controller && [item.path, item.external_link].include?(@controller.request.path)) css_class ||= " class=\"#{@options[:active_nested_class]}\"" if @page && @page.ancestors.include?(item) %Q{#{" " * depth}<li id="#{item_id}"#{css_class}><a href="#{href}" id="#{item_id}-link">#{label}</a>#{render_children(item, depth.succ)}</li>} end |
#render_list_items(parent, depth = 1) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/smithy/liquid/tags/nav.rb', line 56 def render_list_items(parent, depth=1) return unless write_child_list_items?(parent, depth) items = [] items << render_list_item(parent, depth) if depth == 1 && @options[:include_root] parent.children..inject(items) do |items, item| items << render_list_item(item, depth) end.join("\n") end |
#render_wrapper(list_items, id = nil) ⇒ Object
65 66 67 68 69 |
# File 'lib/smithy/liquid/tags/nav.rb', line 65 def render_wrapper(list_items, id = nil) list_id = id.present? ? " id=\"#{id}\"" : '' list_class = @options[:class].present? ? " class=\"#{@options[:class]}\"" : '' %Q{<ul#{list_id}#{list_class}>\n#{list_items}\n</ul>} end |
#root_node ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/smithy/liquid/tags/nav.rb', line 71 def root_node case @source when 'site', 'site-section' @options[:root] when 'page' @page when 'section' @page == @options[:root] ? @page : section_page end end |