class ReVIEW::Book::Chapter

Attributes

book[R]
number[R]

Public Class Methods

mkchap(book, name, number = nil) click to toggle source
# File lib/review/book/chapter.rb, line 19
def self.mkchap(book, name, number = nil)
  name += book.ext if File.extname(name).empty?
  path = File.join(book.contentdir, name)
  raise FileNotFound, "file not exist: #{path}" unless File.file?(path)

  Chapter.new(book, number, name, path)
end
mkchap_ifexist(book, name, number = nil) click to toggle source
# File lib/review/book/chapter.rb, line 27
def self.mkchap_ifexist(book, name, number = nil)
  name += book.ext if File.extname(name).empty?
  path = File.join(book.contentdir, name)
  if File.file?(path)
    Chapter.new(book, number, name, path)
  end
end
new(book, number, name, path, io = nil) click to toggle source
Calls superclass method ReVIEW::Book::BookUnit::new
# File lib/review/book/chapter.rb, line 35
def initialize(book, number, name, path, io = nil)
  @book = book
  @number = number
  @name = name
  @path = path
  @io = io
  @title = nil
  if @io
    begin
      @content = @io.read
    rescue StandardError
      @content = nil
    end
  else
    @content = nil
  end
  if !@content && @path && File.exist?(@path)
    @content = File.read(@path, mode: 'rt:BOM|utf-8')
    @number = nil if %w[nonum nodisp notoc].include?(find_first_header_option)
  end

  super()
end

Public Instance Methods

find_first_header_option() click to toggle source
# File lib/review/book/chapter.rb, line 70
def find_first_header_option
  f = LineInput.new(StringIO.new(@content))
  begin
    while f.next?
      case f.peek
      when /\A=+[\[\s{]/
        m = /\A(=+)(?:\[(.+?)\])?(?:\{(.+?)\})?(.*)/.match(f.gets)
        return m[2] # tag
      when %r{/\A//[a-z]+/}
        line = f.gets
        if line.rstrip[-1, 1] == '{'
          f.until_match(%r{\A//\}})
        end
      end
      f.gets
    end
    nil
  rescue ArgumentError => e
    raise ReVIEW::CompileError, "#{@name}: #{e}"
  rescue SyntaxError => e
    raise ReVIEW::SyntaxError, "#{@name}:#{f.lineno}: #{e}"
  end
end
format_number(heading = true) click to toggle source
# File lib/review/book/chapter.rb, line 98
def format_number(heading = true)
  return '' unless @number
  if on_predef?
    return @number.to_s
  end

  if on_appendix?
    # XXX: should be extracted with magic number
    if @number < 1 || @number > 27
      return @number.to_s
    end
    if @book.config['appendix_format']
      raise ReVIEW::ConfigError, %Q('appendix_format:' in config.yml is obsoleted.)
    end

    i18n_appendix = I18n.get('appendix')
    fmt = i18n_appendix.scan(/%\w{1,3}/).first || '%s'
    I18n.update('appendix_without_heading' => fmt)

    if heading
      return I18n.t('appendix', @number)
    else
      return I18n.t('appendix_without_heading', @number)
    end
  end

  if heading
    I18n.t('chapter', @number)
  else
    @number.to_s
  end
end
generate_indexes() click to toggle source
Calls superclass method ReVIEW::Book::BookUnit#generate_indexes
# File lib/review/book/chapter.rb, line 59
def generate_indexes
  super

  return unless content

  @numberless_image_index = @indexes.numberless_image_index
  @image_index = @indexes.image_index
  @icon_index = @indexes.icon_index
  @indepimage_index = @indexes.indepimage_index
end
inspect() click to toggle source
# File lib/review/book/chapter.rb, line 94
def inspect
  "#<#{self.class} #{@number} #{@path}>"
end
on_appendix?() click to toggle source
# File lib/review/book/chapter.rb, line 139
def on_appendix?
  on_file?(@book.read_appendix)
end
Also aliased as: on_APPENDIX?
on_chaps?() click to toggle source
# File lib/review/book/chapter.rb, line 131
def on_chaps?
  on_file?(@book.read_chaps)
end
Also aliased as: on_CHAPS?
on_postdef?() click to toggle source
# File lib/review/book/chapter.rb, line 143
def on_postdef?
  on_file?(@book.read_postdef)
end
Also aliased as: on_POSTDEF?
on_predef?() click to toggle source
# File lib/review/book/chapter.rb, line 135
def on_predef?
  on_file?(@book.read_predef)
end
Also aliased as: on_PREDEF?

Private Instance Methods

on_APPENDIX?()
Alias for: on_appendix?
on_CHAPS?()

backward compatibility

Alias for: on_chaps?
on_FILE?(contents)
Alias for: on_file?
on_POSTDEF?()
Alias for: on_postdef?
on_PREDEF?()
Alias for: on_predef?
on_file?(contents) click to toggle source
# File lib/review/book/chapter.rb, line 149
def on_file?(contents)
  contents.map(&:strip).include?("#{id}#{@book.ext}")
end
Also aliased as: on_FILE?