Class: Bridgetown::FrontMatter::Loaders::YAML
- Defined in:
- lib/bridgetown-core/front_matter/loaders/yaml.rb
Overview
Reads YAML-formatted front matter delineated by triple hyphens
As an example, this resource loads to the hash {"published": false,
"title": "My post"}
.
---
published: false
title: My post
---
Constant Summary collapse
- HEADER =
%r!\A---[ \t]*\n!
- BLOCK =
%r!#{HEADER.source}(.*?\n?)^((---|\.\.\.)[ \t]*$\n?)!m
Class Method Summary collapse
-
.header?(file) ⇒ Boolean
Determines whether a given file has YAML front matter.
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Bridgetown::FrontMatter::Loaders::Base
Class Method Details
.header?(file) ⇒ Boolean
Determines whether a given file has YAML front matter
25 26 27 |
# File 'lib/bridgetown-core/front_matter/loaders/yaml.rb', line 25 def self.header?(file) File.open(file, "rb", &:gets)&.match?(HEADER) || false end |
Instance Method Details
#read(file_contents) ⇒ Object
30 31 32 33 34 35 36 37 38 |
# File 'lib/bridgetown-core/front_matter/loaders/yaml.rb', line 30 def read(file_contents, **) yaml_content = file_contents.match(BLOCK) or return Result.new( content: yaml_content.post_match.lstrip, front_matter: YAMLParser.load(yaml_content[1]), line_count: yaml_content[1].lines.size - 1 ) end |