Module: LiquidMarkdown::TemplateHandler

Defined in:
lib/liquid_markdown/template_handler.rb

Defined Under Namespace

Classes: LIQMD

Constant Summary collapse

UNDERSCORE =
'_'.freeze
OBJECT_ATTRIBUTE_MATCHER =
/%\{([a-z0-9_]+\.[a-z0-9_]+)\}/i

Class Method Summary collapse

Class Method Details

.expand_variables(template, variables) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/liquid_markdown/template_handler.rb', line 13

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

.extract_variables(context) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/liquid_markdown/template_handler.rb', line 22

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

.render(template, context, format) ⇒ Object



6
7
8
9
10
11
# File 'lib/liquid_markdown/template_handler.rb', line 6

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