lib/review/book/base.rb in review-4.2.0 vs lib/review/book/base.rb in review-5.0.0

- old
+ new

@@ -7,30 +7,38 @@ # 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' +require 'review/book/bib' module ReVIEW module Book class Base attr_accessor :config attr_writer :parts - attr_writer :catalog + attr_accessor :catalog attr_reader :basedir + attr_accessor :bibpaper_index - def self.load(dir = '.') - new(dir) + def self.load(basedir = '.', config: nil) + new(basedir, config: config) end - def initialize(basedir = '.') + def initialize(basedir = '.', config: nil) @basedir = basedir @logger = ReVIEW.logger @parts = nil @chapter_index = nil - @config = ReVIEW::Configure.values + @config = config || ReVIEW::Configure.values @catalog = nil + @bibpaper_index = nil + catalog_path = filename_join(@basedir, @config['catalogfile']) + if catalog_path && File.file?(catalog_path) + parse_catalog_file(catalog_path) + end + @warn_old_files = {} # XXX for checking CHAPS, PREDEF, POSTDEF @basedir_seen = {} update_rubyenv end @@ -42,10 +50,18 @@ Kernel.load(File.expand_path(File.join(@basedir, 'review-ext.rb'))) end end end + def execute_indexer + return unless @catalog + + parts.each do |part| + part.chapters.each(&:execute_indexer) + end + end + def bib_file config['bib_file'] end def reject_file @@ -92,18 +108,42 @@ else config['htmlversion'].to_i end end + def create_chapter_index + chapter_index = ChapterIndex.new + each_chapter do |chap| + chapter_index.add_item(Index::Item.new(chap.id, chap.number, chap)) + end + parts.each do |prt| + if prt.id.present? + chapter_index.add_item(Index::Item.new(prt.id, prt.number, prt)) + end + end + chapter_index + end + + def generate_indexes + if bib_exist? + bib = ReVIEW::Book::Bib.new(file_content: bib_content, book: self) + bib.generate_indexes(use_bib: true) + @bibpaper_index = bib.bibpaper_index + end + self.each_chapter(&:generate_indexes) + self.parts.map(&:generate_indexes) + @chapter_index = create_chapter_index + end + def parts @parts ||= read_parts end def parts_in_file # TODO: should be `parts.find_all{|part| part.present? and part.file?}` ? parts.find_all do |part| - part if part.present? and part.file? + part if part.present? && part.file? end end def part(n) parts.detect { |part| part.number == n } @@ -134,19 +174,11 @@ chapters.reverse_each(&block) end def chapter_index return @chapter_index if @chapter_index - @chapter_index = ChapterIndex.new - each_chapter do |chap| - @chapter_index.add_item(Index::Item.new(chap.id, chap.number, chap)) - end - parts.each do |prt| - if prt.id.present? - @chapter_index.add_item(Index::Item.new(prt.id, prt.number, prt)) - end - end + @chapter_index = create_chapter_index @chapter_index end def chapter(id) chapter_index[id].content @@ -181,21 +213,19 @@ def load_config(filename) new_conf = YAML.load_file(filename) @config.merge!(new_conf) end - def catalog - return @catalog if @catalog.present? - - catalogfile_path = filename_join(@basedir, config['catalogfile']) - if File.file?(catalogfile_path) - @catalog = File.open(catalogfile_path, 'rt:BOM|utf-8') { |f| Catalog.new(f) } + def parse_catalog_file(path) + unless File.file?(path) + raise FileNotFound, "catalog.yml is not found #{path}" end - if @catalog + + File.open(path, 'rt:BOM|utf-8') do |f| + @catalog = Catalog.new(f) @catalog.validate!(@config, basedir) end - @catalog end def read_chaps if catalog catalog.chaps @@ -250,10 +280,14 @@ def bib_exist? File.exist?(File.join(contentdir, bib_file)) end + def bib_content + File.read(File.join(contentdir, bib_file)) + end + def prefaces if catalog return Part.mkpart_from_namelist(self, catalog.predef) end @@ -331,18 +365,20 @@ Part.new(self, nil, [chap]) end end end - chap = read_chaps.map(&:strip).join("\n").split(/\n{2,}/). - map do |part_chunk| + # rubocop:disable Style/RedundantAssignment + 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.size > part Part.new(self, part += 1, chaps, read_part[part - 1]) else Part.new(self, nil, chaps) end end + # rubocop:enable Style/RedundantAssignment + chap end def read_file(filename) unless @warn_old_files[filename]