front_matter_parser 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +0 -1
- data/README.md +52 -26
- data/lib/front_matter_parser.rb +37 -23
- data/lib/front_matter_parser/version.rb +1 -1
- data/spec/fixtures/example +0 -0
- data/spec/fixtures/example.coffee +1 -1
- data/spec/fixtures/example.erb +6 -0
- data/spec/fixtures/example.haml +1 -1
- data/spec/fixtures/example.html +1 -1
- data/spec/fixtures/example.liquid +3 -3
- data/spec/fixtures/example.md +1 -1
- data/spec/fixtures/example.sass +1 -1
- data/spec/fixtures/example.scss +1 -1
- data/spec/fixtures/example.slim +1 -1
- data/spec/front_matter_parser_spec.rb +167 -57
- data/spec/spec_helper.rb +4 -0
- data/spec/support/strings.rb +41 -0
- data/spec/support/syntaxs.rb +14 -0
- data/spec/support/utils.rb +6 -0
- metadata +13 -9
- data/bin/autospec +0 -16
- data/bin/rake +0 -16
- data/bin/rspec +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4c2e7f42d072334636f3781de92e513279c0505
|
4
|
+
data.tar.gz: 06c3a80b9b53bedbb4c48b2d479f44478bf9d6b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ccfb0f6c35751cd44f4b4140f14873390ddc38b400f83f7771a5665d4b81abdc56ea1b8825714101a6c5f1ffc39cecf5ccc4e3537081830de0fe7591f9cde24
|
7
|
+
data.tar.gz: 38dcc37664adaf5b4add747aa9958d5415a5c1833ce01b22aa501faca438306cf33cefb8a26088286042e52b51b4bba718c42fc25c3c24994ee408d2c7b72af0
|
data/.rspec
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# FrontMatterParser
|
2
2
|
|
3
|
-
FrontMatterParser is a library to parse files or strings with YAML front matters.
|
3
|
+
FrontMatterParser is a library to parse files or strings with YAML front matters. It can automatically detect the syntax of a file from its extension and it supposes that the front matter is marked as that syntax comments.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -8,6 +8,10 @@ Add this line to your application's Gemfile:
|
|
8
8
|
|
9
9
|
gem 'front_matter_parser'
|
10
10
|
|
11
|
+
or, to get the development version:
|
12
|
+
|
13
|
+
gem 'front_matter_parser', github: 'laMarciana/front_matter_parser'
|
14
|
+
|
11
15
|
And then execute:
|
12
16
|
|
13
17
|
$ bundle
|
@@ -22,37 +26,47 @@ Front matters must be between two lines with three dashes `---`.
|
|
22
26
|
|
23
27
|
Given a file `example.md`:
|
24
28
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
```md
|
30
|
+
---
|
31
|
+
title: Hello World
|
32
|
+
category: Greetings
|
33
|
+
---
|
34
|
+
Some actual content
|
35
|
+
```
|
30
36
|
|
31
37
|
You can parse it:
|
32
38
|
|
33
|
-
|
34
|
-
|
35
|
-
|
39
|
+
```ruby
|
40
|
+
parsed = FrontMatterParser.parse_file('example.md')
|
41
|
+
parsed.front_matter #=> {'title' => 'Hello World', 'category' => 'Greetings'}
|
42
|
+
parsed.content #=> 'Some actual content'
|
43
|
+
```
|
36
44
|
|
37
45
|
You can apply directly `[]` method to get a front matter value:
|
38
46
|
|
39
|
-
|
47
|
+
```ruby
|
48
|
+
parsed['category'] #=> 'Greetings'
|
49
|
+
```
|
40
50
|
|
41
51
|
### Syntax autodetection
|
42
52
|
|
43
|
-
`FrontMatterParser` detects the syntax of a file by its extension and
|
53
|
+
`FrontMatterParser` detects the syntax of a file by its extension and it supposes that the front matter is within that syntax comments delimiters.
|
44
54
|
|
45
55
|
Given a file `example.haml`:
|
46
56
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
57
|
+
```haml
|
58
|
+
-#
|
59
|
+
---
|
60
|
+
title: Hello
|
61
|
+
---
|
62
|
+
Content
|
63
|
+
```
|
52
64
|
|
53
|
-
The `-#` and the indentation enclose the front matter as a comment. `FrontMatterParser` is aware of
|
65
|
+
The `-#` and the indentation enclose the front matter as a comment. `FrontMatterParser` is aware of that, so you can simply do:
|
54
66
|
|
55
|
-
|
67
|
+
```ruby
|
68
|
+
title = FrontMatterParser.parse_file('example.haml')['title'] #=> 'Hello'
|
69
|
+
```
|
56
70
|
|
57
71
|
Following there is a relation of known syntaxes and their known comment delimiters:
|
58
72
|
|
@@ -61,26 +75,38 @@ Following there is a relation of known syntaxes and their known comment delimite
|
|
61
75
|
| ------ | ------------------- | ----------------------- | ---------------------- |
|
62
76
|
| haml | | -# | (indentation) |
|
63
77
|
| slim | | / | (indentation) |
|
64
|
-
| liquid | |
|
78
|
+
| liquid | | {% comment %} | {% endcomment %} |
|
65
79
|
| md | | | |
|
66
80
|
| html | | <!-- | --> |
|
81
|
+
| erb | | <%# | %> |
|
67
82
|
| coffee | # | | |
|
68
83
|
| sass | // | | |
|
69
84
|
| scss | // | | |
|
70
85
|
</pre>
|
71
86
|
|
72
|
-
|
87
|
+
For unknown syntaxes or alternative ones, you can provide your own single line comment delimiter with the `:comment` option, or multiline comment delimiters with `:start_comment` and `:end_comment`. If `:start_comment` is provided but it isn't `:end_comment`, then it is supposed that the multiline comment is ended by indentation.
|
73
88
|
|
74
|
-
|
75
|
-
|
76
|
-
|
89
|
+
```ruby
|
90
|
+
FrontMatterParser.parse_file('example.haml', start_comment: '<!--', end_comment: '-->') # start and end multiline comment delimiters
|
91
|
+
FrontMatterParser.parse_file('example.slim', start_comment: '/!') # multiline comment closed by indentation
|
92
|
+
FrontMatterParser.parse_file('example.foo', comment: '#') # single line comments
|
93
|
+
```
|
77
94
|
|
78
95
|
### Parsing a string
|
79
96
|
|
80
|
-
You can as well parse a string, providing manually
|
97
|
+
You can as well parse a string, providing manually the `syntax`:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
string = File.read('example.slim')
|
101
|
+
FrontMatterParser.parse(string, syntax: :slim)
|
102
|
+
```
|
103
|
+
|
104
|
+
or the comment delimiters:
|
81
105
|
|
82
|
-
|
83
|
-
|
106
|
+
```ruby
|
107
|
+
string = File.read('example.slim')
|
108
|
+
FrontMatterParser.parse(string, start_comment: '/!')
|
109
|
+
```
|
84
110
|
|
85
111
|
## Contributing
|
86
112
|
|
data/lib/front_matter_parser.rb
CHANGED
@@ -4,7 +4,7 @@ require "front_matter_parser/parsed"
|
|
4
4
|
|
5
5
|
# FrontMatterParser module is the entry point to parse strings or files with YAML front matters. When working with files, it can automatically detect the syntax of a file from its extension and it supposes that the front matter is marked as that syntax comments.
|
6
6
|
module FrontMatterParser
|
7
|
-
# {Hash {Symbol => Array}} Comments delimiters
|
7
|
+
# {Hash {Symbol => Array}} Comments delimiters for FrontMatterParser known syntaxes. Keys are file extensions for {FrontMatterParser.parse_file} or :syntax option values for {FrontMatterParser.parse}, and values are three elements array:
|
8
8
|
#
|
9
9
|
# * First element is single line comment delimiter.
|
10
10
|
# * Second element is the start multiline comment delimiter.
|
@@ -12,31 +12,44 @@ module FrontMatterParser
|
|
12
12
|
COMMENT_DELIMITERS = {
|
13
13
|
slim: [nil, '/', nil],
|
14
14
|
html: [nil, '<!--', '-->'],
|
15
|
+
erb: [nil, '<%#', '%>'],
|
15
16
|
coffee: ['#', nil, nil],
|
16
17
|
haml: [nil, '-#', nil],
|
17
|
-
liquid: [nil, '
|
18
|
+
liquid: [nil, '{% comment %}', '{% endcomment %}'],
|
18
19
|
sass: ['//', nil, nil],
|
19
20
|
scss: ['//', nil, nil],
|
20
21
|
md: [nil, nil, nil],
|
21
22
|
}
|
22
23
|
|
23
|
-
# Parses a string into a {Parsed} instance.
|
24
|
+
# Parses a string into a {Parsed} instance. The syntax of the string can be set with :syntax option. Otherwise, comment marks can be manually indicated with :comment, :start_comment and :end_comment options.
|
24
25
|
#
|
25
26
|
# @param string [String] The string to parse
|
26
27
|
# @param opts [Hash] Options
|
28
|
+
# @option opts [Symbol] :syntax The syntax used in the string. See {FrontMatterParser::COMMENT_DELIMITERS} for allowed values and the comment delimiters that are supposed.
|
27
29
|
# @option opts [String, nil] :comment Single line comment delimiter
|
28
30
|
# @option opts [String, nil] :start_comment Start multiline comment delimiter
|
29
|
-
# @option opts [String, nil] :end_comment End multiline comment delimiter
|
31
|
+
# @option opts [String, nil] :end_comment End multiline comment delimiter. If it is `nil` and :start_comment isn't, the multiline comment is supposed to be closed by indentation
|
30
32
|
# @return [Parsed]
|
31
|
-
# @raise [ArgumentError] If
|
33
|
+
# @raise [ArgumentError] If :syntax is not within {COMMENT_DELIMITERS} keys
|
34
|
+
# @raise [ArgumentError] If :end_comment option is given but not :start_comment
|
35
|
+
# @raise [ArgumentError] If :comment and :start_comment options are given
|
32
36
|
# @see COMMENT_DELIMITERS
|
33
37
|
def self.parse(string, opts = {})
|
34
38
|
opts = {
|
35
39
|
comment: nil,
|
36
40
|
start_comment: nil,
|
37
41
|
end_comment: nil,
|
42
|
+
syntax: nil,
|
38
43
|
}.merge(opts)
|
39
|
-
|
44
|
+
|
45
|
+
raise(ArgumentError, "If you provide :end_comment, you must also provide :start_comment") if (opts[:end_comment] != nil and opts[:start_comment] == nil)
|
46
|
+
raise(ArgumentError, "You can not provide :comment and :start_comment options at the same time") if (opts[:start_comment] != nil and opts[:comment] != nil)
|
47
|
+
|
48
|
+
if opts[:comment].nil? and opts[:start_comment].nil? and not opts[:syntax].nil?
|
49
|
+
raise(ArgumentError, "#{opts[:syntax]} syntax not known. Please call parse providing manually comment delimiters for that syntax.") unless COMMENT_DELIMITERS.has_key?(opts[:syntax])
|
50
|
+
opts[:comment], opts[:start_comment], opts[:end_comment] = COMMENT_DELIMITERS[opts[:syntax]]
|
51
|
+
end
|
52
|
+
|
40
53
|
parsed = Parsed.new
|
41
54
|
if matches = (string.match(/
|
42
55
|
# Start of string
|
@@ -59,7 +72,7 @@ module FrontMatterParser
|
|
59
72
|
# End of string
|
60
73
|
\z
|
61
74
|
/mx))
|
62
|
-
front_matter = matches[:front_matter].gsub(
|
75
|
+
front_matter = matches[:front_matter].gsub(/^[[:blank:]]*#{opts[:comment]}/, '')
|
63
76
|
parsed.front_matter = YAML.load(front_matter)
|
64
77
|
parsed.content = matches[:content]
|
65
78
|
else
|
@@ -69,35 +82,36 @@ module FrontMatterParser
|
|
69
82
|
parsed
|
70
83
|
end
|
71
84
|
|
72
|
-
# Parses a file into a {Parsed} instance.
|
85
|
+
# Parses a file into a {Parsed} instance. Syntax is automatically guessed from the file extension, unless :comment, :start_comment or :end_comment options are given. See {COMMENT_DELIMITERS} for a list of known extensions and the comment delimiters values that are supposed.
|
73
86
|
#
|
74
87
|
# @param pathname [String] The path to the file
|
75
88
|
# @param opts [Hash] Options
|
76
|
-
# @option opts [Boolean] :autodetect If it is true, FrontMatterParser uses the comment delimiters known for the syntax of the file and comment options are ignored. If it is false, comment options are taken into consideration.
|
77
89
|
# @option opts [String, nil] :comment Single line comment delimiter
|
78
90
|
# @option opts [String, nil] :start_comment Start multiline comment delimiter
|
79
|
-
# @option opts [String, nil] :end_comment End multiline comment delimiter
|
91
|
+
# @option opts [String, nil] :end_comment End multiline comment delimiter. If it is `nil`, the multiline comment is supposed to be closed by indentation.
|
80
92
|
# @return [Parsed]
|
81
|
-
# @raise [ArgumentError] If
|
82
|
-
# @raise [
|
93
|
+
# @raise [ArgumentError] If :start_comment option is provided but not :end_comment
|
94
|
+
# @raise [ArgumentError] If :comment and :start_comment options are both provided
|
95
|
+
# @raise [ArgumentError] If :end_comment is provided but :start_comment isn't
|
96
|
+
# @raise [RuntimeError] If the syntax of the file (the extension) is not within the keys of {COMMENT_DELIMITERS} or the file has no extension, and none of :comment, :start_comment or :end_comment are provided
|
83
97
|
# @see COMMENT_DELIMITERS
|
84
98
|
def self.parse_file(pathname, opts={})
|
85
99
|
opts = {
|
86
|
-
autodetect: true,
|
87
100
|
comment: nil,
|
88
101
|
start_comment: nil,
|
89
102
|
end_comment: nil,
|
90
103
|
}.merge(opts)
|
91
|
-
if opts[:
|
92
|
-
ext = File.extname(pathname)[1 .. -1]
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
104
|
+
if opts[:comment].nil? and opts[:start_comment].nil?
|
105
|
+
ext = File.extname(pathname)[1 .. -1]
|
106
|
+
ext = ext.to_sym unless ext.nil?
|
107
|
+
raise(RuntimeError, "Comment delimiters for extension #{ext.to_s} not known. Please, call #parse_file providing manually comment delimiters for that extension.") unless COMMENT_DELIMITERS.has_key?(ext)
|
108
|
+
File.open(pathname) do |file|
|
109
|
+
parse(file.read, syntax: ext)
|
110
|
+
end
|
111
|
+
else
|
112
|
+
File.open(pathname) do |file|
|
113
|
+
parse(file.read, comment: opts[:comment], start_comment: opts[:start_comment], end_comment: opts[:end_comment])
|
114
|
+
end
|
101
115
|
end
|
102
116
|
end
|
103
117
|
end
|
File without changes
|
data/spec/fixtures/example.haml
CHANGED
data/spec/fixtures/example.html
CHANGED
data/spec/fixtures/example.md
CHANGED
data/spec/fixtures/example.sass
CHANGED
data/spec/fixtures/example.scss
CHANGED
data/spec/fixtures/example.slim
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe FrontMatterParser do
|
4
|
-
let(:
|
4
|
+
let(:sample_fm) { {'title' => 'hello'} }
|
5
|
+
let(:sample_c) { 'Content' }
|
5
6
|
|
6
7
|
it 'has a version number' do
|
7
8
|
expect(FrontMatterParser::VERSION).to_not be_nil
|
@@ -9,32 +10,22 @@ describe FrontMatterParser do
|
|
9
10
|
|
10
11
|
describe "#parse" do
|
11
12
|
context "when the string has both front matter and content" do
|
12
|
-
let(:string) { %Q(
|
13
|
-
---
|
14
|
-
title: hello
|
15
|
-
---
|
16
|
-
Content) }
|
17
13
|
let(:parsed) { FrontMatterParser.parse(string) }
|
18
14
|
|
19
15
|
it "parses the front matter as a hash" do
|
20
|
-
expect(parsed.front_matter).to eq(
|
16
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
21
17
|
end
|
22
18
|
|
23
19
|
it "parses the content as a string" do
|
24
|
-
expect(parsed.content).to eq(
|
20
|
+
expect(parsed.content).to eq(sample_c)
|
25
21
|
end
|
26
22
|
end
|
27
23
|
|
28
24
|
context "when the string only has front matter" do
|
29
|
-
let(:
|
30
|
-
---
|
31
|
-
title: hello
|
32
|
-
---
|
33
|
-
) }
|
34
|
-
let(:parsed) { FrontMatterParser.parse(string) }
|
25
|
+
let(:parsed) { FrontMatterParser.parse(string_no_content) }
|
35
26
|
|
36
27
|
it "parses the front matter as a hash" do
|
37
|
-
expect(parsed.front_matter).to eq(
|
28
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
38
29
|
end
|
39
30
|
|
40
31
|
it "parses the content as an empty string" do
|
@@ -43,15 +34,14 @@ title: hello
|
|
43
34
|
end
|
44
35
|
|
45
36
|
context "when an empty front matter is supplied" do
|
46
|
-
let(:
|
47
|
-
let(:parsed) { FrontMatterParser.parse(string) }
|
37
|
+
let(:parsed) { FrontMatterParser.parse(string_no_front_matter) }
|
48
38
|
|
49
39
|
it "parses the front matter as an empty hash" do
|
50
40
|
expect(parsed.front_matter).to eq({})
|
51
41
|
end
|
52
42
|
|
53
43
|
it "parses the content as the whole string" do
|
54
|
-
expect(parsed.content).to eq(
|
44
|
+
expect(parsed.content).to eq(sample_c)
|
55
45
|
end
|
56
46
|
end
|
57
47
|
|
@@ -67,56 +57,176 @@ title: hello
|
|
67
57
|
end
|
68
58
|
end
|
69
59
|
|
70
|
-
context "when
|
60
|
+
context "when :comment option is given" do
|
61
|
+
it "takes it as the single line comment mark for the front matter" do
|
62
|
+
parsed = FrontMatterParser.parse(string_comment('#'), comment: '#')
|
63
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when :start_comment is given" do
|
67
|
+
it "raises an ArgumentError" do
|
68
|
+
expect { FrontMatterParser.parse(string_comment('#'), comment: '#', start_comment: '/')}.to raise_error ArgumentError
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "when :start_comment option is given" do
|
74
|
+
context "when :end_comment option is not given" do
|
75
|
+
it "takes :start_comment as the mark for a multiline comment closed by indentation for the front matter" do
|
76
|
+
parsed = FrontMatterParser.parse(string_start_comment('/'), start_comment: '/')
|
77
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context "when :end_comment option is provided" do
|
82
|
+
it "takes :start_comment and :end_comment as the multiline comment mark delimiters for the front matter" do
|
83
|
+
parsed = FrontMatterParser.parse(string_start_end_comment('<!--', '-->'), start_comment: '<!--', end_coment: '-->')
|
84
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when :end_comment option is given but :start_comment is not" do
|
71
90
|
it "raises an ArgumentError" do
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
91
|
+
expect {FrontMatterParser.parse(string_start_end_comment, end_comment: '-->')}.to raise_error(ArgumentError)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
context "when :syntax is given" do
|
96
|
+
context "when :comment and :start_comment are not given" do
|
97
|
+
syntaxs.each_pair do |name, value|
|
98
|
+
it "can detect a #{name} syntax when its value is #{value}" do
|
99
|
+
parsed = FrontMatterParser.parse(File.read(File.expand_path("../fixtures/example.#{value}", __FILE__)), syntax: value)
|
100
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
it "raises an ArgumentError if syntax is not whithin COMMENT_DELIMITERS keys" do
|
105
|
+
expect { FrontMatterParser.parse(string, syntax: :foo) }.to raise_error ArgumentError
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context "when :comment is given" do
|
110
|
+
it ":syntax is ignored" do
|
111
|
+
parsed = FrontMatterParser.parse(File.read(File.expand_path("../fixtures/example.coffee", __FILE__)), syntax: :slim, comment: '#' )
|
112
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context "when :start_comment is given" do
|
117
|
+
it ":syntax is ignored" do
|
118
|
+
parsed = FrontMatterParser.parse(File.read(File.expand_path("../fixtures/example.slim", __FILE__)), syntax: :coffee, start_comment: '/' )
|
119
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
120
|
+
end
|
80
121
|
end
|
81
122
|
end
|
82
123
|
end
|
83
124
|
|
84
125
|
describe "#parse_file" do
|
85
|
-
context "when
|
86
|
-
{
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
126
|
+
context "when the file has both front matter and content" do
|
127
|
+
let(:parsed) { FrontMatterParser.parse_file(file_fixture(string), comment: '') }
|
128
|
+
|
129
|
+
it "parses the front matter as a hash" do
|
130
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "parses the content as a string" do
|
134
|
+
expect(parsed.content).to eq(sample_c)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when the file only has front matter" do
|
139
|
+
let(:parsed) { FrontMatterParser.parse_file(file_fixture(string_no_content), comment: '') }
|
140
|
+
|
141
|
+
it "parses the front matter as a hash" do
|
142
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
143
|
+
end
|
144
|
+
|
145
|
+
it "parses the content as an empty string" do
|
146
|
+
expect(parsed.content).to eq('')
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "when the file has an empty front matter is supplied" do
|
151
|
+
let(:parsed) { FrontMatterParser.parse_file(file_fixture(string_no_front_matter), comment: '') }
|
152
|
+
|
153
|
+
it "parses the front matter as an empty hash" do
|
154
|
+
expect(parsed.front_matter).to eq({})
|
155
|
+
end
|
156
|
+
|
157
|
+
it "parses the content as the whole string" do
|
158
|
+
expect(parsed.content).to eq(sample_c)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context "when the file has no content" do
|
163
|
+
let(:parsed) { FrontMatterParser.parse_file(file_fixture(''), comment: '') }
|
164
|
+
|
165
|
+
it "parses the front matter as an empty hash" do
|
166
|
+
expect(parsed.front_matter).to eq({})
|
167
|
+
end
|
168
|
+
|
169
|
+
it "parses the content as an empty string" do
|
170
|
+
expect(parsed.content).to eq('')
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context "when :comment option is given" do
|
175
|
+
it "takes it as the single line comment mark for the front matter" do
|
176
|
+
parsed = FrontMatterParser.parse_file(file_fixture(string_comment('#')), comment: '#')
|
177
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
178
|
+
end
|
179
|
+
|
180
|
+
context "when :start_comment is given" do
|
181
|
+
it "raises an ArgumentError" do
|
182
|
+
expect { FrontMatterParser.parse_file(file_fixture(string_comment('#')), comment: '#', start_comment: '/')}.to raise_error ArgumentError
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
context "when :start_comment option is given" do
|
188
|
+
context "when :end_comment option is not given" do
|
189
|
+
it "takes :start_comment as the mark for a multiline comment closed by indentation for the front matter" do
|
190
|
+
parsed = FrontMatterParser.parse_file(file_fixture(string_start_comment('/')), start_comment: '/')
|
191
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
99
192
|
end
|
100
193
|
end
|
101
194
|
|
102
|
-
context "when
|
103
|
-
it "
|
104
|
-
|
195
|
+
context "when :end_comment option is provided" do
|
196
|
+
it "takes :start_comment and :end_comment as the multiline comment mark delimiters for the front matter" do
|
197
|
+
parsed = FrontMatterParser.parse_file(file_fixture(string_start_end_comment('<!--', '-->')), start_comment: '<!--', end_coment: '-->')
|
198
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
105
199
|
end
|
106
200
|
end
|
107
201
|
end
|
108
202
|
|
109
|
-
context "when
|
110
|
-
it "
|
111
|
-
expect
|
112
|
-
|
203
|
+
context "when :end_comment option is given but :start_comment is not" do
|
204
|
+
it "raises an ArgumentError" do
|
205
|
+
expect {FrontMatterParser.parse_file(file_fixture(string_start_end_comment), end_comment: '-->')}.to raise_error(ArgumentError)
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
context "when :comment and :start_comment are not given" do
|
210
|
+
syntaxs.each_pair do |name, value|
|
211
|
+
it "can detect a #{name} syntax file when its extension is #{value}" do
|
212
|
+
parsed = FrontMatterParser.parse_file(File.expand_path("../fixtures/example.#{value}", __FILE__))
|
213
|
+
expect(parsed.front_matter).to eq(sample_fm)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
it "raises an ArgumentError if the file extension is not whithin COMMENT_DELIMITERS keys" do
|
218
|
+
expect { FrontMatterParser.parse_file(File.expand_path("../fixtures/example.foo, __FILE__")) }.to raise_error RuntimeError
|
219
|
+
end
|
220
|
+
|
221
|
+
it "raises an ArgumentError if the file has no extension" do
|
222
|
+
expect { FrontMatterParser.parse_file(File.expand_path("../fixtures/example, __FILE__")) }.to raise_error RuntimeError
|
113
223
|
end
|
114
224
|
end
|
115
225
|
end
|
116
226
|
end
|
117
227
|
|
118
228
|
describe "the front matter" do
|
119
|
-
let(:
|
229
|
+
let(:sample_fm) { {'title' => 'hello'} }
|
120
230
|
|
121
231
|
it "can be indented" do
|
122
232
|
string = %Q(
|
@@ -124,7 +234,7 @@ describe "the front matter" do
|
|
124
234
|
title: hello
|
125
235
|
---
|
126
236
|
Content)
|
127
|
-
expect(FrontMatterParser.parse(string).front_matter).to eq(
|
237
|
+
expect(FrontMatterParser.parse(string).front_matter).to eq(sample_fm)
|
128
238
|
end
|
129
239
|
|
130
240
|
it "can have each line commented" do
|
@@ -133,7 +243,7 @@ Content)
|
|
133
243
|
#title: hello
|
134
244
|
#---
|
135
245
|
Content)
|
136
|
-
expect(FrontMatterParser.parse(string, comment: '#').front_matter).to eq(
|
246
|
+
expect(FrontMatterParser.parse(string, comment: '#').front_matter).to eq(sample_fm)
|
137
247
|
end
|
138
248
|
|
139
249
|
it "can be indented after the comment delimiter" do
|
@@ -142,7 +252,7 @@ Content)
|
|
142
252
|
# title: hello
|
143
253
|
# ---
|
144
254
|
Content)
|
145
|
-
expect(FrontMatterParser.parse(string, comment: '#').front_matter).to eq(
|
255
|
+
expect(FrontMatterParser.parse(string, comment: '#').front_matter).to eq(sample_fm)
|
146
256
|
end
|
147
257
|
|
148
258
|
it "can be between a multiline comment" do
|
@@ -153,7 +263,7 @@ title: hello
|
|
153
263
|
---
|
154
264
|
-->
|
155
265
|
Content)
|
156
|
-
expect(FrontMatterParser.parse(string, start_comment: '<!--', end_comment: '-->').front_matter).to eq(
|
266
|
+
expect(FrontMatterParser.parse(string, start_comment: '<!--', end_comment: '-->').front_matter).to eq(sample_fm)
|
157
267
|
end
|
158
268
|
|
159
269
|
it "can have the multiline comment delimiters indented" do
|
@@ -164,7 +274,7 @@ Content)
|
|
164
274
|
---
|
165
275
|
-->
|
166
276
|
Content)
|
167
|
-
expect(FrontMatterParser.parse(string, start_comment: '<!--', end_comment: '-->').front_matter).to eq(
|
277
|
+
expect(FrontMatterParser.parse(string, start_comment: '<!--', end_comment: '-->').front_matter).to eq(sample_fm)
|
168
278
|
end
|
169
279
|
|
170
280
|
it "can have empty lines between the multiline comment delimiters and the front matter" do
|
@@ -174,10 +284,10 @@ Content)
|
|
174
284
|
---
|
175
285
|
title: hello
|
176
286
|
---
|
177
|
-
|
287
|
+
|
178
288
|
-->
|
179
289
|
Content)
|
180
|
-
expect(FrontMatterParser.parse(string, start_comment: '<!--', end_comment: '-->').front_matter).to eq(
|
290
|
+
expect(FrontMatterParser.parse(string, start_comment: '<!--', end_comment: '-->').front_matter).to eq(sample_fm)
|
181
291
|
end
|
182
292
|
|
183
293
|
it "can have multiline comment delimited by indentation" do
|
@@ -187,6 +297,6 @@ Content)
|
|
187
297
|
title: hello
|
188
298
|
---
|
189
299
|
Content)
|
190
|
-
expect(FrontMatterParser.parse(string, start_comment: '/').front_matter).to eq(
|
300
|
+
expect(FrontMatterParser.parse(string, start_comment: '/').front_matter).to eq(sample_fm)
|
191
301
|
end
|
192
302
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
def string
|
2
|
+
"---
|
3
|
+
title: hello
|
4
|
+
---
|
5
|
+
Content"
|
6
|
+
end
|
7
|
+
|
8
|
+
def string_no_front_matter
|
9
|
+
"Content"
|
10
|
+
end
|
11
|
+
|
12
|
+
def string_no_content
|
13
|
+
"---
|
14
|
+
title: hello
|
15
|
+
---
|
16
|
+
"
|
17
|
+
end
|
18
|
+
|
19
|
+
def string_comment(comment)
|
20
|
+
"#{comment} ---
|
21
|
+
#{comment} title: hello
|
22
|
+
#{comment} ---
|
23
|
+
Content"
|
24
|
+
end
|
25
|
+
|
26
|
+
def string_start_comment(start_comment)
|
27
|
+
"#{start_comment}
|
28
|
+
---
|
29
|
+
title: hello
|
30
|
+
---
|
31
|
+
Content"
|
32
|
+
end
|
33
|
+
|
34
|
+
def string_start_end_comment(start_comment, end_comment)
|
35
|
+
"#{start_comment}
|
36
|
+
---
|
37
|
+
title: hello
|
38
|
+
---
|
39
|
+
#{end_comment}
|
40
|
+
Content"
|
41
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: front_matter_parser
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- marc
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -62,10 +62,7 @@ description: Library to parse files or strings with YAML front matters with synt
|
|
62
62
|
autodetection.
|
63
63
|
email:
|
64
64
|
- marc@lamarciana.com
|
65
|
-
executables:
|
66
|
-
- autospec
|
67
|
-
- rake
|
68
|
-
- rspec
|
65
|
+
executables: []
|
69
66
|
extensions: []
|
70
67
|
extra_rdoc_files: []
|
71
68
|
files:
|
@@ -80,14 +77,13 @@ files:
|
|
80
77
|
- LICENSE.txt
|
81
78
|
- README.md
|
82
79
|
- Rakefile
|
83
|
-
- bin/autospec
|
84
|
-
- bin/rake
|
85
|
-
- bin/rspec
|
86
80
|
- front_matter_parser.gemspec
|
87
81
|
- lib/front_matter_parser.rb
|
88
82
|
- lib/front_matter_parser/parsed.rb
|
89
83
|
- lib/front_matter_parser/version.rb
|
84
|
+
- spec/fixtures/example
|
90
85
|
- spec/fixtures/example.coffee
|
86
|
+
- spec/fixtures/example.erb
|
91
87
|
- spec/fixtures/example.foo
|
92
88
|
- spec/fixtures/example.haml
|
93
89
|
- spec/fixtures/example.html
|
@@ -99,6 +95,9 @@ files:
|
|
99
95
|
- spec/front_matter_parser/parsed_spec.rb
|
100
96
|
- spec/front_matter_parser_spec.rb
|
101
97
|
- spec/spec_helper.rb
|
98
|
+
- spec/support/strings.rb
|
99
|
+
- spec/support/syntaxs.rb
|
100
|
+
- spec/support/utils.rb
|
102
101
|
homepage: https://github.com/laMarciana/front_matter_parser
|
103
102
|
licenses:
|
104
103
|
- LGPL3
|
@@ -127,7 +126,9 @@ summary: FrontMatterParser is a library to parse files or strings with YAML fron
|
|
127
126
|
from its extension and it supposes that the front matter is marked as that syntax
|
128
127
|
comments.
|
129
128
|
test_files:
|
129
|
+
- spec/fixtures/example
|
130
130
|
- spec/fixtures/example.coffee
|
131
|
+
- spec/fixtures/example.erb
|
131
132
|
- spec/fixtures/example.foo
|
132
133
|
- spec/fixtures/example.haml
|
133
134
|
- spec/fixtures/example.html
|
@@ -139,3 +140,6 @@ test_files:
|
|
139
140
|
- spec/front_matter_parser/parsed_spec.rb
|
140
141
|
- spec/front_matter_parser_spec.rb
|
141
142
|
- spec/spec_helper.rb
|
143
|
+
- spec/support/strings.rb
|
144
|
+
- spec/support/syntaxs.rb
|
145
|
+
- spec/support/utils.rb
|
data/bin/autospec
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
# This file was generated by Bundler.
|
4
|
-
#
|
5
|
-
# The application 'autospec' is installed as part of a gem, and
|
6
|
-
# this file is here to facilitate running it.
|
7
|
-
#
|
8
|
-
|
9
|
-
require 'pathname'
|
10
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
-
Pathname.new(__FILE__).realpath)
|
12
|
-
|
13
|
-
require 'rubygems'
|
14
|
-
require 'bundler/setup'
|
15
|
-
|
16
|
-
load Gem.bin_path('rspec-core', 'autospec')
|
data/bin/rake
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
# This file was generated by Bundler.
|
4
|
-
#
|
5
|
-
# The application 'rake' is installed as part of a gem, and
|
6
|
-
# this file is here to facilitate running it.
|
7
|
-
#
|
8
|
-
|
9
|
-
require 'pathname'
|
10
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
-
Pathname.new(__FILE__).realpath)
|
12
|
-
|
13
|
-
require 'rubygems'
|
14
|
-
require 'bundler/setup'
|
15
|
-
|
16
|
-
load Gem.bin_path('rake', 'rake')
|
data/bin/rspec
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
#
|
3
|
-
# This file was generated by Bundler.
|
4
|
-
#
|
5
|
-
# The application 'rspec' is installed as part of a gem, and
|
6
|
-
# this file is here to facilitate running it.
|
7
|
-
#
|
8
|
-
|
9
|
-
require 'pathname'
|
10
|
-
ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
|
11
|
-
Pathname.new(__FILE__).realpath)
|
12
|
-
|
13
|
-
require 'rubygems'
|
14
|
-
require 'bundler/setup'
|
15
|
-
|
16
|
-
load Gem.bin_path('rspec-core', 'rspec')
|