Class: ReVIEW::Book::Chapter

Inherits:
BookUnit show all
Defined in:
lib/review/book/chapter.rb

Instance Attribute Summary collapse

Attributes inherited from BookUnit

#column_index, #content, #endnote_index, #equation_index, #footnote_index, #headline_index, #icon_index, #image_index, #indepimage_index, #lines, #list_index, #numberless_image_index, #path, #table_index

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BookUnit

#basename, #bibpaper, #bibpaper_index, #column, #dirname, #endnote, #endnotes, #equation, #execute_indexer, #footnote, #headline, #image, #image_bound?, #list, #name, #next_chapter, #prev_chapter, #size, #table, #title, #volume

Methods included from TextUtils

#add_space?, #detab, #join_lines_to_paragraph, #split_paragraph

Constructor Details

#initialize(book, number, name, path, io = nil) ⇒ Chapter

Returns a new instance of Chapter.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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

Instance Attribute Details

#bookObject (readonly)

Returns the value of attribute book.



17
18
19
# File 'lib/review/book/chapter.rb', line 17

def book
  @book
end

#numberObject (readonly)

Returns the value of attribute number.



17
18
19
# File 'lib/review/book/chapter.rb', line 17

def number
  @number
end

Class Method Details

.mkchap(book, name, number = nil) ⇒ Object

Raises:



19
20
21
22
23
24
25
# 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) ⇒ Object



27
28
29
30
31
32
33
# 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

Instance Method Details

#find_first_header_optionObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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_indexesObject



59
60
61
62
63
64
65
66
67
68
# 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

#inspectObject



94
95
96
# File 'lib/review/book/chapter.rb', line 94

def inspect
  "#<#{self.class} #{@number} #{@path}>"
end

#on_appendix?Boolean Also known as: on_APPENDIX?

Returns:

  • (Boolean)


139
140
141
# File 'lib/review/book/chapter.rb', line 139

def on_appendix?
  on_file?(@book.read_appendix)
end

#on_chaps?Boolean Also known as: on_CHAPS?

Returns:

  • (Boolean)


131
132
133
# File 'lib/review/book/chapter.rb', line 131

def on_chaps?
  on_file?(@book.read_chaps)
end

#on_postdef?Boolean Also known as: on_POSTDEF?

Returns:

  • (Boolean)


143
144
145
# File 'lib/review/book/chapter.rb', line 143

def on_postdef?
  on_file?(@book.read_postdef)
end

#on_predef?Boolean Also known as: on_PREDEF?

Returns:

  • (Boolean)


135
136
137
# File 'lib/review/book/chapter.rb', line 135

def on_predef?
  on_file?(@book.read_predef)
end