Sha256: 6f7e8977b749cb7dc3a646eb847c39e6a0711c2dd38aed64ed17cb33422a89d0

Contents?: true

Size: 1.13 KB

Versions: 2

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true
require 'temple'
require 'tilt'
require_relative 'base'

module Faml
  module FilterCompilers
    class TiltBase < Base
      include Temple::Utils

      def self.render_with_tilt(name, source, indent_width: 0)
        text = ::Tilt["t.#{name}"].new { source }.render
        indent = ' ' * indent_width
        "#{indent}#{text.rstrip.gsub("\n", "\n#{indent}")}"
      end

      protected

      def compile_with_tilt(temple, name, ast, indent_width: 0)
        source = ast.texts.join("\n")
        if TextCompiler.contains_interpolation?(source)
          text_temple = [:multi]
          compile_texts(text_temple, ast.lineno, ast.texts)
          sym = unique_name
          temple << [:capture, sym, text_temple]
          temple << [:dynamic, "::Faml::FilterCompilers::TiltBase.render_with_tilt(#{name.inspect}, #{sym}, indent_width: #{indent_width})"]
        else
          compiled = self.class.render_with_tilt(name, source, indent_width: indent_width)
          temple << [:static, compiled]
          temple.concat([[:newline]] * (ast.texts.size - 1))
        end
        temple
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faml-0.8.1 lib/faml/filter_compilers/tilt_base.rb
faml-0.8.0 lib/faml/filter_compilers/tilt_base.rb