class Sigmund::Liquid::Tags::ChildrenNav

Constants

Syntax

Public Class Methods

new(tag_name, markup, tokens, context) click to toggle source
Calls superclass method
# File lib/sigmund/liquid/tags/children_nav.rb, line 8
def initialize(tag_name, markup, tokens, context)
  if markup =~ Syntax
    begin
      @options = { id: 'nav', depth: 1, class: '', active_class: 'active'}
      markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/"|'/, '') }
      @options[:exclude] = Regexp.new(@options[:exclude]) if @options[:exclude]
      
      if @options[:snippet]
        template = @options[:snippet].include?('{') ? @options[:snippet] : context[:site].snippets.where(slug: @options[:snippet] ).try(:first).try(:template)
        @options[:liquid_render] = ::Liquid::Template.parse(template) unless template.blank?
      end
    rescue
      nil
    end
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'children_nav ' - Valid syntax: children_nav <options>")
  end
  super
end

Public Instance Methods

render(context) click to toggle source
# File lib/sigmund/liquid/tags/children_nav.rb, line 28
def render(context)          
  @site = context.registers[:site]
  @page = context.registers[:page] #current page for active class
  page = get_page_by_slug()
  render_entry_children(context, page, 1)
end

Private Instance Methods

get_page_by_slug() click to toggle source

Determines root node for the list with the page slug

# File lib/sigmund/liquid/tags/children_nav.rb, line 38
def get_page_by_slug()
  return [] unless @options[:page_slug]
  page = @site.pages.where( slug: @options[:page_slug] ).first
  if page.nil?
    raise ::Liquid::SyntaxError.new("Slug '#{@options[:page_slug]}'' doesn't exists") 
  end
  page
end
include_page?(page) click to toggle source

Determines whether or not a page should be a part of the menu

# File lib/sigmund/liquid/tags/children_nav.rb, line 102
def include_page?(page)
  if !page.listed? || page.templatized? || !page.published?
    false
  elsif @options[:exclude]
    (page.fullpath =~ @options[:exclude]).nil?
  else
    true
  end
end
render_entry_children(context, page, depth) click to toggle source

Recursively creates a nested unordered list for the depth specified

# File lib/sigmund/liquid/tags/children_nav.rb, line 67
def render_entry_children(context, page, depth)
  output = %{}
  children = page.children_with_minimal_attributes.reject { |c| !include_page?(c) }
  i = 0
  if children.present?
    children.each do |c, page|
      css = []
      css << 'first' if children.first == c
      css << 'last'  if children.last  == c
      i = i + 1
      output << '<ul>' if children.first == c
      output << render_entry_link(context, c, css.join(' '), depth, children.first == c, children.last  == c, i)
      output << '</ul>' if children.last == c
    end
  end
  output
end
render_entry_link(context, page, css, depth, is_first, is_last, index ) click to toggle source

Returns a list element, a link to the page and its children

# File lib/sigmund/liquid/tags/children_nav.rb, line 48
def render_entry_link(context, page, css, depth, is_first, is_last, index )
  selected = @page.fullpath =~ /^#{page.fullpath}(\/.*)?$/ ? " #{@options[:active_class]}" : ''

  href = File.join('/', @site.localized_page_fullpath(page))
  title = render_title(context, page, href, selected, is_first, is_last, index)
  
  if @options[:liquid_render]
    return title
  end

  output  = %{<li id="#{page.slug.to_s.dasherize}-link" class="link#{selected} #{css}">}
  output << %{<a href="#{href}">#{title}</a>}
  output << render_entry_children(context, page, depth.succ) if (depth.succ <= @options[:depth].to_i)
  output << %{</li>}

  output.strip
end
render_title(context, page, href, selected, is_first, is_last, index) click to toggle source
# File lib/sigmund/liquid/tags/children_nav.rb, line 85
def render_title(context, page, href, selected, is_first, is_last, index)
  if @options[:liquid_render]
    context.stack do
      context['page'] = page
      context['is_first'] = is_first
      context['is_last'] = is_last
      context['href'] = href
      context['selected'] = selected
      context['index'] = index
      @options[:liquid_render].render(context)
    end
  else
    page.title
  end
end