lib/review/book/base.rb in review-1.3.0 vs lib/review/book/base.rb in review-1.4.0
- old
+ new
@@ -46,31 +46,46 @@
end
end
@basedir_seen[dir] = true
end
- def initialize(basedir, parameters = Parameters.default)
+ def initialize(basedir)
@basedir = basedir
- @parameters = parameters
@parts = nil
@chapter_index = nil
+ @config = ReVIEW::Configure.values
+ @catalog = nil
end
- extend Forwardable
- def_delegators '@parameters',
- :chapter_file,
- :part_file,
- :bib_file,
- :reject_file,
- :predef_file,
- :postdef_file,
- :ext,
- :image_dir,
- :image_types,
- :image_types=,
- :page_metric
+ 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
+ config["page_metric"]
+ end
+
def parts
@parts ||= read_parts()
end
def parts_in_file
@@ -139,70 +154,78 @@
def config
@config ||= Configure.values
end
- # backword compatible
+ # backward compatible
def param=(param)
@config = param
end
- # backword compatible
+ # backward compatible
def param
@config
end
def catalog
return @catalog if @catalog.present?
catalogfile_path = "#{basedir}/#{config["catalogfile"]}"
- if File.exist? catalogfile_path
+ if File.file? catalogfile_path
@catalog = Catalog.new(File.open catalogfile_path)
end
@catalog
end
def read_CHAPS
if catalog
catalog.chaps
else
- read_FILE(chapter_file)
+ read_FILE(config["chapter_file"])
end
end
def read_PREDEF
if catalog
catalog.predef
else
- read_FILE(predef_file)
+ 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
- read_FILE(postdef_file)
+ ""
end
end
def read_PART
return @read_PART if @read_PART
if catalog
@read_PART = catalog.parts
else
- @read_PART = File.read("#{@basedir}/#{part_file}")
+ @read_PART = File.read("#{@basedir}/#{config["part_file"]}")
end
end
def part_exist?
if catalog
catalog.parts.present?
else
- File.exist?("#{@basedir}/#{part_file}")
+ File.exist?("#{@basedir}/#{config["part_file"]}")
end
end
def read_bib
File.read("#{@basedir}/#{bib_file}")
@@ -215,37 +238,43 @@
def prefaces
if catalog
return mkpart_from_namelist(catalog.predef.split("\n"))
end
- if File.file?("#{@basedir}/#{predef_file}")
+ if File.file?("#{@basedir}/#{config["predef_file"]}")
begin
- return mkpart_from_namelistfile("#{@basedir}/#{predef_file}")
+ return mkpart_from_namelistfile("#{@basedir}/#{config["predef_file"]}")
rescue FileNotFound => err
raise FileNotFound, "preface #{err.message}"
end
- else
- mkpart_from_namelist(%w(preface))
end
end
- def postscripts
+ def appendix
if catalog
- return mkpart_from_namelist(catalog.postdef.split("\n"))
+ 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}/#{postdef_file}")
+ if File.file?("#{@basedir}/#{config["postdef_file"]}")
begin
- return mkpart_from_namelistfile("#{@basedir}/#{postdef_file}")
+ return mkpart_from_namelistfile("#{@basedir}/#{config["postdef_file"]}")
rescue FileNotFound => err
raise FileNotFound, "postscript #{err.message}"
end
- else
- mkpart_from_namelist(%w(appendix postscript))
end
end
+ def postscripts
+ if catalog
+ mkpart_from_namelist(catalog.postdef.split("\n"))
+ end
+ end
+
def basedir
@basedir
end
private
@@ -253,10 +282,13 @@
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
@@ -320,14 +352,17 @@
path = "#{@basedir}/#{name}"
raise FileNotFound, "file not exist: #{path}" unless File.file?(path)
Chapter.new(self, number, name, path)
end
- def mkchap_ifexist(name)
+ def mkchap_ifexist(name, idx = nil)
name += ext if File.extname(name) == ""
path = "#{@basedir}/#{name}"
- File.file?(path) ? Chapter.new(self, nil, name, path) : nil
+ 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|
@@ -341,9 +376,11 @@
end
end
res
rescue Errno::ENOENT
Dir.glob("#{@basedir}/*#{ext()}").sort.join("\n")
+ rescue Errno::EISDIR
+ ""
end
end
end
end