class ReVIEW::Book::Base

Attributes

basedir[R]
catalog[W]
config[W]
parts[W]

Public Class Methods

clear_rubyenv() click to toggle source
# File lib/review/book/base.rb, line 45
def self.clear_rubyenv
  @basedir_seen = {}
end
load(dir = '.') click to toggle source
# File lib/review/book/base.rb, line 26
def self.load(dir = '.')
  update_rubyenv dir
  new(dir)
end
load_default() click to toggle source
# File lib/review/book/base.rb, line 21
def self.load_default
  ReVIEW.logger.warn 'Book::Base.load_default() is obsoleted. Use Book::Base.load().'
  load
end
new(basedir) click to toggle source
# File lib/review/book/base.rb, line 49
def initialize(basedir)
  @basedir = basedir
  @parts = nil
  @chapter_index = nil
  @config = ReVIEW::Configure.values
  @catalog = nil
  @read_part = nil
  @warn_old_files = {} # XXX for checking CHAPS, PREDEF, POSTDEF
end
update_rubyenv(dir) click to toggle source
# File lib/review/book/base.rb, line 33
def self.update_rubyenv(dir)
  return if @basedir_seen.key?(dir)
  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(File.join(dir, 'review-ext.rb'))
    end
  end
  @basedir_seen[dir] = true
end

Public Instance Methods

appendix() click to toggle source
# File lib/review/book/base.rb, line 265
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

  begin
    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
bib_exist?() click to toggle source
# File lib/review/book/base.rb, line 248
def bib_exist?
  File.exist?(File.join(@basedir, bib_file))
end
bib_file() click to toggle source
# File lib/review/book/base.rb, line 59
def bib_file
  config['bib_file']
end
catalog() click to toggle source
# File lib/review/book/base.rb, line 183
def catalog
  return @catalog if @catalog.present?

  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
chapter(id) click to toggle source
# File lib/review/book/base.rb, line 146
def chapter(id)
  chapter_index[id]
end
chapter_index() click to toggle source
# File lib/review/book/base.rb, line 138
def chapter_index
  return @chapter_index if @chapter_index

  contents = chapters
  parts.each { |prt| contents << prt if prt.id.present? }
  @chapter_index = ChapterIndex.new(contents)
end
chapters() click to toggle source
# File lib/review/book/base.rb, line 126
def chapters
  parts.map(&:chapters).flatten
end
config() click to toggle source
# File lib/review/book/base.rb, line 174
def config
  @config ||= Configure.values
end
contents() click to toggle source
# File lib/review/book/base.rb, line 117
def contents
  # TODO: includes predef, appendix, postdef
  if parts.present?
    chapters + parts
  else
    chapters
  end
end
each_chapter(&block) click to toggle source
# File lib/review/book/base.rb, line 130
def each_chapter(&block)
  chapters.each(&block)
end
each_chapter_r(&block) click to toggle source
# File lib/review/book/base.rb, line 134
def each_chapter_r(&block)
  chapters.reverse_each(&block)
end
each_part(&block) click to toggle source
# File lib/review/book/base.rb, line 113
def each_part(&block)
  parts.each(&block)
end
ext() click to toggle source
# File lib/review/book/base.rb, line 67
def ext
  config['ext']
end
htmlversion() click to toggle source
# File lib/review/book/base.rb, line 93
def htmlversion
  if config['htmlversion'].blank?
    nil
  else
    config['htmlversion'].to_i
  end
end
image_dir() click to toggle source
# File lib/review/book/base.rb, line 71
def image_dir
  config['image_dir']
end
image_types() click to toggle source
# File lib/review/book/base.rb, line 75
def image_types
  config['image_types']
end
image_types=(types) click to toggle source
# File lib/review/book/base.rb, line 79
def image_types=(types)
  config['image_types'] = types
end
load_config(filename) click to toggle source
# File lib/review/book/base.rb, line 178
def load_config(filename)
  new_conf = YAML.load_file(filename)
  @config.merge!(new_conf)
end
next_chapter(chapter) click to toggle source
# File lib/review/book/base.rb, line 150
def next_chapter(chapter)
  finded = false
  each_chapter do |c|
    return c if finded
    finded = true if c == chapter
  end
  nil # not found
end
page_metric() click to toggle source
# File lib/review/book/base.rb, line 83
def page_metric
  if config['page_metric'].respond_to?(:downcase) && config['page_metric'].upcase =~ /\A[A-Z0-9_]+\Z/
    ReVIEW::Book::PageMetric.const_get(config['page_metric'].upcase)
  elsif config['page_metric'].is_a?(Array) && config['page_metric'].size == 5
    ReVIEW::Book::PageMetric.new(*config['page_metric'])
  else
    config['page_metric']
  end
end
part(n) click to toggle source
# File lib/review/book/base.rb, line 109
def part(n)
  parts.detect { |part| part.number == n }
