front_matter_parser 0.0.4 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.codeclimate.yml +19 -0
- data/.gitignore +1 -0
- data/.overcommit.yml +54 -0
- data/.overcommit_gems.rb +15 -0
- data/.rspec +1 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +18 -1
- data/Dockerfile +8 -0
- data/Gemfile +0 -2
- data/README.md +64 -24
- data/Rakefile +5 -3
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/docker-compose.yml +7 -0
- data/front_matter_parser.gemspec +9 -5
- data/lib/front_matter_parser.rb +10 -114
- data/lib/front_matter_parser/loader.rb +11 -0
- data/lib/front_matter_parser/loader/yaml.rb +18 -0
- data/lib/front_matter_parser/parsed.rb +25 -14
- data/lib/front_matter_parser/parser.rb +80 -0
- data/lib/front_matter_parser/syntax_parser.rb +28 -0
- data/lib/front_matter_parser/syntax_parser/factorizable.rb +30 -0
- data/lib/front_matter_parser/syntax_parser/indentation_comment.rb +48 -0
- data/lib/front_matter_parser/syntax_parser/multi_line_comment.rb +49 -0
- data/lib/front_matter_parser/syntax_parser/single_line_comment.rb +63 -0
- data/lib/front_matter_parser/version.rb +3 -1
- data/spec/fixtures/example +6 -0
- data/spec/front_matter_parser/loader/yaml_spec.rb +15 -0
- data/spec/front_matter_parser/parsed_spec.rb +11 -21
- data/spec/front_matter_parser/parser_spec.rb +111 -0
- data/spec/front_matter_parser/syntax_parser/indentation_comment_spec.rb +146 -0
- data/spec/front_matter_parser/syntax_parser/multi_line_comment_spec.rb +249 -0
- data/spec/front_matter_parser/syntax_parser/single_line_comment_spec.rb +156 -0
- data/spec/front_matter_parser_spec.rb +3 -296
- data/spec/spec_helper.rb +7 -1
- data/spec/support/matcher.rb +8 -0
- metadata +86 -46
- data/spec/fixtures/example.coffee +0 -4
- data/spec/fixtures/example.erb +0 -6
- data/spec/fixtures/example.foo +0 -0
- data/spec/fixtures/example.haml +0 -5
- data/spec/fixtures/example.liquid +0 -6
- data/spec/fixtures/example.md +0 -4
- data/spec/fixtures/example.sass +0 -4
- data/spec/fixtures/example.scss +0 -4
- data/spec/fixtures/example.slim +0 -5
- data/spec/support/strings.rb +0 -41
- data/spec/support/syntaxs.rb +0 -14
- data/spec/support/utils.rb +0 -6
@@ -0,0 +1,111 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe FrontMatterParser::Parser do
|
6
|
+
let(:front_matter) { { 'title' => 'hello' } }
|
7
|
+
let(:content) { "Content\n" }
|
8
|
+
|
9
|
+
describe '#call' do
|
10
|
+
let(:string) do
|
11
|
+
<<~eos
|
12
|
+
<!--
|
13
|
+
---
|
14
|
+
title: hello
|
15
|
+
---
|
16
|
+
-->
|
17
|
+
Content
|
18
|
+
eos
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'parses using given parser' do
|
22
|
+
parser = described_class.new(FrontMatterParser::SyntaxParser::Html.new)
|
23
|
+
|
24
|
+
parsed = parser.call(string)
|
25
|
+
|
26
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'infers parser if it is a symbol' do
|
30
|
+
parser = described_class.new(:html)
|
31
|
+
|
32
|
+
parsed = parser.call(string)
|
33
|
+
|
34
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'parses front matter as an empty hash if it is not present' do
|
38
|
+
string = 'Content'
|
39
|
+
parser = described_class.new(:html)
|
40
|
+
|
41
|
+
parsed = parser.call(string)
|
42
|
+
|
43
|
+
expect(parsed.front_matter).to eq({})
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'parses content as the whole string if front matter is not present' do
|
47
|
+
string = 'Content'
|
48
|
+
parser = described_class.new(:html)
|
49
|
+
|
50
|
+
parsed = parser.call(string)
|
51
|
+
|
52
|
+
expect(parsed.content).to eq(string)
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'can specify custom front matter loader with loader: param' do
|
56
|
+
front_matter = { 'a' => 'b' }
|
57
|
+
parser = described_class.new(:html,
|
58
|
+
loader: ->(_string) { front_matter })
|
59
|
+
|
60
|
+
parsed = parser.call(string)
|
61
|
+
|
62
|
+
expect(parsed.front_matter).to eq(front_matter)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '::parse_file' do
|
67
|
+
# :reek:UtilityFunction
|
68
|
+
def pathname(file)
|
69
|
+
File.expand_path("../../fixtures/#{file}", __FILE__)
|
70
|
+
end
|
71
|
+
|
72
|
+
it 'parses inferring syntax from given pathname' do
|
73
|
+
pathname = pathname('example.html')
|
74
|
+
|
75
|
+
parsed = described_class.parse_file(
|
76
|
+
pathname
|
77
|
+
)
|
78
|
+
|
79
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'can specify custom parser through :syntax_parser param' do
|
83
|
+
pathname = pathname('example')
|
84
|
+
|
85
|
+
parsed = described_class.parse_file(
|
86
|
+
pathname, syntax_parser: FrontMatterParser::SyntaxParser::Html.new
|
87
|
+
)
|
88
|
+
|
89
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'can specify custom parser as symbol through :syntax_parser param' do
|
93
|
+
pathname = pathname('example')
|
94
|
+
|
95
|
+
parsed = described_class.parse_file(
|
96
|
+
pathname, syntax_parser: :html
|
97
|
+
)
|
98
|
+
|
99
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'can specify custom front matter loader with loader: param' do
|
103
|
+
pathname = pathname('example.html')
|
104
|
+
front_matter = { 'a' => 'b' }
|
105
|
+
parsed = described_class.parse_file(pathname,
|
106
|
+
loader: ->(_string) { front_matter })
|
107
|
+
|
108
|
+
expect(parsed.front_matter).to eq(front_matter)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe FrontMatterParser::SyntaxParser::IndentationComment do
|
6
|
+
subject(:parsed) { FrontMatterParser::Parser.new(syntax).call(string) }
|
7
|
+
|
8
|
+
let(:front_matter) { { 'title' => 'hello', 'author' => 'me' } }
|
9
|
+
let(:content) { "Content\n" }
|
10
|
+
|
11
|
+
context 'slim' do
|
12
|
+
let(:syntax) { :slim }
|
13
|
+
let(:string) do
|
14
|
+
<<~eos
|
15
|
+
/
|
16
|
+
---
|
17
|
+
title: hello
|
18
|
+
author: me
|
19
|
+
---
|
20
|
+
Content
|
21
|
+
eos
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'can parse it' do
|
25
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'haml' do
|
30
|
+
let(:syntax) { :haml }
|
31
|
+
let(:string) do
|
32
|
+
<<~eos
|
33
|
+
-#
|
34
|
+
---
|
35
|
+
title: hello
|
36
|
+
author: me
|
37
|
+
---
|
38
|
+
Content
|
39
|
+
eos
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'can parse it' do
|
43
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with space before comment delimiter' do
|
48
|
+
let(:syntax) { :slim }
|
49
|
+
let(:string) do
|
50
|
+
<<~eos
|
51
|
+
|
52
|
+
/
|
53
|
+
---
|
54
|
+
title: hello
|
55
|
+
author: me
|
56
|
+
---
|
57
|
+
Content
|
58
|
+
eos
|
59
|
+
end
|
60
|
+
|
61
|
+
it 'can parse it' do
|
62
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'with front matter starting in comment delimiter line' do
|
67
|
+
let(:syntax) { :slim }
|
68
|
+
let(:string) do
|
69
|
+
<<~eos
|
70
|
+
/---
|
71
|
+
title: hello
|
72
|
+
author: me
|
73
|
+
---
|
74
|
+
Content
|
75
|
+
eos
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'can parse it' do
|
79
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'with space before front matter' do
|
84
|
+
let(:syntax) { :slim }
|
85
|
+
let(:string) do
|
86
|
+
<<~eos
|
87
|
+
/
|
88
|
+
|
89
|
+
---
|
90
|
+
title: hello
|
91
|
+
author: me
|
92
|
+
---
|
93
|
+
Content
|
94
|
+
eos
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'can parse it' do
|
98
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'with space within front matter' do
|
103
|
+
let(:syntax) { :slim }
|
104
|
+
let(:string) do
|
105
|
+
<<~eos
|
106
|
+
/
|
107
|
+
---
|
108
|
+
title: hello
|
109
|
+
|
110
|
+
author: me
|
111
|
+
---
|
112
|
+
Content
|
113
|
+
eos
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'can parse it' do
|
117
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'with comment delimiter in the front matter' do
|
122
|
+
let(:syntax) { :slim }
|
123
|
+
let(:string) do
|
124
|
+
<<~eos
|
125
|
+
/
|
126
|
+
---
|
127
|
+
title: /hello
|
128
|
+
author: me
|
129
|
+
---
|
130
|
+
Content
|
131
|
+
eos
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'can parse it' do
|
135
|
+
front_matter = { 'title' => '/hello', 'author' => 'me' }
|
136
|
+
|
137
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'returns nil if no front matter is found' do
|
142
|
+
string = 'Content'
|
143
|
+
|
144
|
+
expect(FrontMatterParser::SyntaxParser::Slim.new.call(string)).to be_nil
|
145
|
+
end
|
146
|
+
end
|
@@ -0,0 +1,249 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe FrontMatterParser::SyntaxParser::MultiLineComment do
|
6
|
+
subject(:parsed) { FrontMatterParser::Parser.new(syntax).call(string) }
|
7
|
+
|
8
|
+
let(:front_matter) { { 'title' => 'hello', 'author' => 'me' } }
|
9
|
+
let(:content) { "Content\n" }
|
10
|
+
|
11
|
+
context 'html' do
|
12
|
+
let(:syntax) { :html }
|
13
|
+
let(:string) do
|
14
|
+
<<~eos
|
15
|
+
<!--
|
16
|
+
---
|
17
|
+
title: hello
|
18
|
+
author: me
|
19
|
+
---
|
20
|
+
-->
|
21
|
+
Content
|
22
|
+
eos
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'can parse it' do
|
26
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'erb' do
|
31
|
+
let(:syntax) { :erb }
|
32
|
+
let(:string) do
|
33
|
+
<<~eos
|
34
|
+
<%#
|
35
|
+
---
|
36
|
+
title: hello
|
37
|
+
author: me
|
38
|
+
---
|
39
|
+
%>
|
40
|
+
Content
|
41
|
+
eos
|
42
|
+
end
|
43
|
+
|
44
|
+
it 'can parse it' do
|
45
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'liquid' do
|
50
|
+
let(:syntax) { :liquid }
|
51
|
+
let(:string) do
|
52
|
+
<<~eos
|
53
|
+
{% comment %}
|
54
|
+
---
|
55
|
+
title: hello
|
56
|
+
author: me
|
57
|
+
---
|
58
|
+
{% endcomment %}
|
59
|
+
Content
|
60
|
+
eos
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'can parse it' do
|
64
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'md' do
|
69
|
+
let(:syntax) { :md }
|
70
|
+
let(:string) do
|
71
|
+
<<~eos
|
72
|
+
---
|
73
|
+
title: hello
|
74
|
+
author: me
|
75
|
+
---
|
76
|
+
Content
|
77
|
+
eos
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'can parse it' do
|
81
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'with space before start comment delimiter' do
|
86
|
+
let(:syntax) { :html }
|
87
|
+
let(:string) do
|
88
|
+
<<~eos
|
89
|
+
|
90
|
+
<!--
|
91
|
+
---
|
92
|
+
title: hello
|
93
|
+
author: me
|
94
|
+
---
|
95
|
+
-->
|
96
|
+
Content
|
97
|
+
eos
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'can parse it' do
|
101
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'with front matter starting in comment delimiter line' do
|
106
|
+
let(:syntax) { :html }
|
107
|
+
let(:string) do
|
108
|
+
<<~eos
|
109
|
+
<!-- ---
|
110
|
+
title: hello
|
111
|
+
author: me
|
112
|
+
---
|
113
|
+
-->
|
114
|
+
Content
|
115
|
+
eos
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'can parse it' do
|
119
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
context 'with space before front matter' do
|
124
|
+
let(:syntax) { :html }
|
125
|
+
let(:string) do
|
126
|
+
<<~eos
|
127
|
+
<!--
|
128
|
+
|
129
|
+
---
|
130
|
+
title: hello
|
131
|
+
author: me
|
132
|
+
---
|
133
|
+
-->
|
134
|
+
Content
|
135
|
+
eos
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'can parse it' do
|
139
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
context 'with space within front matter' do
|
144
|
+
let(:syntax) { :html }
|
145
|
+
let(:string) do
|
146
|
+
<<~eos
|
147
|
+
<!--
|
148
|
+
---
|
149
|
+
title: hello
|
150
|
+
|
151
|
+
author: me
|
152
|
+
---
|
153
|
+
-->
|
154
|
+
Content
|
155
|
+
eos
|
156
|
+
end
|
157
|
+
|
158
|
+
it 'can parse it' do
|
159
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'with space after front matter' do
|
164
|
+
let(:syntax) { :html }
|
165
|
+
let(:string) do
|
166
|
+
<<~eos
|
167
|
+
<!--
|
168
|
+
---
|
169
|
+
title: hello
|
170
|
+
author: me
|
171
|
+
---
|
172
|
+
|
173
|
+
-->
|
174
|
+
Content
|
175
|
+
eos
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'can parse it' do
|
179
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
context 'with space before end comment delimiter' do
|
184
|
+
let(:syntax) { :html }
|
185
|
+
let(:string) do
|
186
|
+
<<~eos
|
187
|
+
<!--
|
188
|
+
---
|
189
|
+
title: hello
|
190
|
+
author: me
|
191
|
+
---
|
192
|
+
-->
|
193
|
+
Content
|
194
|
+
eos
|
195
|
+
end
|
196
|
+
|
197
|
+
it 'can parse it' do
|
198
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
context 'with start comment delimiter in the front matter' do
|
203
|
+
let(:syntax) { :html }
|
204
|
+
let(:string) do
|
205
|
+
<<~eos
|
206
|
+
<!--
|
207
|
+
---
|
208
|
+
title: <!--hello
|
209
|
+
author: me
|
210
|
+
---
|
211
|
+
-->
|
212
|
+
Content
|
213
|
+
eos
|
214
|
+
end
|
215
|
+
|
216
|
+
it 'can parse it' do
|
217
|
+
front_matter = { 'title' => '<!--hello', 'author' => 'me' }
|
218
|
+
|
219
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
context 'with start and end comment delimiter in the front matter' do
|
224
|
+
let(:syntax) { :html }
|
225
|
+
let(:string) do
|
226
|
+
<<~eos
|
227
|
+
<!--
|
228
|
+
---
|
229
|
+
title: <!--hello-->
|
230
|
+
author: me
|
231
|
+
---
|
232
|
+
-->
|
233
|
+
Content
|
234
|
+
eos
|
235
|
+
end
|
236
|
+
|
237
|
+
it 'can parse it' do
|
238
|
+
front_matter = { 'title' => '<!--hello-->', 'author' => 'me' }
|
239
|
+
|
240
|
+
expect(parsed).to be_parsed_result_with(front_matter, content)
|
241
|
+
end
|
242
|
+
end
|
243
|
+
|
244
|
+
it 'returns nil if no front matter is found' do
|
245
|
+
string = 'Content'
|
246
|
+
|
247
|
+
expect(FrontMatterParser::SyntaxParser::Html.new.call(string)).to be_nil
|
248
|
+
end
|
249
|
+
end
|