2013-03-10 22:08:56 +00:00
|
|
|
require 'json'
|
|
|
|
|
|
|
|
class ConfigTag < Liquid::Tag
|
|
|
|
def initialize(tag_name, options, tokens)
|
|
|
|
super
|
2013-03-11 06:19:35 +00:00
|
|
|
options = options.split(' ').map {|i| i.strip }
|
|
|
|
@key = options.slice!(0)
|
|
|
|
@tag = nil
|
|
|
|
@classname = nil
|
|
|
|
options.each do |option|
|
|
|
|
@tag = $1 if option =~ /tag:(\S+)/
|
|
|
|
@classname = $1 if option =~ /classname:(\S+)/
|
|
|
|
end
|
2013-03-10 22:08:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
2013-03-11 06:19:35 +00:00
|
|
|
config_tag(context.registers[:site].config, @key, @tag, @classname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def config_tag(config, key, tag=nil, classname=nil)
|
|
|
|
options = key.split('.').map { |k| config[k] }.last #reference objects with dot notation
|
|
|
|
tag ||= 'div'
|
|
|
|
classname ||= key.sub(/_/, '-').sub(/\./, '-')
|
|
|
|
output = "<#{tag} class='#{classname}'"
|
|
|
|
|
|
|
|
if options.respond_to? 'keys'
|
2013-03-10 22:08:56 +00:00
|
|
|
options.each do |k,v|
|
|
|
|
unless v.nil?
|
|
|
|
v = v.join ',' if v.respond_to? 'join'
|
|
|
|
v = v.to_json if v.respond_to? 'keys'
|
2013-03-11 06:19:35 +00:00
|
|
|
output += " data-#{k.sub'_','-'}='#{v}'"
|
2013-03-10 22:08:56 +00:00
|
|
|
end
|
|
|
|
end
|
2013-03-11 06:19:35 +00:00
|
|
|
elsif options.respond_to? 'join'
|
|
|
|
output += " data-value='#{config[key].join(',')}'"
|
|
|
|
else
|
|
|
|
output += " data-value='#{config[key]}'"
|
2013-03-10 22:08:56 +00:00
|
|
|
end
|
2013-03-11 06:19:35 +00:00
|
|
|
output += "></#{tag}>"
|
2013-03-10 22:08:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Liquid::Template.register_tag('config_tag', ConfigTag)
|
2013-03-11 06:19:35 +00:00
|
|
|
|