Class: Coradoc::Html::TemplateConfig

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

Overview

Configuration for the Liquid template system

This class manages template directories and provides utilities for template discovery and customization.

Examples:

Global configuration

Coradoc::Html.configure do |config|
  config.template_dirs = ["/path/to/custom/templates"]
end

Per-render configuration

Coradoc::Html.serialize(document, template_dirs: ["/custom/templates"])

Constant Summary collapse

DEFAULT_TEMPLATE_DIR =

Default template directory within the gem

Pathname.new(File.join(
  File.dirname(__FILE__), 'templates', 'core_model'
)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_dirs: []) ⇒ TemplateConfig

Initialize a new configuration

Parameters:

  • template_dirs (Array<String, Pathname>) (defaults to: [])

    Custom template directories



32
33
34
# File 'lib/coradoc/html/template_config.rb', line 32

def initialize(template_dirs: [])
  @template_dirs = Array(template_dirs).map { |dir| Pathname.new(dir) }
end

Instance Attribute Details

#template_dirsArray<Pathname>

Returns List of user-provided template directories.

Returns:

  • (Array<Pathname>)

    List of user-provided template directories



27
28
29
# File 'lib/coradoc/html/template_config.rb', line 27

def template_dirs
  @template_dirs
end

Class Method Details

.available_templatesArray<Symbol>

List all available default templates

Returns:

  • (Array<Symbol>)

    List of template names (without .liquid extension)



46
47
48
49
50
51
52
53
54
55
# File 'lib/coradoc/html/template_config.rb', line 46

def self.available_templates
  @available_templates ||= begin
    return [] unless DEFAULT_TEMPLATE_DIR.exist?

    DEFAULT_TEMPLATE_DIR
      .glob('*.liquid')
      .map { |f| f.basename('.liquid').to_s.to_sym }
      .sort
  end
end

.template_path_for(name) ⇒ Pathname?

Get the path to a specific default template

Parameters:

  • name (Symbol, String)

    Template name (e.g., :bibliography)

Returns:

  • (Pathname, nil)

    Path to the template file, or nil if not found



61
62
63
64
# File 'lib/coradoc/html/template_config.rb', line 61

def self.template_path_for(name)
  path = DEFAULT_TEMPLATE_DIR.join("#{name}.liquid")
  path.exist? ? path : nil
end

Instance Method Details

#all_template_dirsArray<Pathname>

Get all template directories (user + default)

Returns:

  • (Array<Pathname>)

    All template directories in search order



39
40
41
# File 'lib/coradoc/html/template_config.rb', line 39

def all_template_dirs
  @template_dirs + [DEFAULT_TEMPLATE_DIR]
end

#find_template(name) ⇒ Pathname?

Find a template by name

Parameters:

  • name (Symbol, String)

    Template name

Returns:

  • (Pathname, nil)

    Path to the first matching template



80
81
82
83
84
85
86
# File 'lib/coradoc/html/template_config.rb', line 80

def find_template(name)
  all_template_dirs.each do |dir|
    path = dir.join("#{name}.liquid")
    return path if path.exist?
  end
  nil
end

#reset!void

This method returns an undefined value.

Reset configuration to defaults



91
92
93
# File 'lib/coradoc/html/template_config.rb', line 91

def reset!
  @template_dirs = []
end

#template_exists?(name) ⇒ Boolean

Check if a template exists

Parameters:

  • name (Symbol, String)

    Template name

Returns:

  • (Boolean)

    True if template exists in any directory



70
71
72
73
74
# File 'lib/coradoc/html/template_config.rb', line 70

def template_exists?(name)
  all_template_dirs.any? do |dir|
    dir.join("#{name}.liquid").exist?
  end
end

#with_dirs(additional_dirs) ⇒ TemplateConfig

Create a copy of this configuration with additional directories

Parameters:

  • additional_dirs (Array<String, Pathname>)

    Extra directories

Returns:



99
100
101
102
103
# File 'lib/coradoc/html/template_config.rb', line 99

def with_dirs(additional_dirs)
  self.class.new(
    template_dirs: @template_dirs + Array(additional_dirs).map { |d| Pathname.new(d) }
  )
end