Class: Munge::System::ItemFactory::ContentParser

Inherits:
Object
  • Object
show all
Defined in:
lib/munge/system/item_factory/content_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ ContentParser

Returns a new instance of ContentParser.



50
51
52
# File 'lib/munge/system/item_factory/content_parser.rb', line 50

def initialize(string)
  @frontmatter, @content = self.class.parse(string)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



54
55
56
# File 'lib/munge/system/item_factory/content_parser.rb', line 54

def content
  @content
end

#frontmatterObject

Returns the value of attribute frontmatter.



54
55
56
# File 'lib/munge/system/item_factory/content_parser.rb', line 54

def frontmatter
  @frontmatter
end

Class Method Details

.match(string) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/munge/system/item_factory/content_parser.rb', line 5

def self.match(string)
  string.match(/
    # Start of string
    \A
    # Begin frontmatter
    (?:^---\s*[\n\r]+)
    # Capture frontmatter
    (.*)
    # End frontmatter
    (?:^---\s*[\n\r]+)
    /mx)
end

.parse(string) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/munge/system/item_factory/content_parser.rb', line 18

def self.parse(string)
  matchdata = match(string)

  [
    parse_frontmatter(matchdata),
    parse_content(matchdata, string)
  ]
end

.parse_content(matchdata, string) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/munge/system/item_factory/content_parser.rb', line 42

def self.parse_content(matchdata, string)
  if matchdata
    matchdata.post_match
  else
    string
  end
end

.parse_frontmatter(matchdata) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/munge/system/item_factory/content_parser.rb', line 27

def self.parse_frontmatter(matchdata)
  return {} if matchdata.nil?

  parsed_frontmatter = YAML.load(matchdata[1])

  frontmatter_hash =
    if parsed_frontmatter.is_a?(Hash)
      parsed_frontmatter
    else
      {}
    end

  Munge::Util::SymbolHash.deep_convert(frontmatter_hash)
end