2010-07-06 00:05:47 +00:00
|
|
|
module Locomotive
|
2010-07-23 20:09:54 +00:00
|
|
|
module Liquid
|
|
|
|
module Tags
|
2010-07-06 00:05:47 +00:00
|
|
|
# Display the children pages of the site or the current page. If not precised, nav is applied on the current page.
|
|
|
|
# The html output is based on the ul/li tags.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# {% nav site %} => <ul class="nav"><li class="on"><a href="/features">Features</a></li></ul>
|
|
|
|
#
|
|
|
|
class Nav < ::Liquid::Tag
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
Syntax = /(#{::Liquid::Expression}+)?/
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-08-20 00:31:01 +00:00
|
|
|
def initialize(tag_name, markup, tokens, context)
|
2010-07-06 00:05:47 +00:00
|
|
|
if markup =~ Syntax
|
|
|
|
@site_or_page = $1 || 'page'
|
2010-07-13 00:46:17 +00:00
|
|
|
@options = {}
|
|
|
|
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value }
|
2010-07-06 00:05:47 +00:00
|
|
|
else
|
2010-07-13 00:46:17 +00:00
|
|
|
raise ::Liquid::SyntaxError.new("Syntax Error in 'nav' - Valid syntax: nav <page|site> <options>")
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
super
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
def render(context)
|
|
|
|
@current_page = context.registers[:page]
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
source = context.registers[@site_or_page.to_sym]
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-20 10:21:18 +00:00
|
|
|
# puts "#{@site_or_page.to_sym} / source = #{source.inspect}"
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
if source.respond_to?(:name) # site ?
|
2010-07-16 20:36:07 +00:00
|
|
|
source = source.pages.index.first # start from home page
|
2010-07-06 00:05:47 +00:00
|
|
|
else
|
2010-07-16 20:36:07 +00:00
|
|
|
source = source.parent || source
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
output = %{<ul id="nav">}
|
|
|
|
output += source.children.map { |p| render_child_link(p) }.join("\n")
|
|
|
|
output += %{</ul>}
|
|
|
|
output
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
private
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
def render_child_link(page)
|
|
|
|
selected = @current_page._id == page._id ? ' on' : ''
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
icon = @options[:icon] ? '<span></span>' : ''
|
|
|
|
label = %{#{icon if @options[:icon] != 'after' }#{page.title}#{icon if @options[:icon] == 'after' }}
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
%{
|
|
|
|
<li id="#{page.slug.dasherize}" class="link#{selected}">
|
2010-07-13 00:46:17 +00:00
|
|
|
<a href="/#{page.fullpath}">#{label}</a>
|
2010-07-06 00:05:47 +00:00
|
|
|
</li>
|
|
|
|
}.strip
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
::Liquid::Template.register_tag('nav', Nav)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|