Class: ReVIEW::Builder

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Builder.



35
36
37
38
# File 'lib/review/builder.rb', line 35

def initialize(strict = false, *args)
  @strict = strict
  builder_init(*args)
end

Instance Attribute Details

#astObject

Returns the value of attribute ast.



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

def ast
  @ast
end

#outputObject

Returns the value of attribute output.



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

def output
  @output
end

Instance Method Details

#bibpaper(lines, id, caption) ⇒ Object



283
284
285
286
287
288
289
290
291
292
# File 'lib/review/builder.rb', line 283

def bibpaper(lines, id, caption)
  buf = ""
  buf << bibpaper_header(id, caption)
  unless lines.empty?
    buf << "\n"
    buf << bibpaper_bibpaper(id, caption, lines)
  end
  buf << "\n"
  buf
end

#bind(compiler, chapter, location) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/review/builder.rb', line 44

def bind(compiler, chapter, location)
  @compiler = compiler
  @chapter = chapter
  @location = location
  @ast = nil
  @output = StringIO.new
  @book = @chapter.book if @chapter.present?
  @tabwidth = nil
  if @book && @book.config && @book.config["tabwidth"]
    @tabwidth = @book.config["tabwidth"]
  end
  builder_init_file
end

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

Raises:

  • (NotImplementedError)


373
374
375
# File 'lib/review/builder.rb', line 373

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

#detab(str, num = nil) ⇒ Object

override TextUtils::detab



447
448
449
450
451
452
453
454
455
# File 'lib/review/builder.rb', line 447

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

#error(msg) ⇒ Object

Raises:



327
328
329
# File 'lib/review/builder.rb', line 327

def error(msg)
  raise ApplicationError, "error: #{msg} at #{@compiler.show_pos} \n  (#{@compiler.failure_oneline})"
end

#extract_chapter_id(chap_ref) ⇒ Object



364
365
366
367
368
369
370
371
# File 'lib/review/builder.rb', line 364

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

#get_chap(chapter = @chapter) ⇒ Object



357
358
359
360
361
362
# File 'lib/review/builder.rb', line 357

def get_chap(chapter = @chapter)
  if @book.config["secnolevel"] > 0 && !chapter.number.nil? && !chapter.number.to_s.empty?
    return chapter.format_number(nil)
  end
  return nil
end

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



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/review/builder.rb', line 385

def graph(lines, id, command, caption = nil)
  c = target_name
  dir = File.join(@book.basedir, @book.image_dir, c)
  Dir.mkdir(dir) unless File.exist?(dir)
  file = "#{id}.#{image_ext}"
  file_path = File.join(dir, file)

  line = self.unescape(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
  @chapter.image_index.image_finder.add_entry(file_path)

  image(lines, id, caption ||= "")
end

#handle_metric(str) ⇒ Object



331
332
333
# File 'lib/review/builder.rb', line 331

def handle_metric(str)
  str
end

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



118
119
120
121
122
123
124
125
# File 'lib/review/builder.rb', line 118

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)


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

def image_ext
  raise NotImplementedError
end

#include(file_name) ⇒ Object



418
419
420
421
422
# File 'lib/review/builder.rb', line 418

def include(file_name)
  File.foreach(file_name) do |line|
    paragraph([line])
  end
end

#inline_bou(str) ⇒ Object



261
262
263
# File 'lib/review/builder.rb', line 261

def inline_bou(str)
  text(str)
end

#inline_chap(id) ⇒ Object



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

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

#inline_chapref(id) ⇒ Object

def compile_inline(s)

  @compiler.text(s)
end


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

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

#inline_column(id) ⇒ Object



302
303
304
305
306
307
# File 'lib/review/builder.rb', line 302

def inline_column(id)
  @chapter.column(id).caption
rescue
  error "unknown column: #{id}"
  nofunc_text("[UnknownColumn:#{id}]")
end

#inline_fn(id) ⇒ Object



254
255
256
257
258
259
# File 'lib/review/builder.rb', line 254

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

