<commit_before>module LiquidMarkdown
  module TemplateHandler
    UNDERSCORE = '_'.freeze
    OBJECT_ATTRIBUTE_MATCHER = /%\{([a-z0-9_]+\.[a-z0-9_]+)\}/i

    def self.render(template, context, format)
      variables = expand_variables(template, extract_variables(context))
      liquid_variables = variables[:lmVariables]
      layout = variables[:lmLayout]
      lm = LiquidMarkdown::Render.new(template, liquid_variables)
      lm.layout = layout
      lm.send(format)
    end

    def self.expand_variables(template, variables)
      template.scan(OBJECT_ATTRIBUTE_MATCHER)
          .map(&:first)
          .each_with_object(variables) do |match, buffer|
        target, attribute = match.split('.')
        buffer[match.to_sym] = variables[target.to_sym].public_send(attribute)
      end
    end

    def self.extract_variables(context)
      context
          .instance_variable_get(:@_assigns)
          .each_with_object({}) do |(name, value), buffer|
        next if name.start_with?(UNDERSCORE)
        buffer[name.to_sym] = value
      end
    end

    class Text
      def self.call(template)
        "LiquidMarkdown::TemplateHandler.render(#{template.source.inspect}, self, :text)"
      end
    end

    class HTML
      def self.call(template)
        "LiquidMarkdown::TemplateHandler.render(#{template.source.inspect}, self, :html)"
      end
    end
  end
end<commit_msg>Only set if layout is not nil<commit_after>module LiquidMarkdown
  module TemplateHandler
    UNDERSCORE = '_'.freeze
    OBJECT_ATTRIBUTE_MATCHER = /%\{([a-z0-9_]+\.[a-z0-9_]+)\}/i

    def self.render(template, context, format)
      variables = expand_variables(template, extract_variables(context))
      liquid_variables = variables[:lmVariables]
      layout = variables[:lmLayout]
      lm = LiquidMarkdown::Render.new(template, liquid_variables)
      lm.layout = layout unless layout.nil?
      lm.send(format)
    end

    def self.expand_variables(template, variables)
      template.scan(OBJECT_ATTRIBUTE_MATCHER)
          .map(&:first)
          .each_with_object(variables) do |match, buffer|
        target, attribute = match.split('.')
        buffer[match.to_sym] = variables[target.to_sym].public_send(attribute)
      end
    end

    def self.extract_variables(context)
      context
          .instance_variable_get(:@_assigns)
          .each_with_object({}) do |(name, value), buffer|
        next if name.start_with?(UNDERSCORE)
        buffer[name.to_sym] = value
      end
    end

    class Text
      def self.call(template)
        "LiquidMarkdown::TemplateHandler.render(#{template.source.inspect}, self, :text)"
      end
    end

    class HTML
      def self.call(template)
        "LiquidMarkdown::TemplateHandler.render(#{template.source.inspect}, self, :html)"
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11367673179396093, 'fraction_numerical': 0.002841918294849023, 'mean_word_length': 2.9329608938547485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7136078', 'n_tokens_mistral': 878, 'n_tokens_neox': 859, 'n_words': 163}