Sha256: 924b2fc1777d017d3d1df1ee0bd06592dc7d1ed9ed77202ad2e074901dfa9fed

Contents?: true

Size: 989 Bytes

Versions: 2

Compression:

Stored size: 989 Bytes

Contents

module Octopress
  module Conditional
    EXPRESSION = /(.+?)\s+(unless|if)\s+(.+)/i
    TERNARY = /(.*?)\(\s*(.+?)\s+\?\s+(.+?)\s+:\s+(.+?)\s*\)(.+?)?/

    def strip_expression(markup, context = false)
      if markup =~ TERNARY
        result = evaluate_ternary($2, $3, $4, context)
        markup = "#{$1} #{result} #{$5}"
      end
      markup =~ EXPRESSION ? $1 : markup
    end

    def evaluate_ternary(expression, if_true, if_false, context)
      evaluate('if', expression, context) ? if_true : if_false
    end

    def evaluate_expression(markup, context)
      if markup =~ EXPRESSION
        evaluate($2, $3, context)
      else
        true
      end
    end

    def evaluate(type, expression, context)
      tag = if type == 'if'
        Liquid::If.new('if', expression, ["true","{% endif %}"])
      elsif type == 'unless'
        Liquid::Unless.new('unless', expression, ["true","{% endunless %}"])
      end
      result = tag.render(context) != ''
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
octopress-3.0.0.alpha2 lib/octopress/liquid_helpers/conditional.rb
octopress-3.0.0.alpha1 lib/octopress/liquid_helpers/conditional.rb