Class: ReVIEW::Builder

Inherits:
Object show all
Includes:
TextUtils
Defined in:
lib/review/builder.rb

Direct Known Subclasses

HTMLBuilder, IDGXMLBuilder, LATEXBuilder, TOPBuilder

Constant Summary collapse

CAPTION_TITLES =
%w(note memo tip info planning best important security caution term link notice point shoot reference practice expert)

Instance Method Summary collapse

Methods included from TextUtils

#convert_inencoding, #convert_outencoding, #split_paragraph

Constructor Details

#initialize(strict = false, *args) ⇒ Builder

Returns a new instance of Builder.



30
31
32
33
34
35
36
37
# File 'lib/review/builder.rb', line 30

def initialize(strict = false, *args)
  @strict = strict
  @tabwidth = nil
  if ReVIEW.book.param && ReVIEW.book.param["tabwidth"]
    @tabwidth = ReVIEW.book.param["tabwidth"]
  end
  builder_init(*args)
end

Instance Method Details

#bibpaper(lines, id, caption) ⇒ Object



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

def bibpaper(lines, id, caption)
  bibpaper_header id, caption
  unless lines.empty?
    puts ""
    bibpaper_bibpaper id, caption, lines
  end
  puts ""
end

#bind(compiler, chapter, location) ⇒ Object



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

def bind(compiler, chapter, location)
  @compiler = compiler
  @chapter = chapter
  @location = location
  @output = StringIO.new
  @book = ReVIEW.book
  builder_init_file
end

#captionblock(type, lines, caption, specialstyle = nil) ⇒ Object

Raises:

  • (NotImplementedError)


356
357
358
# File 'lib/review/builder.rb', line 356

def captionblock(type, lines, caption, specialstyle = nil)
  raise NotImplementedError
end

#compile_inline(s) ⇒ Object

def footnote(id, str)

@footnotes.push [id, str]

end

def flush_footnote

footnote_begin
@footnotes.each do |id, str|
  footnote_item(id, str)
end
footnote_end

end



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

def compile_inline(s)
  @compiler.text(s)
end

#detab(str, num = nil) ⇒ Object

override TextUtils::detab



433
434
435
436
437
438
439
440
441
# File 'lib/review/builder.rb', line 433

def detab(str, num = nil)
  if num
    super(str, num)
  elsif @tabwidth
    super(str, @tabwidth)
  else
    super(str)
  end
end

#entriesObject



296
297
298
299
300
301
302
303
304
# File 'lib/review/builder.rb', line 296

def entries
  if ReVIEW.book.param["subdirmode"]
    @entries ||= Dir.entries(File.join(@book.basedir + @book.image_dir, @chapter.name))
  else
    @entries ||= Dir.entries(@book.basedir + @book.image_dir)
  end
rescue Errno::ENOENT
@entries = []
end

#error(msg) ⇒ Object

Raises:



310
311
312
# File 'lib/review/builder.rb', line 310

def error(msg)
  raise ApplicationError, "#{@location}: error: #{msg}"
end

#extract_chapter_id(chap_ref) ⇒ Object



347
348
349
350
351
352
353
354
# File 'lib/review/builder.rb', line 347

def extract_chapter_id(chap_ref)
  m = /\A([\w+-]+)\|(.+)/.match(chap_ref)
  if m
    return [@book.chapters.detect{|chap| chap.id == m[1]}, m[2]]
  else
    return [@chapter, chap_ref]
  end
end

#find_pathes(id) ⇒ Object



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/review/builder.rb', line 277

def find_pathes(id)
  if ReVIEW.book.param["subdirmode"]
    re = /\A#{id}(?i:#{@chapter.name.join('|')})\z/x
    entries().select {|ent| re =~ ent }\
      .sort_by {|ent| @book.image_types.index(File.extname(ent).downcase) }\
      .map {|ent| "#{@book.basedir}/#{@chapter.name}/#{ent}" }
  elsif ReVIEW.book.param["singledirmode"]
    re = /\A#{id}(?i:#{@chapter.name.join('|')})\z/x
    entries().select {|ent| re =~ ent }\
      .sort_by {|ent| @book.image_types.index(File.extname(ent).downcase) }\
      .map {|ent| "#{@book.basedir}/#{ent}" }
  else
    re = /\A#{@chapter.name}-#{id}(?i:#{@book.image_types.join('|')})\z/x
    entries().select {|ent| re =~ ent }\
      .sort_by {|ent| @book.image_types.index(File.extname(ent).downcase) }\
      .map {|ent| "#{@book.basedir}/#{ent}" }
  end
end

#get_chap(chapter = @chapter) ⇒ Object



340
341
342
343
344
345
# File 'lib/review/builder.rb', line 340

def get_chap(chapter = @chapter)
  if ReVIEW.book.param["secnolevel"] > 0 && !chapter.number.nil? && !chapter.number.to_s.empty?
    return "#{chapter.number}"
  end
  return nil
end

#graph(lines, id, command, caption = nil) ⇒ Object



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/review/builder.rb', line 368

