module Octopress::TagHelpers::Var
Constants
- HAS_FILTERS
- TERNARY
Public Class Methods
evaluate_ternary(markup, context)
click to toggle source
# File lib/octopress-tag-helpers/var.rb, line 50 def self.evaluate_ternary(markup, context) if matched = markup.strip.match(TERNARY) condition = Liquid::Template.parse("{% if #{matched['condition']} %}true{% endif %}") "#{matched['markup']} #{(condition.render!(context) != '' ? matched['trueresult'] : matched['falseresult'])} #{matched['other']}" else markup end end
get_value(vars, context)
click to toggle source
# File lib/octopress-tag-helpers/var.rb, line 23 def self.get_value(vars, context) vars = evaluate_ternary(vars, context) vars = vars.strip.gsub(/ or /, ' || ') filters = false matched = vars.strip.match(HAS_FILTERS) if matched vars = matched['markup'] filters = matched['filters'] end vars = vars.split(/ \|\| /).map { |v| v if !context[v.strip].nil? }.compact return if vars.empty? var = vars.first if filters Liquid::Template.parse("{{ " +var << filters + " }}").render(context) else context[var] end end
parse_filters(input)
click to toggle source
Parses filters into arrays
input - a string of one or more filters, e.g. “| upcase | replace:'a','b'”
Returns nested arrays of filters and arguments
# File lib/octopress-tag-helpers/var.rb, line 65 def self.parse_filters(input) output = [] if input.match(/#{Liquid::FilterSeparator}\s*(.*)/o) filters = Regexp.last_match(1).scan(Liquid::Variable::FilterParser) filters.each do |f| if matches = f.match(/\s*(\w+)/) filtername = matches[1] filterargs = f.scan(/(?:#{Liquid::FilterArgumentSeparator}|#{Liquid::ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{Liquid::QuotedFragment})/o).flatten output << [filtername, filterargs] end end end output end
render_filters(content, filters, context)
click to toggle source
Passes input through Liquid filters
content - a string to be parsed filters - a series of liquid filters e.g. “| upcase | replace:'a','b'” context - the current Liquid context object
Returns a filtered string
# File lib/octopress-tag-helpers/var.rb, line 88 def self.render_filters(content, filters, context) filters = parse_filters(filters) return '' if content.nil? filters.inject(content) do |output, filter| filterargs = [] keyword_args = {} filter[1].to_a.each do |a| if matches = a.match(/\A#{Liquid::TagAttributes}\z/o) keyword_args[matches[1]] = context[matches[2]] else filterargs << context[a] end end filterargs << keyword_args unless keyword_args.empty? begin output = context.invoke(filter[0], output, *filterargs) rescue raise "Error - filter '#{filter[0]}' could not be found." end end end
set_var(var, operator, value, context)
click to toggle source
# File lib/octopress-tag-helpers/var.rb, line 7 def self.set_var(var, operator, value, context) case operator when '||=' context.scopes.last[var] = value if context.scopes.last[var].nil? when '+=' if context.scopes.last[var].nil? context.scopes.last[var] = value else context.scopes.last[var] += value end else context.scopes.last[var] = value end context end