Class: RubyFrontMatter::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_front_matter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
# File 'lib/ruby_front_matter.rb', line 7

def initialize(options = {})
  options = {delimiter: '---', parser: YAML}.merge(options)
  @delimiter = options[:delimiter]
  @parser = options[:parser]
end

Instance Attribute Details

#parserObject (readonly)

Returns the value of attribute parser.



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

def parser
  @parser
end

Instance Method Details

#parse(string = '') ⇒ Object



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

def parse(string = '')
  res = [{},'']
  res[1] = string.lstrip.gsub(/#{@delimiter}(.*)#{@delimiter}/m) do |match|
    res[0] = @parser.load($~.captures.first.strip)
    ''
  end.strip
  res
end