/
base.rb
414 lines (351 loc) · 9.53 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#
# $Id: book.rb 4315 2009-09-02 04:15:24Z kmuto $
#
# Copyright (c) 2002-2008 Minero Aoki
# 2009 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
# For details of the GNU LGPL, see the file "COPYING".
#
require 'review/configure'
require 'review/catalog'
module ReVIEW
module Book
class Base
attr_writer :config
def self.load_default
warn 'Book::Base.load_default() is obsoleted. Use Book::Base.load().'
load()
end
def self.load(dir = ".")
update_rubyenv dir
new(dir)
end
@basedir_seen = {}
def self.update_rubyenv(dir)
return if @basedir_seen.key?(dir)
if File.file?("#{dir}/review-ext.rb")
if ENV["REVIEW_SAFE_MODE"].to_i & 2 > 0
warn "review-ext.rb is prohibited in safe mode. ignored."
else
Kernel.load File.expand_path("#{dir}/review-ext.rb")
end
end
@basedir_seen[dir] = true
end
def initialize(basedir)
@basedir = basedir
@parts = nil
@chapter_index = nil
@config = ReVIEW::Configure.values
@catalog = nil
end
def bib_file
config["bib_file"]
end
def reject_file
config["reject_file"]
end
def ext
config["ext"]
end
def image_dir
config["image_dir"]
end
def image_types
config["image_types"]
end
def image_types=(types)
config["image_types"] = types
end
def page_metric
if config["page_metric"].respond_to?(:downcase) && config["page_metric"].upcase =~ /^[A-Z0-9_]+$/
ReVIEW::Book::PageMetric.const_get(config["page_metric"].upcase)
elsif config["page_metric"].kind_of?(Array) && config["page_metric"].size == 5
ReVIEW::Book::PageMetric.new(*config["page_metric"])
else
config["page_metric"]
end
end
def htmlversion
if config["htmlversion"].blank?
nil
else
config["htmlversion"].to_i
end
end
def parts
@parts ||= read_parts()
end
def parts_in_file
parts.find_all{|part|
part if part.present? and part.file?
}
end
def part(n)
parts.detect {|part| part.number == n }
end
def each_part(&block)
parts.each(&block)
end
def contents
# TODO: includes predef, appendix, postdef
if parts.present?
chapters + parts
else
chapters
end
end
def chapters
parts().map {|p| p.chapters }.flatten
end
def each_chapter(&block)
chapters.each(&block)
end
def each_chapter_r(&block)
chapters.reverse_each(&block)
end
def chapter_index
return @chapter_index if @chapter_index
contents = chapters()
parts().each do |prt|
if prt.id.present?
contents << prt
end
end
@chapter_index = ChapterIndex.new(contents)
end
def chapter(id)
chapter_index()[id]
end
def next_chapter(chapter)
finded = false
each_chapter do |c|
return c if finded
finded = true if c == chapter
end
nil # not found
end
def prev_chapter(chapter)
finded = false
each_chapter_r do |c|
return c if finded
finded = true if c == chapter
end
nil # not found
end
def volume
vol = Volume.sum(chapters.map {|chap| chap.volume })
vol.page_per_kbyte = page_metric.page_per_kbyte
vol
end
def config
@config ||= Configure.values
end
# backward compatible
def param=(param)
@config = param
end
def load_config(filename)
new_conf = YAML.load_file(filename)
@config.merge!(new_conf)
end
# backward compatible
def param
@config
end
def catalog
return @catalog if @catalog.present?
catalogfile_path = "#{basedir}/#{config["catalogfile"]}"
if File.file? catalogfile_path
@catalog = Catalog.new(File.open(catalogfile_path))
end
@catalog
end
def catalog=(catalog)
@catalog = catalog
end
def read_CHAPS
if catalog
catalog.chaps
else
read_FILE(config["chapter_file"])
end
end
def read_PREDEF
if catalog
catalog.predef
else
read_FILE(config["predef_file"])
end
end
def read_APPENDIX
if catalog
catalog.appendix
else
read_FILE(config["postdef_file"]) # for backward compatibility
end
end
def read_POSTDEF
if catalog
catalog.postdef
else
""
end
end
def read_PART
return @read_part if @read_part
if catalog
@read_part = catalog.parts
else
@read_part = File.read("#{@basedir}/#{config["part_file"]}")
end
end
def part_exist?
if catalog
catalog.parts.present?
else
File.exist?("#{@basedir}/#{config["part_file"]}")
end
end
def read_bib
File.read("#{@basedir}/#{bib_file}")
end
def bib_exist?
File.exist?("#{@basedir}/#{bib_file}")
end
def prefaces
if catalog
return mkpart_from_namelist(catalog.predef.split("\n"))
end
if File.file?("#{@basedir}/#{config["predef_file"]}")
begin
return mkpart_from_namelistfile("#{@basedir}/#{config["predef_file"]}")
rescue FileNotFound => err
raise FileNotFound, "preface #{err.message}"
end
end
end
def appendix
if catalog
names = catalog.appendix.split("\n")
chaps = names.each_with_index.map {|n, idx|
mkchap_ifexist(n, idx)
}.compact
return mkpart(chaps)
end
if File.file?("#{@basedir}/#{config["postdef_file"]}")
begin
return mkpart_from_namelistfile("#{@basedir}/#{config["postdef_file"]}")
rescue FileNotFound => err
raise FileNotFound, "postscript #{err.message}"
end
end
end
def postscripts
if catalog
mkpart_from_namelist(catalog.postdef.split("\n"))
end
end
def basedir
@basedir
end
private
def read_parts
list = parse_chapters
if pre = prefaces
list.unshift pre
end
if app = appendix
list.push app
end
if post = postscripts
list.push post
end
list
end
def parse_chapters
part = 0
num = 0
if catalog
return catalog.parts_with_chaps.map do |entry|
if entry.is_a? Hash
chaps = entry.values.first.map do |chap|
Chapter.new(self, (num += 1), chap, "#{@basedir}/#{chap}")
end
Part.new(self, (part += 1), chaps, read_PART.split("\n")[part - 1])
else
chap = Chapter.new(self, (num += 1), entry, "#{@basedir}/#{entry}")
Part.new(self, nil, [chap])
end
end
end
chap = read_CHAPS()\
.strip.lines.map {|line| line.strip }.join("\n").split(/\n{2,}/)\
.map {|part_chunk|
chaps = part_chunk.split.map {|chapid|
Chapter.new(self, (num += 1), chapid, "#{@basedir}/#{chapid}")
}
if part_exist? && read_PART.split("\n").size >= part
Part.new(self, (part += 1), chaps, read_PART.split("\n")[part-1])
else
Part.new(self, (part += 1), chaps)
end
}
return chap
end
def mkpart_from_namelistfile(path)
chaps = []
File.read(path).split.each_with_index do |name, idx|
name.sub!(/\A\xEF\xBB\xBF/u, '') # remove BOM
if path =~ /PREDEF/
chaps << mkchap(name)
else
chaps << mkchap(name, idx + 1)
end
end
mkpart(chaps)
end
def mkpart_from_namelist(names)
mkpart(names.map {|n| mkchap_ifexist(n) }.compact)
end
def mkpart(chaps)
chaps.empty? ? nil : Part.new(self, nil, chaps)
end
def mkchap(name, number = nil)
name += ext if File.extname(name) == ""
path = "#{@basedir}/#{name}"
raise FileNotFound, "file not exist: #{path}" unless File.file?(path)
Chapter.new(self, number, name, path)
end
def mkchap_ifexist(name, idx = nil)
name += ext if File.extname(name) == ""
path = "#{@basedir}/#{name}"
if File.file?(path)
idx += 1 if idx
Chapter.new(self, idx, name, path)
end
end
def read_FILE(filename)
res = ""
File.open("#{@basedir}/#{filename}") do |f|
while line = f.gets
line.sub!(/\A\xEF\xBB\xBF/u, '') # remove BOM
if /\A#/ =~ line
next
end
line.gsub!(/#.*$/, "")
res << line
end
end
res
rescue Errno::ENOENT
Dir.glob("#{@basedir}/*#{ext()}").sort.join("\n")
rescue Errno::EISDIR
""
end
end
end
end