Sha256: ca073e438e426e5f0290fe3a0745b18273a0b0955d9cb4bdf3d0d62a120d688d

Contents?: true

Size: 1.75 KB

Versions: 101

Compression:

Stored size: 1.75 KB

Contents

require 'slim'

module Slim
  # Handles inlined includes
  #
  # Slim files are compiled, non-Slim files are included as text with `#{interpolation}`
  #
  # @api private
  class Include < Slim::Filter
    define_options :file, include_dirs: [Dir.pwd, '.']

    def on_html_tag(tag, attributes, content = nil)
      return super if tag != 'include'
      name = content.to_a.flatten.select {|s| String === s }.join
      raise ArgumentError, 'Invalid include statement' unless attributes == [:html, :attrs] && !name.empty?
      unless file = find_file(name)
        name = "#{name}.slim" if name !~ /\.slim\Z/i
        file = find_file(name)
      end
      raise Temple::FilterError, "'#{name}' not found in #{options[:include_dirs].join(':')}" unless file
      content = File.read(file)
      if file =~ /\.slim\Z/i
        Thread.current[:slim_include_engine].call(content)
      else
        [:slim, :interpolate, content]
      end
    end

    protected

    def find_file(name)
      current_dir = File.dirname(File.expand_path(options[:file]))
      options[:include_dirs].map {|dir| File.expand_path(File.join(dir, name), current_dir) }.find {|file| File.file?(file) }
    end
  end

  class Engine
    after Slim::Parser, Slim::Include
    after Slim::Include, :stop do |exp|
      throw :stop, exp if Thread.current[:slim_include_level] > 1
      exp
    end

    # @api private
    alias call_without_include call

    # @api private
    def call(input)
      Thread.current[:slim_include_engine] = self
      Thread.current[:slim_include_level] ||= 0
      Thread.current[:slim_include_level] += 1
      catch(:stop) { call_without_include(input) }
    ensure
      Thread.current[:slim_include_engine] = nil if (Thread.current[:slim_include_level] -= 1) == 0
    end
  end
end

Version data entries

101 entries across 87 versions & 6 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/include.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/include.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-6.1.2 bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-6.1.1 bundle/ruby/3.0.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-6.1.0 bundle/ruby/3.1.0/gems/slim-4.1.0/lib/slim/include.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-6.0.1 bundle/ruby/3.1.0/gems/slim-4.1.0/lib/slim/include.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-6.0.0 bundle/ruby/3.0.0/gems/slim-4.1.0/lib/slim/include.rb
slim-5.1.0 lib/slim/include.rb
brakeman-5.4.1 bundle/ruby/3.1.0/gems/slim-4.1.0/lib/slim/include.rb
slim-5.0.0 lib/slim/include.rb
brakeman-5.4.0 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-5.3.1 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-5.3.0 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-5.2.3 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-5.2.2 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-5.2.1 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb
brakeman-5.2.0 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/include.rb