Merge pull request #1647 from middleman/frontmatter_delims

Frontmatter delims
This commit is contained in:
Thomas Reynolds 2015-11-11 13:20:15 -08:00
commit b4457c98d2
9 changed files with 54 additions and 18 deletions

View file

@ -185,6 +185,12 @@ module Middleman
end
}, 'Callbacks that can exclude paths from the sitemap'
# Set textual delimiters that denote the start and end of frontmatter
define_setting :frontmatter_delims, {
json: [%w(;;; ;;;)],
yaml: [%w(--- ---), %w(--- ...)]
}, 'Allowed frontmatter delimiters'
define_setting :watcher_disable, false, 'If the Listen watcher should not run'
define_setting :watcher_force_polling, false, 'If the Listen watcher should run in polling mode'
define_setting :watcher_latency, nil, 'The Listen watcher latency'

View file

@ -99,10 +99,10 @@ module Middleman
basename = File.basename(data_path, extension)
if %w(.yaml .yml).include?(extension)
data, postscript = ::Middleman::Util::Data.parse(file[:full_path], :yaml)
data, postscript = ::Middleman::Util::Data.parse(file[:full_path], @app.config[:frontmatter_delims], :yaml)
data[:postscript] = postscript if !postscript.nil? && data.is_a?(Hash)
elsif extension == '.json'
data, _postscript = ::Middleman::Util::Data.parse(file[:full_path], :json)
data, _postscript = ::Middleman::Util::Data.parse(file[:full_path], @app.config[:frontmatter_delims], :json)
else
return
end

View file

@ -65,7 +65,10 @@ module Middleman::CoreExtensions
return [{}, nil] unless file
@cache[file[:full_path]] ||= ::Middleman::Util::Data.parse(file[:full_path])
@cache[file[:full_path]] ||= ::Middleman::Util::Data.parse(
file[:full_path],
app.config[:frontmatter_delims]
)
end
Contract ArrayOf[IsA['Middleman::SourceFile']], ArrayOf[IsA['Middleman::SourceFile']] => Any

View file

@ -14,7 +14,7 @@ module Middleman::Util::Data
# @param [String] path
# @return [Array<Hash, String>]
Contract Pathname, Maybe[Symbol] => [Hash, Maybe[String]]
def parse(full_path, known_type=nil)
def parse(full_path, frontmatter_delims, known_type=nil)
return [{}, nil] if Middleman::Util.binary?(full_path)
# Avoid weird race condition when a file is renamed
@ -24,16 +24,22 @@ module Middleman::Util::Data
return [{}, nil]
end
/
start_delims, stop_delims = frontmatter_delims
.values
.flatten(1)
.transpose
.map(&Regexp.method(:union))
match = /
\A(?:[^\r\n]*coding:[^\r\n]*\r?\n)?
(?<start>---|;;;)[ ]*\r?\n
(?<start>#{start_delims})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n?
^(?<stop>---|\.\.\.|;;;)[ ]*\r?\n?
^(?<stop>#{stop_delims})[ ]*\r?\n?
\r?\n?
(?<additional_content>.*)
/mx =~ content
/mx.match(content) || {}
unless frontmatter
unless match[:frontmatter]
case known_type
when :yaml
return [parse_yaml(content, full_path), nil]
@ -42,13 +48,22 @@ module Middleman::Util::Data
end
end
case [start, stop]
when %w(--- ---), %w(--- ...)
[parse_yaml(frontmatter, full_path), additional_content]
when %w(;;; ;;;)
[parse_json("{#{frontmatter}}", full_path), additional_content]
case [match[:start], match[:stop]]
when *frontmatter_delims[:yaml]
[
parse_yaml(match[:frontmatter], full_path),
match[:additional_content]
]
when *frontmatter_delims[:json]
[
parse_json("{#{match[:frontmatter]}}", full_path),
match[:additional_content]
]
else
[{}, content]
[
{},
content
]
end
end