Module: Stack::Parsable

Included in:
Templates::Article, Templates::Layout, Templates::Page
Defined in:
lib/stack/parsable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/stack/parsable.rb', line 4

def data
  @data
end

#inline_dataObject

Returns the value of attribute inline_data.



5
6
7
# File 'lib/stack/parsable.rb', line 5

def inline_data
  @inline_data
end

#rawObject

Returns the value of attribute raw.



3
4
5
# File 'lib/stack/parsable.rb', line 3

def raw
  @raw
end

Instance Method Details

#extract_yaml(raw, replace = nil, defaults = { }) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stack/parsable.rb', line 15

def extract_yaml(raw, replace = nil, defaults = { })
  self.inline_data = [ ]
  
  data_table = defaults
  out = raw.dup
  i = -1
  
  while out =~ /^(---\s*\n.*?\n?)(---.*?\n)/m
    yaml = $1.dup
    out = out.gsub(/#{Regexp.escape(yaml)}#{Regexp.escape($2)}/m,((i>-1) ? "{{ document.data[#{i}] | render_table }}" : ""))
    
    d = YAML.load(yaml)
    
    if d.is_a?(Hash)
      self.inline_data << d if i > -1
      data_table = data_table.deep_merge(d)
    end
    
    d = nil;
    i += 1;
  end
  
  data_table = data_table.deep_symbolize
  
  return data_table, out
end

#parse!(raw_doc = self.raw, defaults = { }) ⇒ Object



7
8
9
# File 'lib/stack/parsable.rb', line 7

def parse!(raw_doc = self.raw, defaults = { })
  self.data, self.raw = extract_yaml(raw_doc, nil, defaults)
end

#payloadObject



11
12
13
# File 'lib/stack/parsable.rb', line 11

def payload
  self.data.deep_symbolize.merge(:data => inline_data)
end