lib/review/book/base.rb in review-3.2.0 vs lib/review/book/base.rb in review-4.0.0

- old
+ new

@@ -27,11 +27,10 @@ @logger = ReVIEW.logger @parts = nil @chapter_index = nil @config = ReVIEW::Configure.values @catalog = nil - @read_part = nil @warn_old_files = {} # XXX for checking CHAPS, PREDEF, POSTDEF @basedir_seen = {} update_rubyenv end @@ -187,12 +186,12 @@ def catalog return @catalog if @catalog.present? catalogfile_path = filename_join(@basedir, config['catalogfile']) - if File.file? catalogfile_path - @catalog = File.open(catalogfile_path, 'r:BOM|utf-8') { |f| Catalog.new(f) } + if File.file?(catalogfile_path) + @catalog = File.open(catalogfile_path, 'rt:BOM|utf-8') { |f| Catalog.new(f) } end if @catalog @catalog.validate!(@config, basedir) end @catalog @@ -200,45 +199,43 @@ def read_chaps if catalog catalog.chaps else - read_file(config['chapter_file']) + read_file(config['chapter_file']).split("\n") end end def read_predef if catalog catalog.predef else - read_file(config['predef_file']) + read_file(config['predef_file']).split("\n") end end def read_appendix if catalog catalog.appendix else - read_file(config['postdef_file']) # for backward compatibility + read_file(config['postdef_file']).split("\n") # 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 + catalog.parts else - @read_part = File.read(File.join(@basedir, config['part_file'])) + File.read(File.join(@basedir, config['part_file'])).split("\n") end end def part_exist? if catalog @@ -256,59 +253,59 @@ File.exist?(File.join(contentdir, bib_file)) end def prefaces if catalog - return mkpart_from_namelist(catalog.predef.split("\n")) + return Part.mkpart_from_namelist(self, catalog.predef) end begin predef_file = filename_join(@basedir, config['predef_file']) if File.file?(predef_file) - mkpart_from_namelistfile(predef_file) + Part.mkpart_from_namelistfile(self, predef_file) end rescue FileNotFound => e raise FileNotFound, "preface #{e.message}" 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) + names = catalog.appendix + chaps = names.each_with_index.map { |name, number| Chapter.mkchap_ifexist(self, name, number + 1) }.compact + return Part.mkpart(chaps) end begin postdef_file = filename_join(@basedir, config['postdef_file']) if File.file?(postdef_file) - mkpart_from_namelistfile(postdef_file) + Part.mkpart_from_namelistfile(self, postdef_file) end rescue FileNotFound => e raise FileNotFound, "postscript #{e.message}" end end def postscripts if catalog - mkpart_from_namelist(catalog.postdef.split("\n")) + Part.mkpart_from_namelist(self, catalog.postdef) end end private def read_parts list = parse_chapters # NOTE: keep this = style to work this logic. if pre = prefaces - list.unshift pre + list.unshift(pre) end if app = appendix - list.push app + list.push(app) end if post = postscripts - list.push post + list.push(post) end list end # return Array of Part, not Chapter @@ -322,11 +319,11 @@ if entry.is_a?(Hash) chaps = entry.values.first.map do |chap| chap = Chapter.new(self, num += 1, chap, File.join(contentdir, chap)) chap end - Part.new(self, part += 1, chaps, read_part.split("\n")[part - 1]) + Part.new(self, part += 1, chaps, read_part[part - 1]) else chap = Chapter.new(self, num += 1, entry, File.join(contentdir, entry)) if chap.number num = chap.number else @@ -335,67 +332,30 @@ Part.new(self, nil, [chap]) end end end - chap = read_chaps. - strip.lines.map(&:strip).join("\n").split(/\n{2,}/). + chap = read_chaps.map(&:strip).join("\n").split(/\n{2,}/). map do |part_chunk| chaps = part_chunk.split.map { |chapid| Chapter.new(self, num += 1, chapid, File.join(contentdir, chapid)) } - if part_exist? && read_part.split("\n").size > part - Part.new(self, part += 1, chaps, read_part.split("\n")[part - 1]) + if part_exist? && read_part.size > part + Part.new(self, part += 1, chaps, read_part[part - 1]) else Part.new(self, nil, chaps) end end chap end - def mkpart_from_namelistfile(path) - chaps = [] - File.read(path, mode: 'r:BOM|utf-8').split.each_with_index do |name, idx| - 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).empty? - path = File.join(contentdir, 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).empty? - path = File.join(contentdir, name) - if File.file?(path) - idx += 1 if idx - Chapter.new(self, idx, name, path) - end - end - def read_file(filename) unless @warn_old_files[filename] @warn_old_files[filename] = true if caller.none? { |item| item =~ %r{/review/test/test_} } @logger.warn "!!! #{filename} is obsoleted. please use catalog.yml." end end res = '' - File.open(filename_join(@basedir, filename), 'r:BOM|utf-8') do |f| + File.open(filename_join(@basedir, filename), 'rt:BOM|utf-8') do |f| f.each_line do |line| next if /\A#/ =~ line line.gsub!(/#.*\Z/, '') res << line end