require 'noumenon/template'
require 'uri'
require 'redcloth'
# Liquid extensions provided by Noumenon.
#
# @see http://github.com/Noumenon/noumenon/wiki/Template-Tags
module Noumenon::Template::CoreTags
# The core set of filters provided by Noumenon for use in templates.
#
# @api public
module Filters
# Assigns the data piped in to the named variable.
#
# @example
# {{ "Jon" | assign_to: "name" }}
# {{ name }}
#
# @api public
def assign_to(input, variable)
@context.scopes.last[variable] = input
''
end
# Loads the content item at the path provided and passes it on.
#
# If nothing was found then nothing will be returned, allowing the presence
# of some content to be checked before trying to use it.
#
# @example
# {{ "/includes/site_details" | item_at_path | assign_to: "site" }}
# {% if site %}
#
{{ site.title }}
# Written by {{ site.author }}
# {% end %}
#
# @api public
def item_at_path(input)
item = Noumenon.content_repository.get(input)
item ? item.stringify_keys : nil
end
# Formats the provided text using Textile.
#
# @example
# {{ "h1. Hello, world" | textilize }}
#
#
# @api public
def textilize(input)
RedCloth.new(input).to_html
end
end
Liquid::Template.register_filter(Filters)
# Renders a navigation menu.
#
# @api public
# @example
#
#
#
class NavigationTag < Liquid::Tag
def initialize(tag_name, syntax, tokens)
super
@options = { depth: 1, root: "/", include_root: true }
syntax.split(" ").each do |option|
key, value = option.split("=")
case key
when "depth"
@options[:depth] = value.to_i
when "root"
@options[:root] = value
when "include_root"
@options[:include_root] = [ "true", "yes", "1" ].include?(value)
end
end
end
def render_list(items)
list = items.collect do |item|
str = ""
str << %Q{#{item[:nav_title] || item[:title]}}
str << render_list(item[:children]) if item[:children]
str << ""
str
end
""
end
def render(context)
render_list Noumenon.content_repository.children(@options[:root], @options[:depth], @options[:include_root])
end
end
Liquid::Template.register_tag('nav_menu', NavigationTag)
class ThemeAssetTag < Liquid::Tag
def initialize(tag_name, asset, tokens)
@asset = asset.strip
end
def render(context)
File.join("/themes", URI.escape(Noumenon.theme.name), @asset)
end
end
Liquid::Template.register_tag('theme_asset', ThemeAssetTag)
class ContentAssetTag < Liquid::Tag
def initialize(tag_name, asset, tokens)
@asset = asset.strip
end
def render(context)
Noumenon.asset_repository.url_for(@asset)
end
end
Liquid::Template.register_tag('asset', ContentAssetTag)
end