Class: ReVIEW::Book::Base

Inherits:
Object show all
Includes:
TOCRoot
Defined in:
lib/review/tocparser.rb,
lib/review/book/base.rb

Overview

reopen

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TOCRoot

#chapter?, #each_section, #each_section_with_index, #estimated_lines, #level, #n_sections

Constructor Details

#initialize(basedir) ⇒ Base

Returns a new instance of Base.



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

def initialize(basedir)
  @basedir = basedir
  @parts = nil
  @chapter_index = nil
  @config = ReVIEW::Configure.values
  @catalog = nil
end

Instance Attribute Details

#configObject



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

def config
  @config ||= Configure.values
end

Class Method Details

.load(dir = ".") ⇒ Object



26
27
28
29
# File 'lib/review/book/base.rb', line 26

def self.load(dir = ".")
  update_rubyenv dir
  new(dir)
end

.load_defaultObject



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

def self.load_default
  warn 'Book::Base.load_default() is obsoleted. Use Book::Base.load().'
  load()
end

.update_rubyenv(dir) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/review/book/base.rb', line 33

def self.update_rubyenv(dir)
  return if @basedir_seen.key?(dir)
  if File.file?("#{dir}/review-ext.rb")
    if ENV["REVIEW_SAFE_MODE"].to_i & 2 > 0
      warn "review-ext.rb is prohibited in safe mode. ignored."
    else
      Kernel.load File.expand_path("#{dir}/review-ext.rb")
    end
  end
  @basedir_seen[dir] = true
end

Instance Method Details

#appendixObject



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/review/book/base.rb', line 267

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

  if File.file?("#{@basedir}/#{config["postdef_file"]}")
    begin
      return mkpart_from_namelistfile("#{@basedir}/#{config["postdef_file"]}")
    rescue FileNotFound => err
      raise FileNotFound, "postscript #{err.message}"
    end
  end
end

#basedirObject



291
292
293
# File 'lib/review/book/base.rb', line 291

def basedir
  @basedir
end

#bib_exist?Boolean

Returns:

  • (Boolean)


249
250
251
# File 'lib/review/book/base.rb', line 249

def bib_exist?
  File.exist?("#{@basedir}/#{bib_file}")
end

#bib_fileObject



53
54
55
# File 'lib/review/book/base.rb', line 53

def bib_file
  config["bib_file"]
end

#catalogObject



184
185
186
187
188
189
190
191
192
193
# File 'lib/review/book/base.rb', line 184

def catalog
  return @catalog if @catalog.present?

  catalogfile_path = "#{basedir}/#{config["catalogfile"]}"
  if File.file? catalogfile_path
    @catalog = Catalog.new(File.open catalogfile_path)
  end

  @catalog
end

#chapter(id) ⇒ Object



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

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

#chapter_indexObject



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

def chapter_index
  return @chapter_index if @chapter_index

  contents = chapters()
  parts().each do |prt|
    if prt.id.present?
      contents << prt
    end
  end
  @chapter_index = ChapterIndex.new(contents)
end

#chaptersObject



113
114
115
# File 'lib/review/book/base.rb', line 113

def chapters
  parts().map {|p| p.chapters }.flatten
end

#each_chapter(&block) ⇒ Object



117
118
119
# File 'lib/review/book/base.rb', line 117

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

#each_chapter_r(&block) ⇒ Object



121
122
123
# File 'lib/review/book/base.rb', line 121

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

#each_part(&block) ⇒ Object



109
110
111
# File 'lib/review/book/base.rb', line 109

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

#extObject



61
62
63
# File 'lib/review/book/base.rb', line 61

def ext
  config["ext"]
end

#htmlversionObject



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

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

#image_dirObject



65
66
67
# File 'lib/review/book/base.rb', line 65

def image_dir
  config["image_dir"]
end

#image_typesObject



69
70
71
# File 'lib/review/book/base.rb', line 69

def image_types
  config["image_types"]
end

