Module: Sourcerer::Jekyll::Monkeypatches

Defined in:
lib/sourcerer/jekyll/monkeypatches.rb

Overview

This module contains monkeypatches for Jekyll to modify or extend its behavior.

Class Method Summary collapse

Class Method Details

.patch_jekyllObject

Patches Jekyll's `OptimizedIncludeTag` to modify its behavior. The patch enhances include path resolution and context handling to better suit the needs of Sourcerer's templating environment.


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sourcerer/jekyll/monkeypatches.rb', line 10

def self.patch_jekyll
  return unless defined?(::Jekyll::Tags::OptimizedIncludeTag)

  ::Jekyll::Tags::OptimizedIncludeTag.class_eval do
    define_method :render do |context|
      site = context.registers[:site]
      file = render_variable(context) || @file

      context.stack do
        context['include'] = parse_params(context) if @params

        source = site.inclusions[file]

        unless source

          # Debug lines before attempting path resolution

          # Safe resolution
          paths = context.registers[:includes_load_paths] || []
          path = paths
                 .map { |dir| File.join(dir, file) }
                 .find { |p| File.file?(p) }

          raise IOError, "Include file not found: #{file}" unless path

          source = File.read(path)
        end

        partial = ::Liquid::Template.parse(source)
        partial.registers[:site] = context.registers[:site]
        partial.assigns['include'] = context['include']

        ::Liquid::Template.register_filter(::Jekyll::Filters)
        ::Liquid::Template.register_filter(::Sourcerer::Jekyll::Liquid::Filters)

        # Use an isolated context so we can inspect and copy assigns
        subcontext = ::Liquid::Context.new(
          [{ 'include' => context['include'] }],
          {}, # Environments
          context.registers,
          rethrow_errors: true)

        rendered = partial.render!(subcontext)

        # Copy assigns from subcontext to parent context
        subcontext.environments.each do |env|
          env.each do |k, v|
            # Avoid clobbering outer include if reentrant
            next if k == 'include'

            context.environments.first[k] = v
          end
        end

        rendered
      end
    end
  end

  ::Liquid::Template.tags['include'] = ::Jekyll::Tags::OptimizedIncludeTag
end