maliq 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
+ require "enumerator"
2
+
1
3
  module Maliq
2
4
  module FileUtils
3
- SPLIT_MARKER = /^<<<---\s*([\w\.]+?)\s*--->>>\n/
5
+ SPLIT_MARKER = /^<<<---(.*)--->>>\n/
4
6
 
5
7
  # Retrieve Yaml Front Matter from text.
6
8
  # Returns [yfm, text]
@@ -13,16 +15,28 @@ module Maliq
13
15
  return yfm, text
14
16
  end
15
17
 
18
+ # Split a file with SPLIT_MARKER.
19
+ # Returns a Hash of filename key with its content.
16
20
  def split(path, marker=nil)
17
21
  marker ||= SPLIT_MARKER
18
22
  content = File.read(path)
19
23
  filename = File.basename(path, '.*')
20
24
  yfm, content = retrieveYFM(content)
21
- contents = ([filename] + content.split(marker)).to_hash
22
- contents.with({}) { |(fname, text), h| h[fname] = yfm + text }
25
+ contents = [filename] + content.split(marker)
26
+ prev_name = filename
27
+ contents.each_slice(2).with({}) do |(fname, text), h|
28
+ fname = prev_name = create_filename(prev_name) if fname.strip.empty?
29
+ h[fname.strip] = yfm + text
30
+ end
23
31
  end
24
32
 
25
- module_function :split, :retrieveYFM
33
+ # create filename from previous filename with sequence number.
34
+ def create_filename(prev_name)
35
+ prev_name[/\d+$/] ? prev_name.next : prev_name + '02'
36
+ end
37
+ private :create_filename
38
+
39
+ module_function :split, :retrieveYFM, :create_filename
26
40
  end
27
41
  end
28
42
 
@@ -25,10 +25,4 @@ class Hash
25
25
  end
26
26
  end
27
27
 
28
- class Array
29
- def to_hash
30
- Hash[ *self ]
31
- end
32
- end
33
-
34
28
  Enumerable.send(:alias_method, :with, :each_with_object)
data/lib/maliq/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Maliq
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -99,5 +99,69 @@ describe Maliq::FileUtils do
99
99
 
100
100
  it { @f.should == {'tmp' => @ch1, 'chapter02' => @ch2, 'chapter03' => @ch3} }
101
101
  end
102
+
103
+ context "with chapters but no name" do
104
+ before do
105
+ Dir.mktmpdir do |dir|
106
+ tmpf = "#{dir}/tmp"
107
+ @content = ~<<-EOF
108
+ #hello
109
+ hello
110
+ <<<------>>>
111
+ #Goodbye
112
+ goodbye
113
+ <<<--- chapter03 --->>>
114
+ #Yo
115
+ yoyoyo
116
+ EOF
117
+ @ch1, @ch2, @ch3 = ~<<-F1, ~<<-F2, ~<<-F3
118
+ #hello
119
+ hello
120
+ F1
121
+ #Goodbye
122
+ goodbye
123
+ F2
124
+ #Yo
125
+ yoyoyo
126
+ F3
127
+ File.write(tmpf, @content)
128
+ @f = split(tmpf)
129
+ end
130
+ end
131
+
132
+ it { @f.should == {'tmp' => @ch1, 'tmp02' => @ch2, 'chapter03' => @ch3} }
133
+ end
134
+
135
+ context "with chapters but no name 2" do
136
+ before do
137
+ Dir.mktmpdir do |dir|
138
+ tmpf = "#{dir}/chapter1"
139
+ @content = ~<<-EOF
140
+ #hello
141
+ hello
142
+ <<<------>>>
143
+ #Goodbye
144
+ goodbye
145
+ <<<--- --->>>
146
+ #Yo
147
+ yoyoyo
148
+ EOF
149
+ @ch1, @ch2, @ch3 = ~<<-F1, ~<<-F2, ~<<-F3
150
+ #hello
151
+ hello
152
+ F1
153
+ #Goodbye
154
+ goodbye
155
+ F2
156
+ #Yo
157
+ yoyoyo
158
+ F3
159
+ File.write(tmpf, @content)
160
+ @f = split(tmpf)
161
+ end
162
+ end
163
+
164
+ it { @f.should == {'chapter1' => @ch1, 'chapter2' => @ch2, 'chapter3' => @ch3} }
165
+ end
102
166
  end
103
167
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maliq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: