Sha256: 1a98af032835085e71449270ceabb9ee3eea61062cde6b444e878be379b12cd4
Contents?: true
Size: 1.29 KB
Versions: 2
Compression:
Stored size: 1.29 KB
Contents
module LiquidMarkdown module TemplateHandler UNDERSCORE = '_'.freeze OBJECT_ATTRIBUTE_MATCHER = /%\{([a-z0-9_]+\.[a-z0-9_]+)\}/i def self.render(template, context, format) variables = expand_variables(template, extract_variables(context)) liquid_variables = variables.deep_stringify_keys lm = LiquidMarkdown::Render.new(template, liquid_variables) lm.send(format) end def self.expand_variables(template, variables) template.scan(OBJECT_ATTRIBUTE_MATCHER) .map(&:first) .each_with_object(variables) do |match, buffer| target, attribute = match.split('.') buffer[match.to_sym] = variables[target.to_sym].public_send(attribute) end end def self.extract_variables(context) context .instance_variable_get(:@_assigns) .each_with_object({}) do |(name, value), buffer| next if name.start_with?(UNDERSCORE) buffer[name.to_sym] = value end end class LIQMD def self.call(template) if template.formats.include?(:html) "LiquidMarkdown::TemplateHandler.render(#{template.source.inspect}, self, :html)" else "LiquidMarkdown::TemplateHandler.render(#{template.source.inspect}, self, :text)" end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
liquid_markdown-0.2.2 | lib/liquid_markdown/template_handler.rb |
liquid_markdown-0.2.1 | lib/liquid_markdown/template_handler.rb |