aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrandon Mathis <brandon@imathis.com>2013-03-11 01:19:35 -0500
committerBrandon Mathis <brandon@imathis.com>2013-03-11 01:19:59 -0500
commit05db158fec6d4c2ba3b558f2b83a0addb7f55b34 (patch)
tree4b80f0f6e8a9ee8a455e3cda5addd3ea8e88e488
parent2bec7f858a42b01171b157b5cda60cc54feb7e8d (diff)
downloadmy_new_personal_website-05db158fec6d4c2ba3b558f2b83a0addb7f55b34.tar.xz
my_new_personal_website-05db158fec6d4c2ba3b558f2b83a0addb7f55b34.zip
Improved: config_tag plugin is much more flexible now and can be used by other plugins directly through the config_tag method
-rw-r--r--plugins/config_tag.rb37
1 files changed, 26 insertions, 11 deletions
diff --git a/plugins/config_tag.rb b/plugins/config_tag.rb
index b071b181..92d1f63c 100644
--- a/plugins/config_tag.rb
+++ b/plugins/config_tag.rb
@@ -3,27 +3,42 @@ require 'json'
class ConfigTag < Liquid::Tag
def initialize(tag_name, options, tokens)
super
- @options = options.split(' ').map {|i| i.strip }
- @key = @options.first
- @tag = (@options[1] || 'div')
+ 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
end
def render(context)
- config = context.registers[:site].config
- options = @options.first.split('.').map { |k| config = config[k] }.last #reference objects with dot notation
- keyclass = @key.sub(/_/, '-').sub(/\./, '-')
- tag = "<#{@tag} class='#{keyclass}'"
+ 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'
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'
- tag += " data-#{k.sub'_','-'}='#{v}'"
+ output += " data-#{k.sub'_','-'}='#{v}'"
end
end
- tag += "></#{@tag}>"
- p tag
- tag
+ elsif options.respond_to? 'join'
+ output += " data-value='#{config[key].join(',')}'"
+ else
+ output += " data-value='#{config[key]}'"
end
+ output += "></#{tag}>"
end
Liquid::Template.register_tag('config_tag', ConfigTag)
+