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.



49
50
51
52
53
54
55
56
57
# File 'lib/review/book/base.rb', line 49

def initialize(basedir)
  @basedir = basedir
  @parts = nil
  @chapter_index = nil
  @config = ReVIEW::Configure.values
  @catalog = nil
  @read_part = nil
  @warn_old_files = {} # XXX for checking CHAPS, PREDEF, POSTDEF
end

Instance Attribute Details

#basedirObject (readonly)

Returns the value of attribute basedir.



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

def basedir
  @basedir
end

#catalogObject



183
184
185
186
187
188
189
# File 'lib/review/book/base.rb', line 183

def catalog
  return @catalog if @catalog.present?

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

#configObject



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

def config
  @config ||= Configure.values
end

#partsObject



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

def parts
  @parts ||= read_parts
end

Class Method Details

.clear_rubyenvObject



45
46
47
# File 'lib/review/book/base.rb', line 45

def self.clear_rubyenv
  @basedir_seen = {}
end

.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
  ReVIEW.logger.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
      ReVIEW.logger.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



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

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

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

#bib_exist?Boolean

Returns:

  • (Boolean)


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

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

#bib_fileObject



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

def bib_file
  config['bib_file']
end

#chapter(id) ⇒ Object



146
147
148
# File 'lib/review/book/base.rb', line 146

def chapter(id)
  chapter_index[id]
end

#chapter_indexObject



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

def chapter_index
  return @chapter_index if @chapter_index

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

#chaptersObject



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

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

#contentsObject



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

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

#each_chapter(&block) ⇒ Object



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

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

#each_chapter_r(&block) ⇒ Object



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

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

#each_part(&block) ⇒ Object



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

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

#extObject



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

def ext
  config['ext']
end

#htmlversionObject



93
94
95
96
97
98
99
# File 'lib/review/book/base.rb', line 93

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

#image_dirObject



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

def image_dir
  config['image_dir']
end

#image_typesObject



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

def image_types
  config['image_types']
end

#image_types=(types) ⇒ Object



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

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

#load_config(filename) ⇒ Object



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

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

#next_chapter(chapter) ⇒ Object



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

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



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

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

#part(n) ⇒ Object



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

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

#part_exist?Boolean

Returns:

  • (Boolean)


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

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

#parts_in_fileObject



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

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

#postscriptsObject



273
274
275
# File 'lib/review/book/base.rb', line 273

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

#prefacesObject



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

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

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

#prev_chapter(chapter) ⇒ Object



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

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



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

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

#read_bibObject



241
242
243
# File 'lib/review/book/base.rb', line 241

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

#read_chapsObject



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

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

#read_partObject



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

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



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

def read_postdef
  if catalog
    catalog.postdef
  else
    ''
  end
end

#read_predefObject



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

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

#reject_fileObject



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

def reject_file
  config['reject_file']
end

#volumeObject



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

def volume
  vol = Volume.sum(chapters.map(&:volume))
  vol.page_per_kbyte = page_metric.page_per_kbyte
  vol
end