#image_types=(types) ⇒ Object



73
74
75
# File 'lib/review/book/base.rb', line 73

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

#load_config(filename) ⇒ Object



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

def load_config(filename)
  new_conf = YAML.load_file(filename)
  @config.merge!(new_conf)
end

#next_chapter(chapter) ⇒ Object



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

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_metricObject



77
78
79
80
81
82
83
84
85
# File 'lib/review/book/base.rb', line 77

def page_metric
  if config["page_metric"].respond_to?(:downcase) && config["page_metric"].upcase =~ /^[A-Z0-9_]+$/
    ReVIEW::Book::PageMetric.const_get(config["page_metric"].upcase)
  elsif config["page_metric"].kind_of?(Array) && config["page_metric"].size == 5
    ReVIEW::Book::PageMetric.new(*config["page_metric"])
  else
    config["page_metric"]
  end
end

#paramObject

backward compatible



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

def param
  @config
end

#param=(param) ⇒ Object

backward compatible



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

def param=(param)
  @config = param
end

#part(n) ⇒ Object



105
106
107
# File 'lib/review/book/base.rb', line 105

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

#part_exist?Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
243
# File 'lib/review/book/base.rb', line 237

def part_exist?
  if catalog
    catalog.parts.present?
  else
    File.exist?("#{@basedir}/#{config["part_file"]}")
  end
end

#partsObject



95
96
97
# File 'lib/review/book/base.rb', line 95

def parts
  @parts ||= read_parts()
end

#parts_in_fileObject



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

def parts_in_file
  parts.find_all{|part|
    part if part.present? and part.file?
  }
end

#postscriptsObject



285
286
287
288
289
# File 'lib/review/book/base.rb', line 285

def postscripts
  if catalog
    mkpart_from_namelist(catalog.postdef.split("\n"))
  end
end

#prefacesObject



253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/review/book/base.rb', line 253

def prefaces
  if catalog
    return mkpart_from_namelist(catalog.predef.split("\n"))
  end

  if File.file?("#{@basedir}/#{config["predef_file"]}")
    begin
      return mkpart_from_namelistfile("#{@basedir}/#{config["predef_file"]}")
    rescue FileNotFound => err
      raise FileNotFound, "preface #{err.message}"
    end
  end
end

#prev_chapter(chapter) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/review/book/base.rb', line 150

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_APPENDIXObject



211
212
213
214
215
216
217
# File 'lib/review/book/base.rb', line 211

def read_APPENDIX
  if catalog
    catalog.appendix
  else
    read_FILE(config["postdef_file"]) # for backward compatibility
  end
end

#read_bibObject



245
246
247
# File 'lib/review/book/base.rb', line 245

def read_bib
  File.read("#{@basedir}/#{bib_file}")
end

#read_CHAPSObject



195
196
197
198
199
200
201
# File 'lib/review/book/base.rb', line 195

def read_CHAPS
  if catalog
    catalog.chaps
  else
    read_FILE(config["chapter_file"])
  end
end

#read_PARTObject



227
228
229
230
231
232
233
234
235
# File 'lib/review/book/base.rb', line 227

def read_PART
  return @read_part if @read_part

  if catalog
    @read_part = catalog.parts
  else
    @read_part = File.read("#{@basedir}/#{config["part_file"]}")
  end
end

#read_POSTDEFObject



219
220
221
222
223
224
225
# File 'lib/review/book/base.rb', line 219

def read_POSTDEF
  if catalog
    catalog.postdef
  else
    ""
  end
end

#read_PREDEFObject



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

def read_PREDEF
  if catalog
    catalog.predef
  else
    read_FILE(config["predef_file"])
  end
end

#reject_fileObject



57
58
59
# File 'lib/review/book/base.rb', line 57

def reject_file
  config["reject_file"]
end

#volumeObject



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

def volume
  vol = Volume.sum(chapters.map {|chap| chap.volume })
  vol.page_per_kbyte = page_metric.page_per_kbyte
  vol
end