Class: ReVIEW::Book::Base

Inherits:
Object show all
Defined in:
lib/review/book/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basedir = '.', config: nil) ⇒ Base

Returns a new instance of Base.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/review/book/base.rb', line 27

def initialize(basedir = '.', config: nil)
  @basedir = basedir
  @logger = ReVIEW.logger
  @parts = nil
  @chapter_index = nil
  @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

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



20
21
22
# File 'lib/review/book/base.rb', line 20

def basedir
  @basedir
end

#bibpaper_indexObject

Returns the value of attribute bibpaper_index.



21
22
23
# File 'lib/review/book/base.rb', line 21

def bibpaper_index
  @bibpaper_index
end

#catalogObject

Returns the value of attribute catalog.



19
20
21
# File 'lib/review/book/base.rb', line 19

def catalog
  @catalog
end

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#partsObject



137
138
139
# File 'lib/review/book/base.rb', line 137

def parts
  @parts ||= read_parts
end

Class Method Details

.load(basedir = '.', config: nil) ⇒ Object



23
24
25
# File 'lib/review/book/base.rb', line 23

def self.load(basedir = '.', config: nil)
  new(basedir, config: config)
end

Instance Method Details

#appendixObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/review/book/base.rb', line 307

def appendix
  if catalog
    names = catalog.appendix
    chaps = names.each_with_index.map { |name, number| Chapter.mkchap_ifexist(self, name, number + 1) }.compact
    return Part.mkpart(chaps)
  end

  begin
    postdef_file = filename_join(@basedir, config['postdef_file'])
    if File.file?(postdef_file)
      Part.mkpart_from_namelistfile(self, postdef_file)
    end
  rescue FileNotFound => e
    raise FileNotFound, "postscript #{e.message}"
  end
end

#bib_contentObject



288
289
290
# File 'lib/review/book/base.rb', line 288

def bib_content
  File.read(File.join(contentdir, bib_file))
end

#bib_exist?Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/review/book/base.rb', line 284

def bib_exist?
  File.exist?(File.join(contentdir, bib_file))
end

#bib_fileObject



63
64
65
# File 'lib/review/book/base.rb', line 63

def bib_file
  config['bib_file']
end

#chapter(id) ⇒ Object



184
185
186
# File 'lib/review/book/base.rb', line 184

def chapter(id)
  chapter_index[id].content
end

#chapter_indexObject



177
178
179
180
181
182
# File 'lib/review/book/base.rb', line 177

def chapter_index
  return @chapter_index if @chapter_index

  @chapter_index = create_chapter_index
  @chapter_index
end

#chaptersObject



165
166
167
# File 'lib/review/book/base.rb', line 165

def chapters
  parts.map(&:chapters).flatten
end

#contentdirObject



87
88
89
90
91
92
93
# File 'lib/review/book/base.rb', line 87

def contentdir
  if !config['contentdir'].present? || config['contentdir'] == '.'
    @basedir
  else
    File.join(@basedir, config['contentdir'])
  end
end

#contentsObject



156
157
158
159
160
161
162
163
# File 'lib/review/book/base.rb', line 156

def contents
  # TODO: includes predef, appendix, postdef
  if parts.present?
    chapters + parts
  else
    chapters
  end
end

#create_chapter_indexObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/review/book/base.rb', line 113

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

#each_chapter(&block) ⇒ Object



169
170
171
# File 'lib/review/book/base.rb', line 169

def each_chapter(&block)
  chapters.each(&block)
end

#each_chapter_r(&block) ⇒ Object



173
174
175
# File 'lib/review/book/base.rb', line 173

def each_chapter_r(&block)
  chapters.reverse_each(&block)
end

#each_part(&block) ⇒ Object



152
153
154
# File 'lib/review/book/base.rb', line 152

def each_part(&block)
  parts.each(&block)
end

#execute_indexerObject



55
56
57
58
59
60
61
# File 'lib/review/book/base.rb', line 55

def execute_indexer
  return unless @catalog

  parts.each do |part|
    part.chapters.each(&:execute_indexer)
  end
end

#extObject



71
72
73
# File 'lib/review/book/base.rb', line 71

def ext
  config['ext']
end

#generate_indexesObject



126
127
128
129
130
131
132
133
134
135
# File 'lib/review/book/base.rb', line 126

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

#htmlversionObject



105
106
107
108
109
110
111
# File 'lib/review/book/base.rb', line 105

def htmlversion
  if config['htmlversion'].blank?
    nil
  else
    config['htmlversion'].to_i
  end
end

#image_typesObject



79
80
81
# File 'lib/review/book/base.rb', line 79

def image_types
  config['image_types']
end

#image_types=(types) ⇒ Object



83
84
85
# File 'lib/review/book/base.rb', line 83

def image_types=(types)
  config['image_types'] = types
end

