metaheader 1.3beta1 → 1.3beta2

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: b2785ed7e70b818e49c72e9f8d8dfa868f382936
4
- data.tar.gz: 69186b6be145a432dc9dc81efbc1d86f9c6b8c1c
3
+ metadata.gz: 3eb042274d2edc78769525062d7cd315fad33275
4
+ data.tar.gz: feb8ef5aff9a4c9829363319b3ab7df601bae43b
5
5
  SHA512:
6
- metadata.gz: 5cda458f1ad6a0a11c97018655aae2b66656b497316c35826d3f6567eabf7f84482f12a3591f8bd163ce80c40c332db3c211849f57e98afb7d7e8e901305e61b
7
- data.tar.gz: 0bfe9167f00907300ceccadcc662287081036e70b251f637209c11048932f10fbe99fe09eaf7852c66a7bb5f67862a86400f72dd5b0f0dbecdb8380f7154aed1
6
+ metadata.gz: b53340aa50e7f0468e55a217a29eeb6089b39fadc01327836fd3fc214688507d0e8a57c9c061d2a884c8a720661253381421bdd0a46ee84793d65c48823da775
7
+ data.tar.gz: a7c9be01c42161999999ecf46b33343c7314ec6116a6719d87843dc298e29443d3f0412c5bb655be266808f0a05e75356d43e4b01a6429a4da651a25d3083a4d
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'pry'
4
+
3
5
  gemspec
@@ -1,4 +1,4 @@
1
1
  class MetaHeader
2
2
  # MetaHeader's version
3
- VERSION = '1.3beta1'.freeze
3
+ VERSION = '1.3beta2'.freeze
4
4
  end
data/lib/metaheader.rb CHANGED
@@ -43,7 +43,7 @@ class MetaHeader
43
43
  # @param path [String] path to the file to be read
44
44
  # @return [MetaHeader]
45
45
  def self.from_file(path)
46
- self.new File.read(path)
46
+ File.open(path) {|file| self.new file }
47
47
  end
48
48
 
49
49
  # Construct a new MetaHeader object or return the object untouched
@@ -58,23 +58,23 @@ class MetaHeader
58
58
  end
59
59
 
60
60
  # Parse every tags found in input up to the first newline.
61
- # @param input [String]
61
+ # @param input [String, IO]
62
62
  def initialize(input)
63
63
  @strict = false
64
64
  @data = {}
65
65
 
66
66
  @last_key = nil
67
+ @empty_lines = 0
67
68
 
68
- input = input.encode universal_newline: true
69
- input.each_line {|line|
70
- if line.strip.empty?
71
- break
72
- else
73
- parse line
74
- end
75
- }
69
+ unless input.is_a? IO
70
+ input = StringIO.new input.encode universal_newline: true
71
+ end
72
+
73
+ input.each_line {|line| break unless parse line }
76
74
 