def graph(lines, id, command, caption = nil)
  dir = @book.basedir + @book.image_dir
  file = "#{@chapter.name}-#{id}.#{image_ext}"
  if ReVIEW.book.param["subdirmode"]
    dir = File.join(dir, @chapter.name)
    file = "#{id}.#{image_ext}"
  elsif ReVIEW.book.param["singledirmode"]
    file = "#{id}.#{image_ext}"
  end
  file_path = File.join(dir, file)

  line = CGI.unescapeHTML(lines.join("\n"))
  cmds = {
    :graphviz => "echo '#{line}' | dot -T#{image_ext} -o#{file_path}",
    :gnuplot  => "echo 'set terminal " +
    "#{(image_ext == "eps") ? "postscript eps" : image_ext}\n" +
    " set output \"#{file_path}\"\n#{line}' | gnuplot",
    :blockdiag => "echo '#{line}' "+
    "| blockdiag -a -T #{image_ext} -o #{file_path} /dev/stdin",
    :aafigure => "echo '#{line}' | aafigure -t#{image_ext} -o#{file_path}",
  }
  cmd = cmds[command.to_sym]
  warn cmd
  system cmd

  image(lines, id, caption)
end

#handle_metric(str) ⇒ Object



314
315
316
# File 'lib/review/builder.rb', line 314

def handle_metric(str)
  str
end

#image(lines, id, caption, metric = nil) ⇒ Object



97
98
99
100
101
102
103
104
# File 'lib/review/builder.rb', line 97

def image(lines, id, caption, metric = nil)
  if @chapter.image(id).bound?
    image_image id, caption, metric
  else
    warn "image not bound: #{id}" if @strict
    image_dummy id, caption, lines
  end
end

#image_extObject

Raises:

  • (NotImplementedError)


396
397
398
# File 'lib/review/builder.rb', line 396

def image_ext
  raise NotImplementedError
end

#include(file_name) ⇒ Object



405
406
407
408
409
# File 'lib/review/builder.rb', line 405

def include(file_name)
  File.foreach(file_name) do |line|
    paragraph([convert_inencoding(line, ReVIEW.book.param["inencoding"])])
  end
end

#inline_bou(str) ⇒ Object



222
223
224
# File 'lib/review/builder.rb', line 222

def inline_bou(str)
  text(str)
end

#inline_chap(id) ⇒ Object



180
181
182
183
184
185
# File 'lib/review/builder.rb', line 180

def inline_chap(id)
  @chapter.env.chapter_index.number(id)
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_chapref(id) ⇒ Object



173
174
175
176
177
178
# File 'lib/review/builder.rb', line 173

def inline_chapref(id)
  @chapter.env.chapter_index.display_string(id)
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#inline_fn(id) ⇒ Object



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

def inline_fn(id)
  @chapter.footnote(id).content
rescue KeyError
  error "unknown footnote: #{id}"
  nofunc_text("[UnknownFootnote:#{id}]")
end

#inline_hd(id) ⇒ Object



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

def inline_hd(id)
  m = /\A(\w+)\|(.+)/.match(id)
  chapter = @book.chapters.detect{|chap| chap.id == m[1]} if m && m[1]
  return inline_hd_chap(chapter, m[2]) if chapter
  return inline_hd_chap(@chapter, id)
end

#inline_href(arg) ⇒ Object



236
237
238
239
240
241
# File 'lib/review/builder.rb', line 236

def inline_href(arg)
  url, label = *arg.scan(/(?:(?:(?:\\\\)*\\,)|[^,\\]+)+/).map(&:lstrip)
  url = url.gsub(/\\,/, ",").strip
  label = label.gsub(/\\,/, ",").strip if label
  compile_href(url, label)
end

#inline_img(id) ⇒ Object



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

def inline_img(id)
  "#{I18n.t("image")}#{@chapter.image(id).number}"
rescue KeyError
  error "unknown image: #{id}"
  nofunc_text("[UnknownImage:#{id}]")
end

#inline_include(file_name) ⇒ Object



400
401
402
403
# File 'lib/review/builder.rb', line 400

def inline_include(file_name)
  compile_inline convert_inencoding(File.open(file_name).read,
                                    ReVIEW.book.param["inencoding"])
end

#inline_kw(arg) ⇒ Object



231
232
233
234
# File 'lib/review/builder.rb', line 231

def inline_kw(arg)
  word, alt = *arg.split(',', 2)
  compile_kw(word, alt)
end

#inline_list(id) ⇒ Object



194
195
196
197
198
199
# File 'lib/review/builder.rb', line 194

def inline_list(id)
  "#{I18n.t("list")}#{@chapter.list(id).number}"
rescue KeyError
  error "unknown list: #{id}"
  nofunc_text("[UnknownList:#{id}]")
end

#inline_raw(args) ⇒ Object



418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/review/builder.rb', line 418

def inline_raw(args)
  if matched = args.match(/\|(.*?)\|(.*)/)
    builders = matched[1].split(/,/).map{|i| i.gsub(/\s/, '') }
    c = self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase
    if builders.include?(c)
      matched[2].gsub("\\n", "\n")
    else
      ""
    end
  else
    args.gsub("\\n", "\n")
  end
end

#inline_ruby(arg) ⇒ Object



226
227
228
229
# File 'lib/review/builder.rb', line 226

def inline_ruby(arg)
  base, ruby = *arg.split(',', 2)
  compile_ruby(base, ruby)
end

#inline_table(id) ⇒ Object



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

def inline_table(id)
  "#{I18n.t("table")}#{@chapter.table(id).number}"
rescue KeyError
  error "unknown table: #{id}"
  nofunc_text("[UnknownTable:#{id}]")
end

#inline_title(id) ⇒ Object



187
188
189
190
191
192
# File 'lib/review/builder.rb', line 187

def inline_title(id)
  @chapter.env.chapter_index.title(id)
rescue KeyError
  error "unknown chapter: #{id}"
  nofunc_text("[UnknownChapter:#{id}]")
end

#list(lines, id, caption) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/review/builder.rb', line 74

def list(lines, id, caption)
  begin
    list_header id, caption
  rescue KeyError
    error "no such list: #{id}"
  end
  list_body lines
end

#listnum(lines, id, caption) ⇒ Object



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

def listnum(lines, id, caption)
  begin
    list_header id, caption
  rescue KeyError
    error "no such list: #{id}"
  end
  listnum_body lines
end

#parse_metric(type, metric) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/review/builder.rb', line 322

def parse_metric(type, metric)
  return "" if metric.blank?
  params = metric.split(/,\s*/)
  results = []
  params.each do |p|
    if p =~ /\A.+?::/
      if p =~ /\A#{type}::/
          p.sub!(/\A#{type}::/, '')
      else
        next
      end
    end
    p = handle_metric(p)
    results.push(p)
  end
  return result_metric(results)
end

#post_paragraphObject



26
27
28
# File 'lib/review/builder.rb', line 26

def post_paragraph
  nil
end

#pre_paragraphObject



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

def pre_paragraph
  nil
end


62
63
64
65
66
# File 'lib/review/builder.rb', line 62

def print(*s)
  @output.print *s.map{|i|
    convert_outencoding(i, ReVIEW.book.param["outencoding"])
  }
end

#puts(*s) ⇒ Object



68
69
70
71
72
# File 'lib/review/builder.rb', line 68

def puts(*s)
  @output.puts *s.map{|i|
    convert_outencoding(i, ReVIEW.book.param["outencoding"])
  }
end

#raw(str) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/review/builder.rb', line 263

def raw(str)
  if matched = str.match(/\|(.*?)\|(.*)/)
    builders = matched[1].split(/,/).map{|i| i.gsub(/\s/, '') }
    c = self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase
    if builders.include?(c)
      print matched[2].gsub("\\n", "\n")
    else
      ""
    end
  else
    print str.gsub("\\n", "\n")
  end
end

#resultObject Also known as: raw_result



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

def result
  @output.string
end

#result_metric(array) ⇒ Object



318
319
320
# File 'lib/review/builder.rb', line 318

def result_metric(array)
  array.join(',')
end

#source(lines, caption) ⇒ Object



92
93
94
95
# File 'lib/review/builder.rb', line 92

def source(lines, caption)
  source_header caption
  source_body lines
end

#table(lines, id = nil, caption = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/review/builder.rb', line 106

def table(lines, id = nil, caption = nil)
  rows = []
  sepidx = nil
  lines.each_with_index do |line, idx|
    if /\A[\=\-]{12}/ =~ line
      # just ignore
      #error "too many table separator" if sepidx
      sepidx ||= idx
      next
    end
    rows.push line.strip.split(/\t+/).map {|s| s.sub(/\A\./, '') }
  end
  rows = adjust_n_cols(rows)

  begin
    table_header id, caption unless caption.nil?
  rescue KeyError => err
    error "no such table: #{id}"
  end
  return if rows.empty?
  table_begin rows.first.size
  if sepidx
    sepidx.times do
      tr rows.shift.map {|s| th(s) }
    end
    rows.each do |cols|
      tr cols.map {|s| td(s) }
    end
  else
    rows.each do |cols|
      h, *cs = *cols
      tr [th(h)] + cs.map {|s| td(s) }
    end
  end
  table_end
end

#text(str) ⇒ Object



243
244
245
# File 'lib/review/builder.rb', line 243

def text(str)
  str
end

#ul_item_begin(lines) ⇒ Object



411
412
413
# File 'lib/review/builder.rb', line 411

def ul_item_begin(lines)
  ul_item(lines)
end

#ul_item_endObject



415
416
# File 'lib/review/builder.rb', line 415

def ul_item_end
end

#warn(msg) ⇒ Object



306
307
308
# File 'lib/review/builder.rb', line 306

def warn(msg)
  $stderr.puts "#{@location}: warning: #{msg}"
end