Module: LiquidComponent

Defined in:
lib/liquid-component.rb,
lib/liquid-component/version.rb,
lib/liquid-component/metadata.rb,
lib/liquid-component/variable.rb,
lib/liquid-component/component.rb

Defined Under Namespace

Classes: Component, Metadata, Variable

Constant Summary collapse

FRONT_MATTER_REGEXP =
%r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.has_yaml_header?(template) ⇒ Boolean

Returns:



34
35
36
# File 'lib/liquid-component.rb', line 34

def self.has_yaml_header?(template)
  template.lines.first&.match? %r!\A---\s*\r?\n!
end

.parse(template) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/liquid-component.rb', line 13

def self.parse(template)
  template = template.to_s

  yaml_data = {}
  file_content = nil

  if has_yaml_header?(template)
    if template = template.match(FRONT_MATTER_REGEXP)
      file_content = template.post_match
      yaml_data = SafeYAML.load(template.captures[0])
    end
  else
    file_content = template
  end

  Component.new(
    metadata: .new(yaml_data),
    content: file_content
  )
end