Class: Sitepress::Parsers::Frontmatter

Inherits:
Base
  • Object
show all
Defined in:
lib/sitepress/parsers/frontmatter.rb

Overview

Parses metadata from the header of the page.

Defined Under Namespace

Classes: Renderer

Constant Summary collapse

PERMITTED_CLASSES =

Default classes that we’ll allow YAML to parse. Ideally this doesn’t get too huge and we let users control it by setting ‘Sitepress::Parsers::Frontmatter.permitted_classes = [Whatever, SuperDanger]`

[
  Date,
  Time
]
DELIMITER =
"---".freeze
NEWLINE =
/\r\n?|\n/.freeze
PATTERN =
/\A(#{DELIMITER}#{NEWLINE}(.+?)#{NEWLINE}#{DELIMITER}#{NEWLINE}*)?(.+)\Z/m

Class Attribute Summary collapse

Attributes inherited from Base

#body

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Frontmatter

Returns a new instance of Frontmatter.



48
49
50
# File 'lib/sitepress/parsers/frontmatter.rb', line 48

def initialize(content)
  _, @data, @body = content.match(PATTERN).captures
end

Class Attribute Details

.permitted_classesObject



68
69
70
# File 'lib/sitepress/parsers/frontmatter.rb', line 68

def permitted_classes
  @permitted_classes ||= PERMITTED_CLASSES
end

Instance Method Details

#dataObject



52
53
54
# File 'lib/sitepress/parsers/frontmatter.rb', line 52

def data
  @data ? load_yaml(@data) : {}
end

#load_yaml(data) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/sitepress/parsers/frontmatter.rb', line 56

def load_yaml(data)
  if YAML.respond_to? :safe_load
    YAML.safe_load data, permitted_classes: self.class.permitted_classes
  else
    # Live dangerously, lol
    YAML.load data
  end
end