Module: Sourcerer::Rendering
- Defined in:
- lib/sourcerer/rendering.rb
Overview
Rendering and output orchestration primitives.
Class Method Summary collapse
-
.render_liquid_string(content, data) ⇒ String
Render a Liquid template string directly with a data hash.
-
.render_outputs(render_config) ⇒ Object
Renders templates or converter outputs based on a configuration.
-
.render_template(template_file, data_file, out_file, **options) ⇒ Object
Renders a single template with data.
-
.render_templates(templates_config) ⇒ Object
Renders a set of templates based on a configuration.
-
.render_with_converter(render_entry) ⇒ void
Renders output using a converter callable or converter constant name.
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.
205 206 207 208 209 210 211 212 213 214 |
# File 'lib/sourcerer/rendering.rb', line 205 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.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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, vars: render_entry[:vars] || {}) end end |
.render_template(template_file, data_file, out_file, **options) ⇒ Object
Renders a single template with data.
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 82 83 84 85 86 87 |
# File 'lib/sourcerer/rendering.rb', line 56 def self.render_template template_file, data_file, out_file, ** supported_option_keys = %i[data_object includes_load_paths attrs_source engine vars] unknown_option_keys = .keys - supported_option_keys raise ArgumentError, "unknown option(s): #{unknown_option_keys.join(', ')}" unless unknown_option_keys.empty? data_object = .fetch(:data_object, 'data') includes_load_paths = .fetch(:includes_load_paths, []) attrs_source = [:attrs_source] engine = .fetch(:engine, 'liquid') vars = ([:vars] || {}).transform_keys(&:to_s) data = load_render_data(data_file, attrs_source) out_file = File.(out_file) FileUtils.mkdir_p(File.dirname(out_file)) template_path = File.(template_file) template_content = File.read(template_path) context = { data_object => data, 'include' => { data_object => data }, 'vars' => vars } 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.
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.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/sourcerer/rendering.rb', line 93 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.(out_file) FileUtils.mkdir_p(File.dirname(out_file)) File.write(out_file, rendered) end |