<commit_before>module Munge
  module Helpers
    module Rendering
      def render(item, engines: nil, data: {}, content_override: nil)
        content   = content_override || item.content
        renderers = tilt_renderer_list(item, engines)
        mdata     = merged_data(item.frontmatter, data, self_item: item)

        render_string(content, data: mdata, engines: renderers)
      end

      def layout(item_or_string, data: {}, &block)
        layout_item = resolve_layout(item_or_string)
        renderers   = tilt_renderer_list(layout_item, nil)
        mdata       = merged_data(layout_item.frontmatter, data, self_layout: layout_item)

        render_string(layout_item.content, data: mdata, engines: renderers, &block)
      end

      def render_string(content, data: {}, engines: [], &block)
        inner =
          if block_given?
            capture(&block)
          end

        output =
          engines
            .inject(content) do |memo, engine|
              template = engine.new { memo }

              if inner
                template.render(self, data) { inner }
              else
                template.render(self, data)
              end
            end

        if block_given?
          append_to_erbout(block.binding, output)
        end

        output
      end

      def render_with_layout(item, content_engines: nil, data: {}, content_override: nil)
        inner = render(item, engines: content_engines, data: data, content_override: content_override)
        mdata = merged_data(item.frontmatter, data, self_item: item)

        if item.layout
          layout(item.layout, data: mdata) do
            inner
          end
        else
          inner
        end
      end

      private

      def merged_data(*data)<commit_msg>name the template with the error<commit_after>module Munge
  module Helpers
    module Rendering
      def render(item, engines: nil, data: {}, content_override: nil)
        content   = content_override || item.content
        renderers = tilt_renderer_list(item, engines)
        mdata     = merged_data(item.frontmatter, data, self_item: item)
        item_path = item.relpath

        render_string(content, data: mdata, engines: renderers, template_name: item_path)
      end

      def layout(item_or_string, data: {}, &block)
        layout_item = resolve_layout(item_or_string)
        renderers   = tilt_renderer_list(layout_item, nil)
        mdata       = merged_data(layout_item.frontmatter, data, self_layout: layout_item)
        layout_path = "(layout) #{layout_item.relpath}"

        render_string(layout_item.content, data: mdata, engines: renderers, template_name: layout_path, &block)
      end

      def render_string(content, data: {}, engines: [], template_name: nil, &block)
        inner =
          if block_given?
            capture(&block)
          end

        output =
          engines
            .inject(content) do |memo, engine|
              template = engine.new(template_name) { memo }

              if inner
                template.render(self, data) { inner }
              else
                template.render(self, data)
              end
            end

        if block_given?
          append_to_erbout(block.binding, output)
        end

        output
      end

      def render_with_layout(item, content_engines: nil, data: {}, content_override: nil)
        inner = render(item, engines: content_engines, data: data, content_override: content_override)
        mdata = merged_data(item.frontmatter, data, self_item: item)

        if item.layout
          layout(item.layout, data: mdata) do
            inner
          end
        else
          inner
        end
      end

      private

      def merged_data(*data)
{'fraction_non_alphanumeric': 0.08126831867839063, 'fraction_numerical': 0.0, 'mean_word_length': 2.0545158665581775, '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': '4900088', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1030, 'n_words': 259}