#inline_href(url, label = nil) ⇒ Object



273
274
275
276
277
# File 'lib/review/builder.rb', line 273

def inline_href(url, label = nil)
  url = url.strip
  label = label.strip if label
  compile_href(url, label)
end

#inline_img(id) ⇒ Object



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

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



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

def inline_include(file_name)
  File.open(file_name).read
end

#inline_kw(word, alt = nil) ⇒ Object



269
270
271
# File 'lib/review/builder.rb', line 269

def inline_kw(word, alt = nil)
  compile_kw(word, alt)
end

#inline_list(id) ⇒ Object



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

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



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

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(base, ruby) ⇒ Object



265
266
267
# File 'lib/review/builder.rb', line 265

def inline_ruby(base, ruby)
  compile_ruby(base, ruby)
end

#inline_table(id) ⇒ Object



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

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



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

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

#list(lines, id, caption = nil, lang = nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/review/builder.rb', line 89

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

#listnum(lines, id, caption = nil, lang = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'lib/review/builder.rb', line 100

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

#node_inline_hd(nodelist) ⇒ Object



294
295
296
297
298
299
300
# File 'lib/review/builder.rb', line 294

def node_inline_hd(nodelist)
  id = nodelist[0].to_raw
  m = /\A([^|]+)\|(.+)/.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

#node_inline_img(node) ⇒ Object



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

def node_inline_img(node)
  id = node[0].to_raw
  inline_img(id)
end

#node_inline_imgref(node) ⇒ Object



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

def node_inline_imgref(node)
  id = node[0].to_raw
  img = node_inline_img(node)

  if @chapter.image(id).caption
    "#{img}#{I18n.t('image_quote', @chapter.image(id).caption)}"
  else
    img
  end
end

#parse_metric(type, metric) ⇒ Object



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/review/builder.rb', line 339

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



31
32
33
# File 'lib/review/builder.rb', line 31

def post_paragraph
  nil
end

#pre_paragraphObject



28
29
30
# File 'lib/review/builder.rb', line 28

def pre_paragraph
  nil
end

Raises:

  • (NotImplementedError)


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

def print(*s)
  raise NotImplementedError, "XXX: `print` method is obsoleted. Do not use it."
end

#puts(*s) ⇒ Object

Raises:

  • (NotImplementedError)


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

def puts(*s)
  raise NotImplementedError, "XXX: `puts` method is obsoleted. Do not use it."
end

#raw(str) ⇒ Object



309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/review/builder.rb', line 309

def raw(str)
  if matched = str.match(/\|(.*?)\|(.*)/)
    builders = matched[1].split(/,/).map{|i| i.gsub(/\s/, '') }
    c = target_name
    if builders.include?(c)
      matched[2].gsub("\\n", "\n")
    else
      ""
    end
  else
    str.gsub("\\n", "\n")
  end
end

#resultObject Also known as: raw_result



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

def result
  @output.string
end

#result_metric(array) ⇒ Object



335
336
337
# File 'lib/review/builder.rb', line 335

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

#source(lines, caption = nil) ⇒ Object



111
112
113
114
115
116
# File 'lib/review/builder.rb', line 111

def source(lines, caption = nil)
  buf = ""
  buf << source_header(caption)
  buf << source_body(lines, lang)
  buf
end

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



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/review/builder.rb', line 127

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

#target_nameObject



77
78
79
# File 'lib/review/builder.rb', line 77

def target_name
  self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase
end

#text(str) ⇒ Object



279
280
281
# File 'lib/review/builder.rb', line 279

def text(str)
  str
end

#ul_item_begin(lines) ⇒ Object



424
425
426
# File 'lib/review/builder.rb', line 424

def ul_item_begin(lines)
  ul_item(lines)
end

#ul_item_endObject



428
429
430
# File 'lib/review/builder.rb', line 428

def ul_item_end
  ""
end

#warn(msg) ⇒ Object



323
324
325
# File 'lib/review/builder.rb', line 323

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