Class: Coradoc::Html::TemplateLocator

Inherits:
Object
  • Object
show all
Defined in:
lib/coradoc/html/template_locator.rb

Overview

Locates Liquid templates from filesystem with fallback support

This class implements a template lookup algorithm:

  1. Check user’s template directories in order (first match wins)

  2. Fall back to default templates if not found

  3. Return nil if no template found (caller decides fallback)

Examples:

Basic usage

locator = TemplateLocator.new(
  user_dirs: ["/path/to/templates"],
  default_dir: "/default/templates"
)
locator.find("bibliography") # => "/path/to/templates/bibliography.liquid"

Constant Summary collapse

CORE_MODEL_DIR =

Default template subdirectory within each template root

'core_model'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_dirs: [], default_dir: nil) ⇒ TemplateLocator

Initialize the locator

Parameters:

  • user_dirs (Array<String>) (defaults to: [])

    User template directories (checked first)

  • default_dir (String) (defaults to: nil)

    Default templates directory (fallback)



32
33
34
35
36
# File 'lib/coradoc/html/template_locator.rb', line 32

def initialize(user_dirs: [], default_dir: nil)
  @user_dirs = Array(user_dirs).map { |d| Pathname.new(d) }
  @default_dir = default_dir ? Pathname.new(default_dir) : default_template_dir
  @cache = {}
end

Instance Attribute Details

#default_dirObject (readonly)

Returns the value of attribute default_dir.



26
27
28
# File 'lib/coradoc/html/template_locator.rb', line 26

def default_dir
  @default_dir
end

#user_dirsObject (readonly)

Returns the value of attribute user_dirs.



26
27
28
# File 'lib/coradoc/html/template_locator.rb', line 26

def user_dirs
  @user_dirs
end

Instance Method Details

#available_templatesArray<String>

Get all available template types

Returns:

  • (Array<String>)

    List of available template names



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/coradoc/html/template_locator.rb', line 78

def available_templates
  types = Set.new

  # Collect from user directories
  @user_dirs.each do |dir|
    core_model_path = dir / CORE_MODEL_DIR
    next unless core_model_path.exist? && core_model_path.directory?

    core_model_path.glob('*.liquid') do |f|
      types.add(f.basename('.liquid').to_s)
    end
  end

  # Collect from default directory
  if @default_dir&.exist? && @default_dir.directory?
    @default_dir.glob('*.liquid') do |f|
      types.add(f.basename('.liquid').to_s)
    end
  end

  types.to_a.sort
end

#clear_cacheObject

Clear the template cache (useful when template directories change)



109
110
111
# File 'lib/coradoc/html/template_locator.rb', line 109

def clear_cache
  @cache.clear
end

#default_template_dirPathname

Get the default template directory path

Returns:

  • (Pathname)

    Path to default templates



104
105
106
# File 'lib/coradoc/html/template_locator.rb', line 104

def default_template_dir
  Pathname.new(File.join(File.dirname(__FILE__), 'templates', CORE_MODEL_DIR))
end

#exists?(type_name) ⇒ Boolean

Check if a template exists (without loading it)

Parameters:

  • type_name (String)

    The template type

Returns:

  • (Boolean)

    True if template exists



71
72
73
# File 'lib/coradoc/html/template_locator.rb', line 71

def exists?(type_name)
  !find(type_name).nil?
end

#find(type_name) ⇒ Pathname?

Find a template by type name

Parameters:

  • type_name (String)

    The template type (e.g., “bibliography”, “section”)

Returns:

  • (Pathname, nil)

    Path to template file or nil if not found



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/coradoc/html/template_locator.rb', line 42

def find(type_name)
  return @cache[type_name] if @cache.key?(type_name)

  # First check user directories
  @user_dirs.each do |dir|
    template_path = dir / CORE_MODEL_DIR / "#{type_name}.liquid"
    if template_path.exist?
      @cache[type_name] = template_path
      return template_path
    end
  end

  # Then check default directory
  if @default_dir
    template_path = @default_dir / "#{type_name}.liquid"
    if template_path.exist?
      @cache[type_name] = template_path
      return template_path
    end
  end

  @cache[type_name] = nil
  nil
end