Module: Sourcerer::Rendering

Defined in:
lib/sourcerer/rendering.rb

Overview

Rendering and output orchestration primitives.

Class Method Summary collapse

Class Method Details

.render_liquid_string(content, data) ⇒ String

Render a Liquid template string directly with a data hash.

Unlike render_template, this method accepts an in-memory string and a plain Ruby Hash rather than paths to data files. Suitable for rendering individual block content (e.g. in Sync/Cast) without setting up a full template pipeline.

Keys in ‘data` are stringified to satisfy Liquid’s string-key contract. Nested key stringification is shallow; callee is responsible for deeper transformations if required.

The Jekyll/Liquid runtime is initialized before rendering so that any custom filters or tags registered elsewhere in Sourcerer are available.

Parameters:

  • content (String)

    Liquid template source.

  • data (Hash)

    Variables available to the template.

Returns:

  • (String)

    Rendered output.



199
200
201
202
203
204
205
206
207
208
# File 'lib/sourcerer/rendering.rb', line 199

def self.render_liquid_string content, data
  require_relative 'jekyll'
  require_relative 'jekyll/liquid/filters'
  require_relative 'jekyll/liquid/tags'
  require 'liquid' unless defined?(Liquid::Template)
  Sourcerer::Jekyll.initialize_liquid_runtime

  template = Liquid::Template.parse(content)
  template.render(data.transform_keys(&:to_s))
end

.render_outputs(render_config) ⇒ Object

Renders templates or converter outputs based on a configuration.

Parameters:

  • render_config (Array<Hash>)

    A list of render configurations.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sourcerer/rendering.rb', line 20

def self.render_outputs render_config
  return if render_config.nil? || render_config.empty?

  render_config.each do |render_entry|
    if render_entry[:converter]
      render_with_converter(render_entry)
      next
    end

    data_obj = render_entry[:key] || 'data'
    attrs_source = render_entry[:attrs]
    engine = render_entry[:engine] || 'liquid'

    render_template(
      render_entry[:template],
      render_entry[:data],
      render_entry[:out],
      data_object: data_obj,
      attrs_source: attrs_source,
      engine: engine)
  end
end

.render_template(template_file, data_file, out_file, **options) ⇒ Object

Renders a single template with data.

Parameters:

  • template_file (String)

    The path to the template file.

  • data_file (String)

    The path to the data file (YAML).

  • out_file (String)

    The path to the output file.

  • data_object (String)

    The name of the data object in the template.

  • includes_load_paths (Array<String>)

    Paths for Liquid includes.

  • attrs_source (String)

    The path to an AsciiDoc file for attributes.

  • engine (String)

    The template engine to use.

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sourcerer/rendering.rb', line 52

def self.render_template template_file, data_file, out_file, **options
  supported_option_keys = %i[data_object includes_load_paths attrs_source engine]
  unknown_option_keys = options.keys - supported_option_keys
  raise ArgumentError, "unknown option(s): #{unknown_option_keys.join(', ')}" unless unknown_option_keys.empty?

  data_object = options.fetch(:data_object, 'data')
  includes_load_paths = options.fetch(:includes_load_paths, [])
  attrs_source = options[:attrs_source]
  engine = options.fetch(:engine, 'liquid')

  data = load_render_data(data_file, attrs_source)
  out_file = File.expand_path(out_file)
  FileUtils.mkdir_p(File.dirname(out_file))

  template_path = File.expand_path(template_file)
  template_content = File.read(template_path)

  context = {
    data_object => data,
    'include' => { data_object => data }
  }

  rendered = case engine.to_s
             when 'erb' then render_erb(template_content, context)
             when 'liquid' then render_liquid(template_file, template_content, context, includes_load_paths)
             else raise ArgumentError, "Unsupported template engine: #{engine}"
             end

  File.write(out_file, rendered)
end

.render_templates(templates_config) ⇒ Object

Renders a set of templates based on a configuration.

Parameters:

  • templates_config (Array<Hash>)

    An array of template configurations.



13
14
15
# File 'lib/sourcerer/rendering.rb', line 13

def self.render_templates templates_config
  render_outputs(templates_config)
end

.render_with_converter(render_entry) ⇒ void

This method returns an undefined value.

Renders output using a converter callable or converter constant name.

Parameters:

  • render_entry (Hash)

    Render entry containing converter config.

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sourcerer/rendering.rb', line 87

def self.render_with_converter render_entry
  data_file = render_entry[:data]
  out_file  = render_entry[:out]
  raise ArgumentError, 'render entry missing :data' unless data_file
  raise ArgumentError, 'render entry missing :out' unless out_file

  data = load_render_data(data_file, render_entry[:attrs])
  converter = resolve_converter(render_entry[:converter])
  rendered = converter.call(data, render_entry)
  raise ArgumentError, 'converter returned non-string output' unless rendered.is_a?(String)

  out_file = File.expand_path(out_file)
  FileUtils.mkdir_p(File.dirname(out_file))
  File.write(out_file, rendered)
end