end
part_exist?() click to toggle source
# File lib/review/book/base.rb, line 236
def part_exist?
  if catalog
    catalog.parts.present?
  else
    File.exist?(File.join(@basedir, config['part_file']))
  end
end
parts() click to toggle source
# File lib/review/book/base.rb, line 101
def parts
  @parts ||= read_parts
end
parts_in_file() click to toggle source
# File lib/review/book/base.rb, line 105
def parts_in_file
  parts.find_all { |part| part if part.present? and part.file? }
end
postscripts() click to toggle source
# File lib/review/book/base.rb, line 280
def postscripts
  mkpart_from_namelist(catalog.postdef.split("\n")) if catalog
end
prefaces() click to toggle source
# File lib/review/book/base.rb, line 252
def prefaces
  if catalog
    return mkpart_from_namelist(catalog.predef.split("\n"))
  end

  begin
    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
prev_chapter(chapter) click to toggle source
# File lib/review/book/base.rb, line 159
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
read_appendix() click to toggle source
# File lib/review/book/base.rb, line 210
def read_appendix
  if catalog
    catalog.appendix
  else
    read_file(config['postdef_file']) # for backward compatibility
  end
end
read_bib() click to toggle source
# File lib/review/book/base.rb, line 244
def read_bib
  File.read(File.join(@basedir, bib_file))
end
read_chaps() click to toggle source
# File lib/review/book/base.rb, line 194
def read_chaps
  if catalog
    catalog.chaps
  else
    read_file(config['chapter_file'])
  end
end
read_part() click to toggle source
# File lib/review/book/base.rb, line 226
def read_part
  return @read_part if @read_part

  if catalog
    @read_part = catalog.parts
  else
    @read_part = File.read(File.join(@basedir, config['part_file']))
  end
end
read_postdef() click to toggle source
# File lib/review/book/base.rb, line 218
def read_postdef
  if catalog
    catalog.postdef
  else
    ''
  end
end
read_predef() click to toggle source
# File lib/review/book/base.rb, line 202
def read_predef
  if catalog
    catalog.predef
  else
    read_file(config['predef_file'])
  end
end
reject_file() click to toggle source
# File lib/review/book/base.rb, line 63
def reject_file
  config['reject_file']
end
volume() click to toggle source
# File lib/review/book/base.rb, line 168
def volume
  vol = Volume.sum(chapters.map(&:volume))
  vol.page_per_kbyte = page_metric.page_per_kbyte
  vol
end

Private Instance Methods

filename_join(*args) click to toggle source
# File lib/review/book/base.rb, line 396
def filename_join(*args)
  File.join(args.reject(&:nil?))
end
mkchap(name, number = nil) click to toggle source
# File lib/review/book/base.rb, line 360
def mkchap(name, number = nil)
  name += ext if File.extname(name).empty?
  path = File.join(@basedir, name)
  raise FileNotFound, "file not exist: #{path}" unless File.file?(path)
  Chapter.new(self, number, name, path)
end
mkchap_ifexist(name, idx = nil) click to toggle source
# File lib/review/book/base.rb, line 367
def mkchap_ifexist(name, idx = nil)
  name += ext if File.extname(name).empty?
  path = File.join(@basedir, name)
  if File.file?(path)
    idx += 1 if idx
    Chapter.new(self, idx, name, path)
  end
end
mkpart(chaps) click to toggle source
# File lib/review/book/base.rb, line 356
def mkpart(chaps)
  chaps.empty? ? nil : Part.new(self, nil, chaps)
end
mkpart_from_namelist(names) click to toggle source
# File lib/review/book/base.rb, line 352
def mkpart_from_namelist(names)
  mkpart(names.map { |n| mkchap_ifexist(n) }.compact)
end
mkpart_from_namelistfile(path) click to toggle source
# File lib/review/book/base.rb, line 340
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
parse_chapters() click to toggle source

return Array of Part, not Chapter

# File lib/review/book/base.rb, line 303
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|
          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, File.join(@basedir, entry))
        if chap.number
          num = chap.number
        else
          num -= 1
        end
        Part.new(self, nil, [chap])
      end
    end
  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, 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
  end
  chap
end
read_file(filename) click to toggle source
# File lib/review/book/base.rb, line 376
def read_file(filename)
  unless @warn_old_files[filename]
    @warn_old_files[filename] = true
    ReVIEW.logger.warn "!!! #{filename} is obsoleted. please use catalog.yml." if caller.none? { |item| item =~ %r{/review/test/test_} }
  end
  res = ''
  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
  end
  res
rescue Errno::ENOENT
  Dir.glob("#{@basedir}/*#{ext}").sort.join("\n")
rescue Errno::EISDIR
  ''
end
read_parts() click to toggle source
# File lib/review/book/base.rb, line 286
def read_parts
  list = parse_chapters
  # NOTE: keep this = style to work this logic.
  if pre = prefaces
    list.unshift pre
  end
  if app = appendix
    list.push app
  end
  if post = postscripts
    list.push post
  end
  list
end