Module: JekyllHighlightCards::TemplateRenderer

Included in:
LinkcardTag, PolaroidTag
Defined in:
lib/jekyll-highlight-cards/template_renderer.rb

Overview

Module for rendering Liquid templates with user override support

Instance Method Summary collapse

Instance Method Details

#find_template_path(site, template_name) ⇒ String?

Find template file path, checking user override first, then gem default

Parameters:

  • site (Jekyll::Site)

    the Jekyll site object

  • template_name (String)

    template name without .html extension

Returns:

  • (String, nil)

    path to template file or nil if not found

Raises:

  • (ArgumentError)

    if template_name contains invalid characters



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/jekyll-highlight-cards/template_renderer.rb', line 47

def find_template_path(site, template_name)
  validate_template_name!(template_name)

  template_filename = "#{template_name}.html"
  relative_path = File.join("highlight-cards", template_filename)

  # Check for user override first
  user_path = find_user_template(site, relative_path)
  return user_path if user_path

  # Fall back to gem bundled template
  find_gem_template(relative_path)
end

#render_template(site, template_name, variables) ⇒ String

Render a template with given variables

Parameters:

  • site (Jekyll::Site)

    the Jekyll site object

  • template_name (String)

    template name (e.g., "linkcard", "polaroid")

  • variables (Hash)

    variables to pass to template

Returns:

  • (String)

    rendered HTML

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jekyll-highlight-cards/template_renderer.rb', line 20

def render_template(site, template_name, variables)
  template_path = find_template_path(site, template_name)

  raise TemplateNotFoundError, "Template not found: #{template_name}" if template_path.nil?

  begin
    template_content = File.read(template_path)
    template = Liquid::Template.parse(template_content)
    template.render(variables)
  rescue Errno::ENOENT, Errno::EACCES => e
    raise TemplateRenderError, "Failed to read template '#{template_name}' at #{template_path}: #{e.class}: #{e}"
  rescue Encoding::InvalidByteSequenceError => e
    raise TemplateRenderError,
          "Invalid encoding in template '#{template_name}' at #{template_path}: #{e.class}: #{e}"
  rescue Liquid::SyntaxError, Liquid::Error => e
    raise TemplateRenderError, "Liquid error in template '#{template_name}': #{e.class}: #{e}"
  rescue StandardError => e
    raise TemplateRenderError, "Unexpected error rendering template '#{template_name}': #{e.class}: #{e}"
  end
end

#safe_template_path(allowed_dir, template_path) ⇒ String?

Verify template path is safe (within allowed directory)

Parameters:

  • allowed_dir (String)

    the allowed base directory

  • template_path (String)

    the template path to check

Returns:

  • (String, nil)

    template path if safe, nil otherwise



66
67
68
69
70
71
72
# File 'lib/jekyll-highlight-cards/template_renderer.rb', line 66

def safe_template_path(allowed_dir, template_path)
  return nil unless File.exist?(template_path)

  expanded_dir = File.expand_path(allowed_dir)
  expanded_path = File.expand_path(template_path)
  template_path if expanded_path.start_with?(expanded_dir)
end