front_matter_parser 0.0.4 → 1.0.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.
Files changed (49) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +19 -0
  3. data/.gitignore +1 -0
  4. data/.rspec +1 -0
  5. data/.rubocop.yml +50 -0
  6. data/.travis.yml +19 -1
  7. data/CHANGELOG.md +30 -0
  8. data/Dockerfile +5 -0
  9. data/Gemfile +0 -2
  10. data/README.md +76 -38
  11. data/Rakefile +5 -3
  12. data/bin/console +15 -0
  13. data/bin/setup +8 -0
  14. data/docker-compose.yml +12 -0
  15. data/front_matter_parser.gemspec +12 -6
  16. data/lib/front_matter_parser.rb +10 -114
  17. data/lib/front_matter_parser/loader.rb +11 -0
  18. data/lib/front_matter_parser/loader/yaml.rb +26 -0
  19. data/lib/front_matter_parser/parsed.rb +25 -14
  20. data/lib/front_matter_parser/parser.rb +82 -0
  21. data/lib/front_matter_parser/syntax_parser.rb +28 -0
  22. data/lib/front_matter_parser/syntax_parser/factorizable.rb +30 -0
  23. data/lib/front_matter_parser/syntax_parser/indentation_comment.rb +49 -0
  24. data/lib/front_matter_parser/syntax_parser/multi_line_comment.rb +50 -0
  25. data/lib/front_matter_parser/syntax_parser/single_line_comment.rb +64 -0
  26. data/lib/front_matter_parser/version.rb +3 -1
  27. data/spec/fixtures/example +6 -0
  28. data/spec/front_matter_parser/loader/yaml_spec.rb +24 -0
  29. data/spec/front_matter_parser/parsed_spec.rb +11 -21
  30. data/spec/front_matter_parser/parser_spec.rb +111 -0
  31. data/spec/front_matter_parser/syntax_parser/indentation_comment_spec.rb +166 -0
  32. data/spec/front_matter_parser/syntax_parser/multi_line_comment_spec.rb +267 -0
  33. data/spec/front_matter_parser/syntax_parser/single_line_comment_spec.rb +175 -0
  34. data/spec/front_matter_parser_spec.rb +3 -296
  35. data/spec/spec_helper.rb +9 -3
  36. data/spec/support/matcher.rb +8 -0
  37. metadata +110 -46
  38. data/spec/fixtures/example.coffee +0 -4
  39. data/spec/fixtures/example.erb +0 -6
  40. data/spec/fixtures/example.foo +0 -0
  41. data/spec/fixtures/example.haml +0 -5
  42. data/spec/fixtures/example.liquid +0 -6
  43. data/spec/fixtures/example.md +0 -4
  44. data/spec/fixtures/example.sass +0 -4
  45. data/spec/fixtures/example.scss +0 -4
  46. data/spec/fixtures/example.slim +0 -5
  47. data/spec/support/strings.rb +0 -41
  48. data/spec/support/syntaxs.rb +0 -14
  49. 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
