-
-
Notifications
You must be signed in to change notification settings - Fork 747
/
data.rb
193 lines (171 loc) · 5.09 KB
/
data.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# frozen_string_literal: true
require 'yaml'
require 'json'
require 'toml'
require 'pathname'
require 'hashie'
require 'memoist'
require 'middleman-core/util/binary'
require 'middleman-core/contracts'
module Middleman
module Util
include Contracts
module_function
class EnhancedHash < ::Hashie::Mash
# include ::Hashie::Extensions::MergeInitializer
# include ::Hashie::Extensions::MethodReader
# include ::Hashie::Extensions::IndifferentAccess
end
# Recursively convert a normal Hash into a EnhancedHash
#
# @private
# @param [Hash] data Normal hash
# @return [Hash]
Contract Hash => IsA['::Middleman::Util::EnhancedHash']
def recursively_enhance(obj)
case obj
when ::Array
obj.map { |e| recursively_enhance(e) }
when ::Hash
EnhancedHash.new(obj)
else
obj
end
end
module Data
extend Memoist
include Contracts
module_function
# Get the frontmatter and plain content from a file
# @param [String] path
# @return [Array<Hash, String>]
Contract IsA['Middleman::SourceFile'], Maybe[Symbol] => [Hash, Maybe[String]]
def parse(source_file, frontmatter_delims, known_type = nil)
full_path = source_file[:full_path]
return [{}, nil] if ::Middleman::Util.binary?(full_path) || source_file[:types].include?(:binary)
# Avoid weird race condition when a file is renamed
begin
content = source_file.read
rescue EOFError, IOError, ::Errno::ENOENT
return [{}, nil]
end
match = build_regexes(frontmatter_delims)
.lazy
.map { |r| r.match(content) }
.reject(&:nil?)
.first || {}
unless match[:frontmatter]
case known_type
when :yaml
return [parse_yaml(content, full_path), nil]
when :json
return [parse_json(content, full_path), nil]
when :toml
return [parse_toml(content, full_path), nil]
end
end
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]
]
when *frontmatter_delims[:toml]
[
parse_toml(match[:frontmatter], full_path),
match[:additional_content]
]
else
[
{},
content
]
end
end
def build_regexes(frontmatter_delims)
frontmatter_delims
.values
.flatten(1)
.map do |start, stop|
/
\A(?:[^\r\n]*coding:[^\r\n]*\r?\n)?
(?<start>#{Regexp.escape(start)})[ ]*\r?\n
(?<frontmatter>.*?)[ ]*\r?\n?
^(?<stop>#{Regexp.escape(stop)})[ ]*\r?\n?
\r?\n?
(?<additional_content>.*)
/mx
end
end
memoize :build_regexes
# Parse YAML frontmatter out of a string
# @param [String] content
# @return [Hash]
Contract String, Pathname => Hash
def parse_yaml(content, full_path)
c = begin
::Middleman::Util.instrument 'parse.yaml' do
::YAML.load(content)
end
rescue StandardError, ::Psych::SyntaxError => e
warn "YAML Exception parsing #{full_path}: #{e.message}"
{}
end
c ? symbolize_recursive(c) : {}
end
memoize :parse_yaml
# Parse TOML frontmatter out of a string
# @param [String] content
# @return [Hash]
Contract String, Pathname => Hash
def parse_toml(content, full_path)
c = begin
::Middleman::Util.instrument 'parse.toml' do
::TOML.load(content)
end
rescue StandardError
# TOML parser swallows useful error, so we can't warn about it.
# https://github.com/jm/toml/issues/47
warn "TOML Exception parsing #{full_path}"
{}
end
c ? symbolize_recursive(c) : {}
end
memoize :parse_yaml
# Parse JSON frontmatter out of a string
# @param [String] content
# @return [Hash]
Contract String, Pathname => Hash
def parse_json(content, full_path)
c = begin
::Middleman::Util.instrument 'parse.json' do
::JSON.parse(content)
end
rescue StandardError => e
warn "JSON Exception parsing #{full_path}: #{e.message}"
{}
end
c ? symbolize_recursive(c) : {}
end
memoize :parse_json
def symbolize_recursive(value)
case value
when Hash
value.map do |k, v|
key = k.is_a?(String) ? k.to_sym : k
[key, symbolize_recursive(v)]
end.to_h
when Array
value.map { |v| symbolize_recursive(v) }
else
value
end
end
end
end
end