<commit_before>require 'slim'

module Slim
  # Handles inlined includes
  # @api private
  class Include < Slim::Filter
    define_options :file, :include_dirs => [Dir.pwd, '.']

    CONTENT_RULE = Temple::Grammar::Rule([:slim, :text, [:multi, [:slim, :interpolate, String]]])

    def on_html_tag(tag, attributes, content)
      if tag == 'include'
        raise ArgumentError, "Invalid include statement" unless attributes == [:html, :attrs] && CONTENT_RULE === content
        name = content[2][1][2]
        name << '.slim' if name !~ /\.slim\Z/i
        current_dir = File.dirname(File.expand_path(options[:file]))
        file = options[:include_dirs].map {|dir| File.expand_path(File.join(dir, name), current_dir) }.find {|file| File.exists?(file) }
        raise "'#{name}' not found in #{options[:include_dirs].inspect}" unless file
        content = File.read(name)
        Thread.current[:slim_engine].call(content)
      else
        [:html, :tag, tag, attributes, content]
      end
    end
  end

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

    alias old_call call
    def call(input)
      Thread.current[:slim_engine] = self
      Thread.current[:slim_level] ||= 0
      Thread.current[:slim_level] += 1
      catch(:stop) { old_call(input) }
    ensure
      Thread.current[:slim_engine] = nil if (Thread.current[:slim_level] -= 1) == 0
    end
  end
end<commit_msg>slim/include minor cleanup<commit_after>require 'slim'

module Slim
  # Handles inlined includes
  # @api private
  class Include < Slim::Filter
    define_options :file, :include_dirs => [Dir.pwd, '.']

    CONTENT_RULE = Temple::Grammar::Rule [:slim, :text, [:multi, [:slim, :interpolate, String]]]

    def on_html_tag(tag, attributes, content)
      return super if tag != 'include'
      raise ArgumentError, 'Invalid include statement' unless attributes == [:html, :attrs] && CONTENT_RULE === content
      name = content[2][1][2]
      name = "#{name}.slim" if name !~ /\.slim\Z/i
      current_dir = File.dirname(File.expand_path(options[:file]))
      file = options[:include_dirs].map {|dir| File.expand_path(File.join(dir, name), current_dir) }.find {|file| File.exists?(file) }
      raise "'#{name}' not found in #{options[:include_dirs].inspect}" unless file
      Thread.current[:slim_include_engine].call(File.read(name))
    end
  end

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

    alias call_without_include call
    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
{'fraction_non_alphanumeric': 0.141644908616188, 'fraction_numerical': 0.005221932114882507, 'mean_word_length': 3.4614264919941777, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '614895', 'n_tokens_mistral': 1032, 'n_tokens_neox': 1010, 'n_words': 239}