Sha256: 0f9f36a9246cc23dfecae343430b2fdfc58f16049c1453696f5015adf88326c6

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

module Tongo
  class Generator
    
    TOKENIZER     = /(\%\{.+?\})/
    INTERPOLATION = /\%\{(.+?)\}/
    
    def call(exp)
      "\"#{compile!(exp)}\""
    end
    
    def initialize(options = {})
    end
    
    protected
    def compile!(exp)
      case exp.first
      when :multi
        exp[1..-1].map { |e| compile!(e) }.join
      when :empty
        "\#{#{exp[1]}(#{exp[2]})}"
      when :nested
        "\#{#{exp[1]}(#{exp[2].inspect}, \"#{compile!(exp[3])}\")}"
      when :text
        compiled_text(exp[1])
      when :html
        html(exp)
      else
        raise "Unhandled exp: #{exp.first}"
      end
    end
    
    private
    def html(exp)
      "<#{exp[1]}#{join_attributes(exp[2])}>#{compile!(exp[3])}</#{exp[1]}>"
    end
    
    def join_attributes(attributes)
      return '' if attributes.empty?
      attributes.inject([]) do |memo,attribute|
        memo << "#{attribute[0]}=\\\"#{attribute[1]}\\\""
      end.join('').tap { |result| result.replace(" #{result}") }
    end
    
    def tokenize_text(input)
      input.split(TOKENIZER)
    end
    
    def compiled_text(input)
      tokenize_text(input).map do |token|
        (matchdata = token.match(INTERPOLATION)) ? handle_interpolation_token(token, matchdata) : token
      end.join
    end
    
    def handle_interpolation_token(token, matchdata)
      pattern, value = matchdata.values_at(0, 1)
      expand_interpolation(value)
    end
    
    def expand_interpolation(interpolation)
      interpolation.split('|').inject('') { |memo,element| "#{element.strip}(#{memo})" }.tap { |result| result.replace("\#{#{result}}") }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tongo-0.0.2 lib/tongo/generator.rb