+ <<~STRING
12
+ <!--
13
+ ---
14
+ title: hello
15
+ ---
16
+ -->
17
+ Content
18
+ STRING
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,166 @@
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 'when syntax is slim' do
12
+ let(:syntax) { :slim }
13
+ let(:string) do
14
+ <<~STRING
15
+ /
16
+ ---
17
+ title: hello
18
+ author: me
19
+ ---
20
+ Content
21
+ STRING
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 'when syntax is haml' do
30
+ let(:syntax) { :haml }
31
+ let(:string) do
32
+ <<~STRING
33
+ -#
34
+ ---
35
+ title: hello
36
+ author: me
37
+ ---
38
+ Content
39
+ STRING
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
+ <<~STRING
51
+
52
+ /
53
+ ---
54
+ title: hello
55
+ author: me
56
+ ---
57
+ Content
58
+ STRING
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
+ <<~STRING
70
+ /---
71
+ title: hello
72
+ author: me
73
+ ---
74
+ Content
75
+ STRING
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
+ <<~STRING
87
+ /
88
+
89
+ ---
90
+ title: hello
91
+ author: me
92
+ ---
93
+ Content
94
+ STRING
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
+ <<~STRING
106
+ /
107
+ ---
108
+ title: hello
109
+
110
+ author: me
111
+ ---
112
+ Content
113
+ STRING
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
+ <<~STRING
125
+ /
126
+ ---
127
+ title: /hello
128
+ author: me
129
+ ---
130
+ Content
131
+ STRING
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
+ context 'with front matter delimiter chars in the content' do
142
+ let(:syntax) { :slim }
143
+ let(:string) do
144
+ <<~STRING
145
+ /
146
+ ---
147
+ title: hello
148
+ ---
149
+ Content
150
+ ---
151
+ STRING
152
+ end
153
+
154
+ it 'is not greedy' do
155
+ front_matter = { 'title' => 'hello' }
156
+
157
+ expect(parsed).to be_parsed_result_with(front_matter, "Content\n---\n")
158
+ end
159
+ end
160
+
161
+ it 'returns nil if no front matter is found' do
162
+ string = 'Content'
163
+
164
+ expect(FrontMatterParser::SyntaxParser::Slim.new.call(string)).to be_nil
165
+ end
166
+ end
@@ -0,0 +1,267 @@
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 'when syntax is html' do
12
+ let(:syntax) { :html }
13
+ let(:string) do
14
+ <<~STRING
15
+ <!--
16
+ ---
17
+ title: hello
18
+ author: me
19
+ ---
20
+ -->
21
+ Content
22
+ STRING
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 'when syntax is erb' do
31
+ let(:syntax) { :erb }
32
+ let(:string) do
33
+ <<~STRING
34
+ <%#
35
+ ---
36
+ title: hello
37
+ author: me
38
+ ---
39
+ %>
40
+ Content
41
+ STRING
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 'when syntax is liquid' do
50
+ let(:syntax) { :liquid }
51
+ let(:string) do
52
+ <<~STRING
53
+ {% comment %}
54
+ ---
55
+ title: hello
56
+ author: me
57
+ ---
58
+ {% endcomment %}
59
+ Content
60
+ STRING
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 'when syntax is md' do
69
+ let(:syntax) { :md }
70
+ let(:string) do
71
+ <<~STRING
72
+ ---
73
+ title: hello
74
+ author: me
75
+ ---
76
+ Content
77
+ STRING
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
+ <<~STRING
89
+
90
+ <!--
91
+ ---
92
+ title: hello
93
+ author: me
94
+ ---
95
+ -->
96
+ Content
97
+ STRING
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
+ <<~STRING
109
+ <!-- ---
110
+ title: hello
111
+ author: me
112
+ ---
113
+ -->
114
+ Content
115
+ STRING
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
+ <<~STRING
127
+ <!--
128
+
129
+ ---
130
+ title: hello
131
+ author: me
132
+ ---
133
+ -->
134
+ Content
135
+ STRING
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
+ <<~STRING
147
+ <!--
148
+ ---
149
+ title: hello
150
+
151
+ author: me
152
+ ---
153
+ -->
154
+ Content
155
+ STRING
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
+ <<~STRING
167
+ <!--
168
+ ---
169
+ title: hello
170
+ author: me
171
+ ---
172
+
173
+ -->
174
+ Content
175
+ STRING
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
+ <<~STRING
187
+ <!--
188
+ ---
189
+ title: hello
190
+ author: me
191
+ ---
192
+ -->
193
+ Content
194
+ STRING
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
+ <<~STRING
206
+ <!--
207
+ ---
208
+ title: <!--hello
209
+ author: me
210
+ ---
211
+ -->
212
+ Content
213
+ STRING
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
+ <<~STRING
227
+ <!--
228
+ ---
229
+ title: <!--hello-->
230
+ author: me
231
+ ---
232
+ -->
233
+ Content
234
+ STRING
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
+ context 'with front matter delimiter chars in the content' do
245
+ let(:syntax) { :md }
246
+ let(:string) do
247
+ <<~STRING
248
+ ---
249
+ title: hello
250
+ ---
251
+ Content---
252
+ STRING
253
+ end
254
+
255
+ it 'is not greedy' do
256
+ front_matter = { 'title' => 'hello' }
257
+
258
+ expect(parsed).to be_parsed_result_with(front_matter, "Content---\n")
259
+ end
260
+ end
261
+
262
+ it 'returns nil if no front matter is found' do
263
+ string = 'Content'
264
+
265
+ expect(FrontMatterParser::SyntaxParser::Html.new.call(string)).to be_nil
266
+ end
267
+ end