Sha256: 4b5b0743d08b977955c73772185c970a991eda90dc63f81ed7616a336d83da55

Contents?: true

Size: 1.78 KB

Versions: 8

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true
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

8 entries across 8 versions & 2 rubygems

Version Path
brakeman-7.0.0 bundle/ruby/3.1.0/gems/slim-5.2.1/lib/slim/include.rb
brakeman-6.2.2 bundle/ruby/3.1.0/gems/slim-5.2.1/lib/slim/include.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/slim-5.2.1/lib/slim/include.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/slim-5.2.1/lib/slim/include.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/slim-5.2.1/lib/slim/include.rb
slim-5.2.1 lib/slim/include.rb
slim-5.2.0 lib/slim/include.rb
slim-5.1.1 lib/slim/include.rb