77
75
  Parser.each {|klass|
76
+ input.rewind
77
+
78
78
  parser = klass.new
79
79
  parser.instance_variable_set :@mh, self
80
80
  parser.parse input
@@ -198,8 +198,8 @@ private
198
198
  Tag = Struct.new :name, :value
199
199
 
200
200
  REGEX = /\A(?<prefix>.*?)
201
- (?:@(?<key>\w+)|(?<key>[\w][\w\s]*?)\s*:)
202
- (?:\s+(?<value>[^\n]+))?
201
+ (?:@(?<key>\w+)|(?<key>[\w][\w\s]*?)\s*(?<alt>:))
202
+ (?:\s*(?<value>[^\n]+))?
203
203
  \Z/x.freeze
204
204
 
205
205
  def parse(line)
@@ -208,7 +208,7 @@ private
208
208
  # multiline value must have the same line prefix as the key
209
209
  if @last_key && line.start_with?(@last_prefix.rstrip)
210
210
  if append line
211
- return
211
+ return true
212
212
  else
213
213
  @last_key = nil
214
214
  end
@@ -216,7 +216,9 @@ private
216
216
 
217
217
  line.rstrip!
218
218
 
219
- return unless match = REGEX.match(line)
219
+ return false if @empty_lines > 0
220
+ return !line.empty? unless match = REGEX.match(line)
221
+ return true if match[:alt] && match[:value].nil?
220
222
 
221
223
  # single line
222
224
  @last_prefix = match[:prefix]
@@ -231,8 +233,10 @@ private
231
233
  end
232
234
 
233
235
  def append(line)
234
- if line.rstrip == @last_prefix.rstrip
236
+ prefix = line.rstrip
237
+ if prefix == @last_prefix.rstrip
235
238
  @line_breaks += 1
239
+ @empty_lines += 1 if prefix.empty?
236
240
  return true # add the line break later
237
241
  elsif line.start_with? @last_prefix
238
242
  mline = line[@last_prefix.size..-1]
@@ -248,7 +252,7 @@ private
248
252
 
249
253
  @line_breaks += 1 unless tag.value.empty?
250
254
  tag.value += "\n" * @line_breaks
251
- @line_breaks = 0
255
+ @line_breaks = @empty_lines = 0
252
256
 
253
257
  tag.value += stripped
254
258
  stripped
data/test/test_parser.rb CHANGED
@@ -22,7 +22,7 @@ class CustomParser < MetaHeader::Parser
22
22
 
23
23
  header[:hello] = header[:hello].to_s * 2
24
24
 
25
- @@input = input
25
+ @@input = input.read
26
26
  @@instance = header
27
27
  @@called = true
28
28
  end
@@ -85,8 +85,8 @@ class TestParser < MiniTest::Test
85
85
 
86
86
  def test_break_at_empty_line
87
87
  mh = MetaHeader.new <<-IN
88
- -- @hello world
89
-
88
+ @hello world
89
+ \x20
90
90
  @chunky bacon
91
91
  IN
92
92
 
@@ -114,6 +114,16 @@ class TestParser < MiniTest::Test
114
114
  assert_equal 'world', mh[:hello]
115
115
  end
116
116
 
117
+ def test_empty_prefixed_line
118
+ mh = MetaHeader.new <<-IN
119
+ -- @first
120
+ --
121
+ -- @second
122
+ IN
123
+
124
+ refute_nil mh[:second]
125
+ end
126
+
117
127
  def test_multiline
118
128
  mh = MetaHeader.new <<-IN
119
129
  @test Lorem
@@ -187,7 +197,7 @@ class TestParser < MiniTest::Test
187
197
  assert_equal "true\ntest", mh[:test]
188
198
  end
189
199
 
190
- def test_multiline_empty_line
200
+ def test_multiline_empty_line_prefix
191
201
  mh = MetaHeader.new <<-IN
192
202
  --@test
193
203
  -- Hello
@@ -199,7 +209,7 @@ class TestParser < MiniTest::Test
199
209
  IN
200
210
 
201
211
  assert_equal "Hello\n\nWorld", mh[:test]
202
- assert_equal 'bacon', mh[:chunky] # no leading newline
212
+ assert_equal 'bacon', mh[:chunky]
203
213
  end
204
214
 
205
215
  def test_multiline_empty_line_space_prefix
@@ -213,6 +223,33 @@ class TestParser < MiniTest::Test
213
223
  assert_equal "Hello\n\nWorld", mh[:test]
214
224
  end
215
225
 
226
+ def test_multiline_empty_line
227
+ mh = MetaHeader.new <<-IN
228
+ @test
229
+ Hello
230
+
231
+ World
232
+ @chunky bacon
233
+
234
+ @foo
235
+ IN
236
+
237
+ assert_equal "Hello\n\nWorld", mh[:test]
238
+ assert_equal 'bacon', mh[:chunky]
239
+ assert_nil mh[:foo]
240
+ end
241
+
242
+ def test_multiline_break_at_empty_line
243
+ mh = MetaHeader.new <<-IN
244
+ -- @hello world
245
+
246
+ @chunky bacon
247
+ IN
248
+
249
+ assert_equal 'world', mh[:hello]
250
+ assert_nil mh[:chunky]
251
+ end
252
+
216
253
  def test_read_file
217
254
  path = File.expand_path '../input/basic_tag', __FILE__
218
255
  mh = MetaHeader.from_file path
@@ -247,6 +284,16 @@ class TestParser < MiniTest::Test
247
284
  assert_equal Hash[key_test: 'Value'], mh.to_h
248
285
  end
249
286
 
287
+ def test_alternate_syntax_compact
288
+ mh = MetaHeader.new 'Key Test:Value'
289
+ assert_equal Hash[key_test: 'Value'], mh.to_h
290
+ end
291
+
292
+ def test_alternate_syntax_no_value
293
+ mh = MetaHeader.new 'Key Test:'
294
+ assert_equal Hash.new, mh.to_h
295
+ end
296
+
250
297
  def test_inspect
251
298
  mh = MetaHeader.new '@hello world'
252
299
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metaheader
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3beta1
4
+ version: 1.3beta2
5
5
  platform: ruby
6
6
  authors:
7
7
  - cfillion
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-11 00:00:00.000000000 Z
11
+ date: 2016-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler