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) ⇒ Base

Returns a new instance of Base.



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

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

Instance Attribute Details

#configObject



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

def config
  @config ||= Configure.values
end

Class Method Details

.load(dir = ".") ⇒ Object



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

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

.load_defaultObject



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

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

.update_rubyenv(dir) ⇒ Object



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

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



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/review/book/base.rb', line 273

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



297
298
299
# File 'lib/review/book/base.rb', line 297

def basedir
  @basedir
end

#bib_exist?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/review/book/base.rb', line 255

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

#bib_fileObject



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

def bib_file
  config["bib_file"]
end

#catalogObject



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

def catalog
  return @catalog if @catalog.present?

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

  @catalog
end

#catalog=(catalog) ⇒ Object



197
198
199
# File 'lib/review/book/base.rb', line 197

def catalog=(catalog)
  @catalog = catalog
end

#chapter(id) ⇒ Object



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

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

#chapter_indexObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/review/book/base.rb', line 137

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



125
126
127
# File 'lib/review/book/base.rb', line 125

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

#contentsObject



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

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

#each_chapter(&block) ⇒ Object



129
130
131
# File 'lib/review/book/base.rb', line 129

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

#each_chapter_r(&block) ⇒ Object



133
134
135
# File 'lib/review/book/base.rb', line 133

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

#each_part(&block) ⇒ Object



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

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

#extObject



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

def ext
  config["ext"]
end

#htmlversionObject



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

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

#image_dirObject



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

def image_dir
  config["image_dir"]
end

#image_typesObject



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

def image_types
  config["image_types"]
end

#image_types=(types) ⇒ Object



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

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

#load_config(filename) ⇒ Object



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

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

#next_chapter(chapter) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/review/book/base.rb', line 153

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



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

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

#part(n) ⇒ Object



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

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

#part_exist?Boolean

Returns:

  • (Boolean)


243
244
245
246
247
248
249
# File 'lib/review/book/base.rb', line 243

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

#partsObject



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

def parts
  @parts ||= read_parts()
end

#parts=(parts) ⇒ Object



98
99
100
# File 'lib/review/book/base.rb', line 98

def parts=(parts)
  @parts = parts
end

#parts_in_fileObject



102
103
104
105
106
# File 'lib/review/book/base.rb', line 102

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

#postscriptsObject



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

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

#prefacesObject



259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/review/book/base.rb', line 259

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



162
163
164
165
166
167
168
169
# File 'lib/review/book/base.rb', line 162

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



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

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

#read_bibObject



251
252
253
# File 'lib/review/book/base.rb', line 251

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

#read_CHAPSObject



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

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

#read_PARTObject



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

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



225
226
227
228
229
230
231
# File 'lib/review/book/base.rb', line 225

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

#read_PREDEFObject



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

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

#reject_fileObject



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

def reject_file
  config["reject_file"]
end

#volumeObject



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

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