Module: Octopress::TagHelpers::Var
- Defined in:
- lib/octopress-tag-helpers/var.rb
Constant Summary collapse
- TERNARY =
/(?<markup>.*?)\(\s*(?<condition>.+?)\s+\?\s+(?<trueresult>.+?)\s+:\s+(?<falseresult>.+?)\s*\)(?<other>.+)?/- HAS_FILTERS =
/(?<markup>.*?)(?<filters>(\A|[^|])\|\s+.+)/
Class Method Summary collapse
- .evaluate_ternary(markup, context) ⇒ Object
- .get_value(vars, context) ⇒ Object
-
.parse_filters(input) ⇒ Object
Parses filters into arrays.
-
.render_filters(content, filters, context) ⇒ Object
Passes input through Liquid filters.
- .set_var(var, operator, value, context) ⇒ Object
Class Method Details
.evaluate_ternary(markup, context) ⇒ Object
50 51 52 53 54 55 56 57 |
# 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) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# 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) ⇒ Object
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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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) ⇒ Object
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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# 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) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# 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 |