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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 747db28088db295148c0d41fbcd79c9a861b900c
4
- data.tar.gz: 455ff93bddc82da6139f449e24b52010b257d3cb
3
+ metadata.gz: f4c2e7f42d072334636f3781de92e513279c0505
4
+ data.tar.gz: 06c3a80b9b53bedbb4c48b2d479f44478bf9d6b2
5
5
  SHA512:
6
- metadata.gz: 015bd32903bf893ab8a8a162b682b7fcb7197a9bd5f9a56cdec6db6056b7f88797d5278f0aa57e421e040442def75aeb3e691e3f4384cb2fead12c9a69b4b285
7
- data.tar.gz: 77405afc93d9ace168815d5f0b6f92d0e50dffc110aa74a07456afb9927e9b104fd2f88f9fa8876053cdcc59874f876611aec7eb92385042547763b058507ea1
6
+ metadata.gz: 2ccfb0f6c35751cd44f4b4140f14873390ddc38b400f83f7771a5665d4b81abdc56ea1b8825714101a6c5f1ffc39cecf5ccc4e3537081830de0fe7591f9cde24
7
+ data.tar.gz: 38dcc37664adaf5b4add747aa9958d5415a5c1833ce01b22aa501faca438306cf33cefb8a26088286042e52b51b4bba718c42fc25c3c24994ee408d2c7b72af0
data/.rspec CHANGED
@@ -1,2 +1 @@
1
- --format documentation
2
1
  --color
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. 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.
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
- title: Hello World
27
- category: Greetings
28
- ---
29
- Some actual content
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
- parsed = FrontMatterParser.parse_file('example.md')
34
- parsed.front_matter #=> {'title' => 'Hello World'}
35
- parsed.content #=> 'Some actual content'
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
- parsed['category'] #=> 'Greetings'
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 suppose that the front matter is within that syntax comments delimiters.
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
- title: Hello
50
- ---
51
- Content
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 it and you can simply do:
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
- title = FrontMatterParser.parse_file('example.haml')['title'] #=> 'Hello'
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 | | <% comment %> | <% endcomment %> |
78
+ | liquid | | {% comment %} | {% endcomment %} |
65
79
  | md | | | |
66
80
  | html | | &lt;!-- | --&gt; |
81
+ | erb | | &lt;%# | %&gt; |
67
82
  | coffee | # | | |
68
83
  | sass | // | | |
69
84
  | scss | // | | |
70
85
  </pre>
71
86
 
72
- You can provide your own by passing `autodetect: false` and options for single line comment delimiter (`:comment`) or start multiline comment (`:start_comment`) and end multiline comment (`:end_comment`) delimiters. If `:start_comment` is provided but it isn't `:end_comment`, then it is supposed that the multiline comment is ended by indentation.
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
- FrontMatterParser.parse_file('example.haml', autodetect: false, start_comment: '<!--', end_comment: '-->') # start and end multiline comment delimiters
75
- FrontMatterParser.parse_file('example.slim', autodetect: false, start_comment: '/!') # multiline comment closed by indentation
76
- FrontMatterParser.parse_file('example.foo', autodetect: false, comment: '#') # single line comments
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 its comment delimiters if needed:
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
- string = File.read('example.slim')
83
- FrontMatterParser.parse(string, start_comment: '/')
106
+ ```ruby
107
+ string = File.read('example.slim')
108
+ FrontMatterParser.parse(string, start_comment: '/!')
109
+ ```
84
110
 
85
111
  ## Contributing
86
112
 
