Class: Sourcerer::Jekyll::Liquid::FileSystem

Inherits:
Liquid::LocalFileSystem
  • Object
show all
Defined in:
lib/sourcerer/jekyll/liquid/file_system.rb

Overview

A custom Liquid file system that extends `Liquid::LocalFileSystem` to support multiple root paths for template lookups. This allows templates to be resolved from a prioritized list of directories.

Instance Method Summary collapse

Constructor Details

#initialize(roots_or_root) ⇒ FileSystem

Initializes the file system with one or more root paths. rubocop:disable Lint/MissingSuper Intentional: Custom implementation that doesn't need parent's initialization

Parameters:

  • roots_or_root (String, Array<String>)
    A single root path or an array of root paths.


17
18
19
20
21
22
23
24
25
# File 'lib/sourcerer/jekyll/liquid/file_system.rb', line 17

def initialize roots_or_root
  if roots_or_root.is_a?(Array)
    @roots = roots_or_root.map { |root| File.expand_path(root) }
    @multi_root = true
  else
    @root = File.expand_path(roots_or_root)
    @multi_root = false
  end
end

Instance Method Details

#full_path(template_path) ⇒ String

Finds the full path of a template, searching through multiple roots if configured.

Parameters:

  • template_path (String)
    The path to the template.

Returns:

  • (String)
    The full, validated path to the template.

Raises:

  • (Liquid::FileSystemError)
    if the template is not found.


33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sourcerer/jekyll/liquid/file_system.rb', line 33

def full_path template_path
  if @multi_root
    @roots.each do |root|
      full = File.expand_path(File.join(root, template_path))
      return full if File.exist?(full) && full.start_with?(root)
    end
    raise ::Liquid::FileSystemError, "Template not found: '#{template_path}' in paths: #{@roots}"
  else
    full = File.expand_path(File.join(@root, template_path))
    validate_path(full)
  end
end

#read_template_file(template_path) ⇒ String

Reads the content of a template file.

Parameters:

  • template_path (String)
    The path to the template.

Returns:

  • (String)
    The content of the template file.


50
51
52
53
# File 'lib/sourcerer/jekyll/liquid/file_system.rb', line 50

def read_template_file template_path
  path = full_path(template_path)
  File.read(path)
end