Sha256: f7c5064b69cd34ad856c264c68847d5bdc22c27d4c40f797292c4c6bb0a1a4a1

Contents?: true

Size: 1.17 KB

Versions: 55

Compression:

Stored size: 1.17 KB

Contents

require 'tilt'
require 'hamlit/concerns/string_interpolation'
require 'hamlit/filters/base'

module Hamlit
  module Filters
    class Tilt < Filters::Base
      include Concerns::StringInterpolation

      def self.render(name, source, indent_width: 2)
        result = ::Tilt["t.#{name}"].new { source }.render
        indent_source(result, indent_width: indent_width)
      end

      private

      def compile_with_tilt(name, source, ast, indent_width: 2)
        if contains_interpolation?(source)
          return runtime_compile(name, source, ast, indent_width: indent_width)
        end

        content = [:static, Filters::Tilt.render(name, source, indent_width: indent_width)]
        build_ast(ast, content)
      end

      def runtime_compile(name, source, ast, indent_width: 2)
        literal = string_literal(source)
        code    = "::Hamlit::Filters::Tilt.render(#{name.inspect}, #{literal}, indent_width: #{indent_width})"
        content = [:dynamic, code]
        build_ast(ast, content)
      end

      def build_ast(ast, content)
        return content if ast.empty?

        content = [:multi, [:static, "\n"], content]
        ast << content
      end
    end
  end
end

Version data entries

55 entries across 55 versions & 1 rubygems

Version Path
hamlit-1.7.2 lib/hamlit/filters/tilt.rb
hamlit-1.7.1 lib/hamlit/filters/tilt.rb
hamlit-1.7.0 lib/hamlit/filters/tilt.rb
hamlit-1.6.7 lib/hamlit/filters/tilt.rb
hamlit-1.6.6 lib/hamlit/filters/tilt.rb
hamlit-1.6.5 lib/hamlit/filters/tilt.rb
hamlit-1.6.4 lib/hamlit/filters/tilt.rb
hamlit-1.6.3 lib/hamlit/filters/tilt.rb
hamlit-1.6.2 lib/hamlit/filters/tilt.rb
hamlit-1.6.1 lib/hamlit/filters/tilt.rb
hamlit-1.6.0 lib/hamlit/filters/tilt.rb
hamlit-1.5.9 lib/hamlit/filters/tilt.rb
hamlit-1.5.8 lib/hamlit/filters/tilt.rb
hamlit-1.5.7 lib/hamlit/filters/tilt.rb
hamlit-1.5.6 lib/hamlit/filters/tilt.rb
hamlit-1.5.5 lib/hamlit/filters/tilt.rb
hamlit-1.5.4 lib/hamlit/filters/tilt.rb
hamlit-1.5.3 lib/hamlit/filters/tilt.rb
hamlit-1.5.2 lib/hamlit/filters/tilt.rb
hamlit-1.5.1 lib/hamlit/filters/tilt.rb