@@ -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 used in FrontMatterParser known syntaxes. Keys are file extensions, and values are three elements array:
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, '<% comment %>', '<% endcomment %>'],
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. For comment options, see {COMMENT_DELIMITERS} values (but they are not limited to those for the known syntaxes).
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 end_comment option is provided but not start_comment
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
- raise(ArgumentError, "If you provide the `end_comment` option, you must provide also the `start_comment` option") if (opts[:end_comment] != nil and opts[:start_comment] == nil)
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(/^#{opts[:comment]}/, '')
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. If autodetect option is `true`, comment delimiters are guessed from the file extension. If it is `false` comment options are taken into consideration. See {COMMENT_DELIMITERS} for a list of known syntaxes and the comment delimiters values.
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 autodetect option is false, and start_comment option is provided but not end_comment
82
- # @raise [RuntimeError] If the syntax of the file (the extension) is not within the keys of {COMMENT_DELIMITERS}
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[:autodetect]
92
- ext = File.extname(pathname)[1 .. -1].to_sym
93
- raise(RuntimeError, "Comment delimiters for extension #{ext.to_s} not known. Please, call #parse_file without autodetect option and provide comment delimiters.") unless COMMENT_DELIMITERS.has_key?(ext)
94
- comment_delimiters = COMMENT_DELIMITERS[ext]
95
- opts[:comment] = comment_delimiters[0]
96
- opts[:start_comment] = comment_delimiters[1]
97
- opts[:end_comment] = comment_delimiters[2]
98
- end
99
- File.open(pathname) do |file|
100
- parse(file.read, comment: opts[:comment], start_comment: opts[:start_comment], end_comment: opts[:end_comment])
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
@@ -1,3 +1,3 @@
1
1
  module FrontMatterParser
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
File without changes
@@ -1,4 +1,4 @@
1
1
  # ---
2
- # title: Hello
2
+ # title: hello
3
3
  # ---
4
4
  Content
@@ -0,0 +1,6 @@
1
+ <%#
2
+ ---
3
+ title: hello
4
+ ---
5
+ %>
6
+ Content
@@ -1,5 +1,5 @@
1
1
  -#
2
2
  ---
3
- title: Hello
3
+ title: hello
4
4
  ---
5
5
  Content
@@ -1,6 +1,6 @@
1
1
  <!--
2
2
  ---
3
- title: Hello
3
+ title: hello
4
4
  ---
5
5
  -->
6
6
  Content
@@ -1,6 +1,6 @@
1
- { %comment% }
1
+ {% comment %}
2
2
  ---
3
- title: Hello
3
+ title: hello
4
4
  ---
5
- { %endcomment% }
5
+ {% endcomment %}
6
6
  Content
@@ -1,4 +1,4 @@
1
1
  ---
2
- title: Hello
2
+ title: hello
3
3
  ---
4
4
  Content
@@ -1,4 +1,4 @@
1
1
  // ---
2
- // title: Hello
2
+ // title: hello
3
3
  // ---
4
4
  Content
@@ -1,4 +1,4 @@
1
1
  // ---
2
- // title: Hello
2
+ // title: hello
3
3
  // ---
4
4
  Content
@@ -1,5 +1,5 @@
1
1
  /
2
2
  ---
3
- title: Hello
3
+ title: hello
4
4
  ---
5
5
  Content
@@ -1,7 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe FrontMatterParser do
4
- let(:sample) { {'title' => 'hello'} }
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(sample)
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("Content")
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(:string) { %Q(
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(sample)
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(:string) { %Q(Hello) }
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(string)
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 the end multiline comment delimiter is provided but not the start one" do
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
- string = %Q(
73
- <!--
74
- ---
75
- title: hello
76
- ---
77
- -->
78
- Content)
79
- expect {FrontMatterParser.parse(string, end_comment: '-->')}.to raise_error(ArgumentError)
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 autodetect is true" do
86
- {
87
- slim: ['slim', nil, '/', nil],
88
- coffee: ['coffee', '#', nil, nil],
89
- html: ['html', nil, '<!--', '-->'],
90
- haml: ['haml', nil, '-#', nil],
91
- liquid: ['liquid', nil, '<% comment %>', '<% endcomment %>'],
92
- sass: ['sass', '//', nil, nil],
93
- scss: ['scss', '//', nil, nil],
94
- md: ['md', nil, nil, nil],
95
- }.each_pair do |format, prop|
96
- it "can detect a #{format} file" do
97
- expect(FrontMatterParser).to receive(:parse).with(File.read(File.expand_path("../fixtures/example.#{prop[0]}", __FILE__)), comment: prop[1], start_comment: prop[2], end_comment: prop[3])
98
- FrontMatterParser.parse_file(File.expand_path("../fixtures/example.#{prop[0]}", __FILE__), autodetct: true)
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 the file extension is unknown" do
103
- it "raises a RuntimeError" do
104
- expect {FrontMatterParser.parse_file(File.expand_path('../fixtures/example.foo', __FILE__), autodetect: true)}.to raise_error(RuntimeError)
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 autodetect is false" do
110
- it "calls #parse with the content of the file and given comment delimiters" do
111
- expect(FrontMatterParser).to receive(:parse).with(File.read(File.expand_path('../fixtures/example.md', __FILE__)), comment: nil, start_comment: nil, end_comment: nil)
112
- FrontMatterParser.parse_file(File.expand_path('../fixtures/example.md', __FILE__), autodetect: false)
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(:sample) { {'title' => 'hello'} }
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(sample)
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(sample)
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(sample)
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(sample)
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(sample)
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(sample)
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(sample)
300
+ expect(FrontMatterParser.parse(string, start_comment: '/').front_matter).to eq(sample_fm)
191
301
  end
192
302
  end
@@ -1,2 +1,6 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
  require 'front_matter_parser'
3
+ require 'tempfile'
4
+ Dir["#{File.expand_path('../support', __FILE__)}/*.rb"].each do |file|
5
+ require file
6
+ end
@@ -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
@@ -0,0 +1,14 @@
1
+ # key is a human name, value is :syntax value or file extension
2
+ def syntaxs
3
+ {
4
+ slim: :slim,
5
+ 'coffee script' => :coffee,
6
+ html: :html,
7
+ haml: :haml,
8
+ liquid: :liquid,
9
+ sass: :sass,
10
+ scss: :scss,
11
+ md: :md,
12
+ erb: :erb,
13
+ }
14
+ end
@@ -0,0 +1,6 @@
1
+ def file_fixture(string)
2
+ file = Tempfile.new('foo')
3
+ file.write(string)
4
+ file.rewind
5
+ file.path
6
+ 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.1
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-03-17 00:00:00.000000000 Z
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
@@ -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')