#imagedirObject



75
76
77
# File 'lib/review/book/base.rb', line 75

def imagedir
  config['imagedir']
end

#load_config(filename) ⇒ Object



216
217
218
219
# File 'lib/review/book/base.rb', line 216

def load_config(filename)
  new_conf = YAMLLoader.safe_load_file(filename)
  @config.merge!(new_conf)
end

#next_chapter(chapter) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/review/book/base.rb', line 188

def next_chapter(chapter)
  finded = false
  each_chapter do |c|
    return c if finded

    if c == chapter
      finded = true
    end
  end
  nil # not found
end

#page_metricObject



95
96
97
98
99
100
101
102
103
# File 'lib/review/book/base.rb', line 95

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 || config['page_metric'].size == 4)
    ReVIEW::Book::PageMetric.new(*config['page_metric'])
  else
    config['page_metric']
  end
end

#parse_catalog_file(path) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/review/book/base.rb', line 221

def parse_catalog_file(path)
  unless File.file?(path)
    raise FileNotFound, "catalog.yml is not found #{path}"
  end

  File.open(path, 'rt:BOM|utf-8') do |f|
    @catalog = Catalog.new(f)
    @catalog.validate!(@config, basedir)
  end
end

#part(n) ⇒ Object



148
149
150
# File 'lib/review/book/base.rb', line 148

def part(n)
  parts.detect { |part| part.number == n }
end

#part_exist?Boolean

Returns:

  • (Boolean)


272
273
274
275
276
277
278
# File 'lib/review/book/base.rb', line 272

def part_exist?
  if catalog
    catalog.parts.present?
  else
    File.exist?(File.join(@basedir, config['part_file']))
  end
end

#parts_in_fileObject



141
142
143
144
145
146
# File 'lib/review/book/base.rb', line 141

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? && part.file?
  end
end

#postscriptsObject



324
325
326
327
328
# File 'lib/review/book/base.rb', line 324

def postscripts
  if catalog
    Part.mkpart_from_namelist(self, catalog.postdef)
  end
end

#prefacesObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/review/book/base.rb', line 292

def prefaces
  if catalog
    return Part.mkpart_from_namelist(self, catalog.predef)
  end

  begin
    predef_file = filename_join(@basedir, config['predef_file'])
    if File.file?(predef_file)
      Part.mkpart_from_namelistfile(self, predef_file)
    end
  rescue FileNotFound => e
    raise FileNotFound, "preface #{e.message}"
  end
end

#prev_chapter(chapter) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
# File 'lib/review/book/base.rb', line 200

def prev_chapter(chapter)
  finded = false
  each_chapter_r do |c|
    return c if finded

    if c == chapter
      finded = true
    end
  end
  nil # not found
end

#read_appendixObject



248
249
250
251
252
253
254
# File 'lib/review/book/base.rb', line 248

def read_appendix
  if catalog
    catalog.appendix
  else
    read_file(config['postdef_file']).split("\n") # for backward compatibility
  end
end

#read_bibObject



280
281
282
# File 'lib/review/book/base.rb', line 280

def read_bib
  File.read(File.join(contentdir, bib_file))
end

#read_chapsObject



232
233
234
235
236
237
238
# File 'lib/review/book/base.rb', line 232

def read_chaps
  if catalog
    catalog.chaps
  else
    read_file(config['chapter_file']).split("\n")
  end
end

#read_partObject



264
265
266
267
268
269
270
# File 'lib/review/book/base.rb', line 264

def read_part
  if catalog
    catalog.parts
  else
    File.read(File.join(@basedir, config['part_file'])).split("\n")
  end
end

#read_postdefObject



256
257
258
259
260
261
262
# File 'lib/review/book/base.rb', line 256

def read_postdef
  if catalog
    catalog.postdef
  else
    []
  end
end

#read_predefObject



240
241
242
243
244
245
246
# File 'lib/review/book/base.rb', line 240

def read_predef
  if catalog
    catalog.predef
  else
    read_file(config['predef_file']).split("\n")
  end
end

#reject_fileObject



67
68
69
# File 'lib/review/book/base.rb', line 67

def reject_file
  config['reject_file']
end

#update_rubyenvObject



45
46
47
48
49
50
51
52
53
# File 'lib/review/book/base.rb', line 45

def update_rubyenv
  if File.file?(File.join(@basedir, 'review-ext.rb'))
    if ENV['REVIEW_SAFE_MODE'].to_i & 2 > 0
      @logger.warn 'review-ext.rb is prohibited in safe mode. ignored.'
    else
      Kernel.load(File.expand_path(File.join(@basedir, 'review-ext.rb')))
    end
  end
end

#volumeObject



212
213
214
# File 'lib/review/book/base.rb', line 212

def volume
  Volume.sum(parts.map(&:volume) + chapters.map(&:volume))
end