class ReVIEW::Book::Chapter

Attributes

book[R]
number[R]

Public Class Methods

new(book, number, name, path, io = nil) click to toggle source
# File lib/review/book/chapter.rb, line 21
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
      @content = nil
    end
  else
    @content = nil
  end
  if !@content && @path && File.exist?(@path)
    @content = File.read(@path, mode: 'r:BOM|utf-8')
    @number = nil if %w[nonum nodisp notoc].include?(find_first_header_option)
  end
  @list_index = nil
  @table_index = nil
  @footnote_index = nil
  @image_index = nil
  @icon_index = nil
  @numberless_image_index = nil
  @indepimage_index = nil
  @headline_index = nil
  @column_index = nil
  @volume = nil
end

Public Instance Methods

find_first_header_option() click to toggle source
# File lib/review/book/chapter.rb, line 53
def find_first_header_option
  f = LineInput.new(Preprocessor::Strip.new(StringIO.new(@content)))
  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
      f.until_match(%r{\A//\}}) if line.rstrip[-1, 1] == '{'
    end
    f.gets
  end
  nil
end
format_number(heading = true) click to toggle source
# File lib/review/book/chapter.rb, line 73
def format_number(heading = true)
  return '' unless @number
  return @number.to_s if on_predef?

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

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

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

  if heading
    I18n.t('chapter', @number)
  else
    @number.to_s
  end
end
inspect() click to toggle source
# File lib/review/book/chapter.rb, line 69
def inspect
  "#<#{self.class} #{@number} #{@path}>"
end
on_appendix?() click to toggle source
# File lib/review/book/chapter.rb, line 104
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 96
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 108
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 100
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 114
def on_file?(contents)
  contents.lines.map(&:strip).include?("#{id}#{@book.ext}")
end
Also aliased as: on_FILE?