Module: Dimples::Frontable

Included in:
Page, Template
Defined in:
lib/dimples/frontable.rb

Overview

A mixin class that handles reading and parsing front matter from a file.

Constant Summary collapse

SKIPPED_METADATA_KEYS =
%w[site path contents].freeze

Instance Method Summary collapse

Instance Method Details

#read_with_front_matterObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/dimples/frontable.rb', line 8

def read_with_front_matter
  @contents = File.read(@path)

  matches = @contents.match(/^(-{3}\n.*?\n?)^(-{3}*$\n?)/m)
  return if matches.nil?

  YAML.safe_load(matches[1]).each_pair do |key, value|
    if !SKIPPED_METADATA_KEYS.include?(key) && respond_to?("#{key}=")
      send("#{key}=", value)
    end
  end

  @contents = matches.post_match.strip
end