maliq 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/maliq/file_utils.rb +18 -4
- data/lib/maliq/system_extensions.rb +0 -6
- data/lib/maliq/version.rb +1 -1
- data/spec/file_utils_spec.rb +64 -0
- metadata +1 -1
data/lib/maliq/file_utils.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
+
require "enumerator"
|
2
|
+
|
1
3
|
module Maliq
|
2
4
|
module FileUtils
|
3
|
-
SPLIT_MARKER =
|
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 =
|
22
|
-
|
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
|
-
|
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
|
|
data/lib/maliq/version.rb
CHANGED
data/spec/file_utils_spec.rb
CHANGED
@@ -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
|