lib/review/book/base.rb in review-2.4.0 vs lib/review/book/base.rb in review-2.5.0

- old
+ new

@@ -30,15 +30,15 @@ @basedir_seen = {} def self.update_rubyenv(dir) return if @basedir_seen.key?(dir) - if File.file?("#{dir}/review-ext.rb") + if File.file?(File.join(dir, 'review-ext.rb')) if ENV['REVIEW_SAFE_MODE'].to_i & 2 > 0 ReVIEW.logger.warn 'review-ext.rb is prohibited in safe mode. ignored.' else - Kernel.load File.expand_path("#{dir}/review-ext.rb") + Kernel.load File.expand_path(File.join(dir, 'review-ext.rb')) end end @basedir_seen[dir] = true end @@ -181,12 +181,15 @@ end def catalog return @catalog if @catalog.present? - catalogfile_path = "#{basedir}/#{config['catalogfile']}" - @catalog = File.open(catalogfile_path) { |f| Catalog.new(f) } if File.file? catalogfile_path + catalogfile_path = filename_join(@basedir, config['catalogfile']) + @catalog = File.open(catalogfile_path, 'r:BOM|utf-8') { |f| Catalog.new(f) } if File.file? catalogfile_path + if @catalog + @catalog.validate!(basedir) + end @catalog end def read_chaps if catalog @@ -224,35 +227,38 @@ return @read_part if @read_part if catalog @read_part = catalog.parts else - @read_part = File.read("#{@basedir}/#{config['part_file']}") + @read_part = File.read(File.join(@basedir, config['part_file'])) end end def part_exist? if catalog catalog.parts.present? else - File.exist?("#{@basedir}/#{config['part_file']}") + File.exist?(File.join(@basedir, config['part_file'])) end end def read_bib - File.read("#{@basedir}/#{bib_file}") + File.read(File.join(@basedir, bib_file)) end def bib_exist? - File.exist?("#{@basedir}/#{bib_file}") + File.exist?(File.join(@basedir, bib_file)) end def prefaces - return mkpart_from_namelist(catalog.predef.split("\n")) if catalog + if catalog + return mkpart_from_namelist(catalog.predef.split("\n")) + end begin - mkpart_from_namelistfile("#{@basedir}/#{config['predef_file']}") if File.file?("#{@basedir}/#{config['predef_file']}") + predef_file = filename_join(@basedir, config['predef_file']) + mkpart_from_namelistfile(predef_file) if File.file?(predef_file) rescue FileNotFound => err raise FileNotFound, "preface #{err.message}" end end @@ -262,11 +268,12 @@ chaps = names.each_with_index.map { |n, idx| mkchap_ifexist(n, idx) }.compact return mkpart(chaps) end begin - mkpart_from_namelistfile("#{@basedir}/#{config['postdef_file']}") if File.file?("#{@basedir}/#{config['postdef_file']}") + postdef_file = filename_join(@basedir, config['postdef_file']) + mkpart_from_namelistfile(postdef_file) if File.file?(postdef_file) rescue FileNotFound => err raise FileNotFound, "postscript #{err.message}" end end @@ -299,16 +306,16 @@ if catalog return catalog.parts_with_chaps.map do |entry| if entry.is_a?(Hash) chaps = entry.values.first.map do |chap| - chap = Chapter.new(self, num += 1, chap, "#{@basedir}/#{chap}") + chap = Chapter.new(self, num += 1, chap, File.join(@basedir, chap)) chap end Part.new(self, part += 1, chaps, read_part.split("\n")[part - 1]) else - chap = Chapter.new(self, num += 1, entry, "#{@basedir}/#{entry}") + chap = Chapter.new(self, num += 1, entry, File.join(@basedir, entry)) if chap.number num = chap.number else num -= 1 end @@ -318,11 +325,11 @@ end chap = read_chaps. strip.lines.map(&:strip).join("\n").split(/\n{2,}/). map do |part_chunk| - chaps = part_chunk.split.map { |chapid| Chapter.new(self, num += 1, chapid, "#{@basedir}/#{chapid}") } + chaps = part_chunk.split.map { |chapid| Chapter.new(self, num += 1, chapid, File.join(@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, nil, chaps) end @@ -350,31 +357,31 @@ chaps.empty? ? nil : Part.new(self, nil, chaps) end def mkchap(name, number = nil) name += ext if File.extname(name).empty? - path = "#{@basedir}/#{name}" + path = File.join(@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).empty? - path = "#{@basedir}/#{name}" + path = File.join(@basedir, 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 - warn "!!! #{filename} is obsoleted. please use catalog.yml." if caller.none? { |item| item =~ %r{/review/test/test_} } + ReVIEW.logger.warn "!!! #{filename} is obsoleted. please use catalog.yml." if caller.none? { |item| item =~ %r{/review/test/test_} } end res = '' - File.open("#{@basedir}/#{filename}", 'r:BOM|utf-8') do |f| + File.open(filename_join(@basedir, filename), 'r:BOM|utf-8') do |f| f.each_line do |line| next if /\A#/ =~ line line.gsub!(/#.*\Z/, '') res << line end @@ -382,9 +389,13 @@ res rescue Errno::ENOENT Dir.glob("#{@basedir}/*#{ext}").sort.join("\n") rescue Errno::EISDIR '' + end + + def filename_join(*args) + File.join(args.reject(&:nil?)) end end end end