Sha256: 055ff69ad2f06c96458de3cc5330460bc54c98fad601835781a5ee178e494044

Contents?: true

Size: 1007 Bytes

Versions: 7

Compression:

Stored size: 1007 Bytes

Contents

require "multi_json"

module Temple
  module CoffeeScript
    class Generator < Temple::Generator
      default_options[:indent] = 0

      def call(exp)
        @indent = options[:indent]

        compile [:multi,
                 [:code, "#{buffer} = []"],
                 exp,
                 [:code, "#{buffer}.join('')"]]
      end

      def on_multi(*exp)
        exp.map {|e| compile(e) }.join("\n")
      end

      def on_static(text)
        concat(MultiJson.encode(text))
      end

      def on_dynamic(code)
        concat(code)
      end

      def on_code(code)
        indent(code)
      end

      def on_indent(exp)
        @indent += 1
        compile(exp)
      ensure
        @indent -= 1
      end

      def on_capture(name, exp)
        self.class.new(:buffer => name, :indent => @indent).call(exp)
      end

      def concat(str)
        indent("#{buffer}.push(#{str})")
      end

      def indent(str, indent = @indent)
        "  " * indent + str
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
skim-0.8.6 lib/temple/coffee_script/generators.rb
skim-0.8.5 lib/temple/coffee_script/generators.rb
skim-0.8.4 lib/temple/coffee_script/generators.rb
skim-0.8.3 lib/temple/coffee_script/generators.rb
skim-0.8.2 lib/temple/coffee_script/generators.rb
skim-0.8.1 lib/temple/coffee_script/generators.rb
skim-0.8.0 lib/temple/coffee_script/generators.rb