Class: ReVIEW::Builder
- Includes:
- TextUtils
- Defined in:
- lib/review/builder.rb
Direct Known Subclasses
HTMLBuilder, IDGXMLBuilder, LATEXBuilder, MARKDOWNBuilder, PLAINTEXTBuilder, RSTBuilder
Constant Summary collapse
- CAPTION_TITLES =
%w[note memo tip info warning important caution notice].freeze
Instance Attribute Summary collapse
-
#doc_status ⇒ Object
Returns the value of attribute doc_status.
Instance Method Summary collapse
- #bibpaper(lines, id, caption) ⇒ Object
- #bind(compiler, chapter, location) ⇒ Object
-
#blankline ⇒ Object
def footnote(id, str) @footnotes.push [id, str] end.
- #captionblock(_type, _lines, _caption, _specialstyle = nil) ⇒ Object
- #compile_inline(s) ⇒ Object
-
#detab(str, num = nil) ⇒ Object
override TextUtils::detab.
- #embed(lines, arg = nil) ⇒ Object
- #emtable(lines, caption = nil) ⇒ Object
- #error(msg) ⇒ Object
- #escape(str) ⇒ Object
- #extract_chapter_id(chap_ref) ⇒ Object
-
#firstlinenum(num) ⇒ Object
for //firstlinenum.
- #get_chap(chapter = @chapter) ⇒ Object
- #graph(lines, id, command, caption = '') ⇒ Object
- #graph_aafigure(id, file_path, _line, tf_path) ⇒ Object
- #graph_blockdiag(id, file_path, _line, tf_path) ⇒ Object
- #graph_gnuplot(id, file_path, line, tf_path) ⇒ Object
- #graph_graphviz(id, file_path, _line, tf_path) ⇒ Object
- #graph_plantuml(id, file_path, _line, tf_path) ⇒ Object
- #handle_metric(str) ⇒ Object
- #image(lines, id, caption, metric = nil) ⇒ Object
- #image_ext ⇒ Object
-
#initialize(strict = false, *args) ⇒ Builder
constructor
A new instance of Builder.
- #inline_balloon(arg) ⇒ Object
- #inline_bou(str) ⇒ Object
- #inline_chap(id) ⇒ Object
- #inline_chapref(id) ⇒ Object
- #inline_column(id) ⇒ Object
- #inline_column_chap(chapter, id) ⇒ Object
- #inline_embed(args) ⇒ Object
- #inline_eq(id) ⇒ Object
- #inline_fn(id) ⇒ Object
- #inline_hd(id) ⇒ Object
- #inline_href(arg) ⇒ Object
- #inline_img(id) ⇒ Object
- #inline_imgref(id) ⇒ Object
- #inline_include(file_name) ⇒ Object
- #inline_kw(arg) ⇒ Object
- #inline_list(id) ⇒ Object
- #inline_pageref(id) ⇒ Object
- #inline_raw(args) ⇒ Object
- #inline_ruby(arg) ⇒ Object
- #inline_table(id) ⇒ Object
- #inline_tcy(arg) ⇒ Object
- #inline_title(id) ⇒ Object
- #inline_w(s) ⇒ Object
- #inline_wb(s) ⇒ Object
- #line_num ⇒ Object
- #list(lines, id, caption, lang = nil) ⇒ Object
- #listnum(lines, id, caption, lang = nil) ⇒ Object
- #load_words(file) ⇒ Object
- #parse_metric(type, metric) ⇒ Object
- #post_paragraph ⇒ Object
- #pre_paragraph ⇒ Object
- #print(*s) ⇒ Object
- #puts(*s) ⇒ Object
- #raw(str) ⇒ Object
- #result ⇒ Object (also: #raw_result)
- #result_metric(array) ⇒ Object
- #source(lines, caption, lang = nil) ⇒ Object
- #system_graph(id, *args) ⇒ Object
- #table(lines, id = nil, caption = nil) ⇒ Object
- #target_name ⇒ Object
- #text(str) ⇒ Object
- #tsize(str) ⇒ Object
- #ul_item_begin(lines) ⇒ Object
- #ul_item_end ⇒ Object
- #unescape(str) ⇒ Object
- #warn(msg) ⇒ Object
Methods included from TextUtils
Constructor Details
Instance Attribute Details
#doc_status ⇒ Object
Returns the value of attribute doc_status.
33 34 35 |
# File 'lib/review/builder.rb', line 33 def doc_status @doc_status end |
Instance Method Details
#bibpaper(lines, id, caption) ⇒ Object
343 344 345 346 347 348 349 350 |
# File 'lib/review/builder.rb', line 343 def bibpaper(lines, id, ) bibpaper_header id, unless lines.empty? puts bibpaper_bibpaper id, , lines end puts end |
#bind(compiler, chapter, location) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/review/builder.rb', line 48 def bind(compiler, chapter, location) @compiler = compiler @chapter = chapter @location = location @output = StringIO.new if @chapter.present? @book = @chapter.book end @tabwidth = nil @tsize = nil if @book && @book.config if @book.config['words_file'] load_words(@book.config['words_file']) end if @book.config['tabwidth'] @tabwidth = @book.config['tabwidth'] end end builder_init_file end |
#blankline ⇒ 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
224 225 226 |
# File 'lib/review/builder.rb', line 224 def blankline puts '' end |
#captionblock(_type, _lines, _caption, _specialstyle = nil) ⇒ Object
486 487 488 |
# File 'lib/review/builder.rb', line 486 def (_type, _lines, , _specialstyle = nil) raise NotImplementedError end |
#compile_inline(s) ⇒ Object
228 229 230 |
# File 'lib/review/builder.rb', line 228 def compile_inline(s) @compiler.text(s) end |
#detab(str, num = nil) ⇒ Object
override TextUtils::detab
618 619 620 621 622 623 624 625 626 |
# File 'lib/review/builder.rb', line 618 def detab(str, num = nil) if num super(str, num) elsif @tabwidth super(str, @tabwidth) else super(str) end end |
#embed(lines, arg = nil) ⇒ Object
422 423 424 425 426 427 428 429 430 |
# File 'lib/review/builder.rb', line 422 def (lines, arg = nil) if arg builders = arg.gsub(/^\s*\|/, '').gsub(/\|\s*$/, '').gsub(/\s/, '').split(',') c = target_name print lines.join if builders.include?(c) else print lines.join end end |
#emtable(lines, caption = nil) ⇒ Object
208 209 210 |
# File 'lib/review/builder.rb', line 208 def emtable(lines, = nil) table(lines, nil, ) end |
#error(msg) ⇒ Object
436 437 438 439 440 441 442 |
# File 'lib/review/builder.rb', line 436 def error(msg) if msg =~ /:\d+: error: / raise ApplicationError, msg else raise ApplicationError, "#{@location}: error: #{msg}" end end |
#escape(str) ⇒ Object
628 629 630 |
# File 'lib/review/builder.rb', line 628 def escape(str) str end |
#extract_chapter_id(chap_ref) ⇒ Object
478 479 480 481 482 483 484 |
# File 'lib/review/builder.rb', line 478 def extract_chapter_id(chap_ref) m = /\A([\w+-]+)\|(.+)/.match(chap_ref) if m return [@book.contents.detect { |chap| chap.id == m[1] }, m[2]] end [@chapter, chap_ref] end |
#firstlinenum(num) ⇒ Object
for //firstlinenum
111 112 113 |
# File 'lib/review/builder.rb', line 111 def firstlinenum(num) @first_line_num = num.to_i end |
#get_chap(chapter = @chapter) ⇒ Object
467 468 469 470 471 472 473 474 475 476 |
# File 'lib/review/builder.rb', line 467 def get_chap(chapter = @chapter) if @book.config['secnolevel'] > 0 && !chapter.number.nil? && !chapter.number.to_s.empty? if chapter.is_a?(ReVIEW::Book::Part) return I18n.t('part_short', chapter.number) else return chapter.format_number(nil) end end nil end |
#graph(lines, id, command, caption = '') ⇒ Object
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/review/builder.rb', line 498 def graph(lines, id, command, = '') c = target_name dir = File.join(@book.imagedir, c) FileUtils.mkdir_p(dir) file = "#{id}.#{image_ext}" file_path = File.join(dir, file) line = self.unescape(lines.join("\n")) tf = Tempfile.new('review_graph') tf.puts line tf.close begin file_path = send("graph_#{command}".to_sym, id, file_path, line, tf.path) ensure tf.unlink end @chapter.image_index.image_finder.add_entry(file_path) image(lines, id, ) end |
#graph_aafigure(id, file_path, _line, tf_path) ⇒ Object
547 548 549 550 |
# File 'lib/review/builder.rb', line 547 def graph_aafigure(id, file_path, _line, tf_path) system_graph(id, 'aafigure', '-t', image_ext, '-o', file_path, tf_path) file_path end |
#graph_blockdiag(id, file_path, _line, tf_path) ⇒ Object
542 543 544 545 |
# File 'lib/review/builder.rb', line 542 def graph_blockdiag(id, file_path, _line, tf_path) system_graph(id, 'blockdiag', '-a', '-T', image_ext, '-o', file_path, tf_path) file_path end |
#graph_gnuplot(id, file_path, line, tf_path) ⇒ Object
530 531 532 533 534 535 536 537 538 539 540 |
# File 'lib/review/builder.rb', line 530 def graph_gnuplot(id, file_path, line, tf_path) File.open(tf_path, 'w') do |tf| tf.puts <<EOTGNUPLOT set terminal #{image_ext == 'eps' ? 'postscript eps' : image_ext} set output "#{file_path}" #{line} EOTGNUPLOT end system_graph(id, 'gnuplot', tf_path) file_path end |
#graph_graphviz(id, file_path, _line, tf_path) ⇒ Object
525 526 527 528 |
# File 'lib/review/builder.rb', line 525 def graph_graphviz(id, file_path, _line, tf_path) system_graph(id, 'dot', "-T#{image_ext}", "-o#{file_path}", tf_path) file_path end |
#graph_plantuml(id, file_path, _line, tf_path) ⇒ Object
552 553 554 555 556 557 558 559 560 561 |
# File 'lib/review/builder.rb', line 552 def graph_plantuml(id, file_path, _line, tf_path) ext = image_ext if ext == 'pdf' ext = 'eps' file_path.sub!(/\.pdf\Z/, '.eps') end system_graph(id, 'java', '-jar', 'plantuml.jar', "-t#{ext}", '-charset', 'UTF-8', tf_path) FileUtils.mv "#{tf_path}.#{ext}", file_path file_path end |
#handle_metric(str) ⇒ Object
444 445 446 |
# File 'lib/review/builder.rb', line 444 def handle_metric(str) str end |
#image(lines, id, caption, metric = nil) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/review/builder.rb', line 146 def image(lines, id, , metric = nil) if @chapter.image(id).bound? image_image id, , metric else warn "image not bound: #{id}" if @strict image_dummy id, , lines end end |
#image_ext ⇒ Object
563 564 565 |
# File 'lib/review/builder.rb', line 563 def image_ext raise NotImplementedError end |
#inline_balloon(arg) ⇒ Object
392 393 394 |
# File 'lib/review/builder.rb', line 392 def inline_balloon(arg) "← #{arg}" end |
#inline_bou(str) ⇒ Object
310 311 312 |
# File 'lib/review/builder.rb', line 310 def inline_bou(str) text(str) end |
#inline_chap(id) ⇒ Object
238 239 240 241 242 |
# File 'lib/review/builder.rb', line 238 def inline_chap(id) @book.chapter_index.number(id) rescue KeyError error "unknown chapter: #{id}" end |
#inline_chapref(id) ⇒ Object
232 233 234 235 236 |
# File 'lib/review/builder.rb', line 232 def inline_chapref(id) compile_inline @book.chapter_index.display_string(id) rescue KeyError error "unknown chapter: #{id}" end |
#inline_column(id) ⇒ Object
366 367 368 369 370 371 372 373 374 375 376 377 378 |
# File 'lib/review/builder.rb', line 366 def inline_column(id) m = /\A([^|]+)\|(.+)/.match(id) if m && m[1] chapter = @book.chapters.detect { |chap| chap.id == m[1] } end if chapter inline_column_chap(chapter, m[2]) else inline_column_chap(@chapter, id) end rescue KeyError error "unknown column: #{id}" end |
#inline_column_chap(chapter, id) ⇒ Object
380 381 382 |
# File 'lib/review/builder.rb', line 380 def inline_column_chap(chapter, id) I18n.t('column', chapter.column(id).) end |
#inline_embed(args) ⇒ Object
604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/review/builder.rb', line 604 def (args) if matched = args.match(/\|(.*?)\|(.*)/) builders = matched[1].split(',').map { |i| i.gsub(/\s/, '') } if builders.include?(target_name) matched[2] else '' end else args end end |
#inline_eq(id) ⇒ Object
293 294 295 296 297 298 299 300 301 302 |
# File 'lib/review/builder.rb', line 293 def inline_eq(id) chapter, id = extract_chapter_id(id) if get_chap(chapter) %Q(#{I18n.t('equation')}#{I18n.t('format_number', [get_chap(chapter), chapter.equation(id).number])}) else %Q(#{I18n.t('equation')}#{I18n.t('format_number_without_chapter', [chapter.equation(id).number])}) end rescue KeyError error "unknown equation: #{id}" end |
#inline_fn(id) ⇒ Object
304 305 306 307 308 |
# File 'lib/review/builder.rb', line 304 def inline_fn(id) @chapter.footnote(id).content rescue KeyError error "unknown footnote: #{id}" end |
#inline_hd(id) ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/review/builder.rb', line 352 def inline_hd(id) m = /\A([^|]+)\|(.+)/.match(id) if m && m[1] chapter = @book.contents.detect { |chap| chap.id == m[1] } end if chapter inline_hd_chap(chapter, m[2]) else inline_hd_chap(@chapter, id) end rescue KeyError error "unknown headline: #{id}" end |
#inline_href(arg) ⇒ Object
330 331 332 333 334 335 336 337 |
# File 'lib/review/builder.rb', line 330 def inline_href(arg) url, label = *arg.scan(/(?:(?:(?:\\\\)*\\,)|[^,\\]+)+/).map(&:lstrip) url = url.gsub(/\\,/, ',').strip if label label = label.gsub(/\\,/, ',').strip end compile_href(url, label) end |
#inline_img(id) ⇒ Object
261 262 263 264 265 266 267 268 269 270 |
# File 'lib/review/builder.rb', line 261 def inline_img(id) chapter, id = extract_chapter_id(id) if get_chap(chapter) %Q(#{I18n.t('image')}#{I18n.t('format_number', [get_chap(chapter), chapter.image(id).number])}) else %Q(#{I18n.t('image')}#{I18n.t('format_number_without_chapter', [chapter.image(id).number])}) end rescue KeyError error "unknown image: #{id}" end |
#inline_imgref(id) ⇒ Object
272 273 274 275 276 277 278 279 280 |
# File 'lib/review/builder.rb', line 272 def inline_imgref(id) img = inline_img(id) if @chapter.image(id). "#{img}#{I18n.t('image_quote', @chapter.image(id).)}" else img end end |
#inline_include(file_name) ⇒ Object
567 568 569 |
# File 'lib/review/builder.rb', line 567 def inline_include(file_name) compile_inline File.read(file_name, mode: 'rt:BOM|utf-8').chomp end |
#inline_kw(arg) ⇒ Object
325 326 327 328 |
# File 'lib/review/builder.rb', line 325 def inline_kw(arg) word, alt = *arg.split(',', 2) compile_kw(word, alt) end |
#inline_list(id) ⇒ Object
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/review/builder.rb', line 250 def inline_list(id) chapter, id = extract_chapter_id(id) if get_chap(chapter) %Q(#{I18n.t('list')}#{I18n.t('format_number', [get_chap(chapter), chapter.list(id).number])}) else %Q(#{I18n.t('list')}#{I18n.t('format_number_without_chapter', [chapter.list(id).number])}) end rescue KeyError error "unknown list: #{id}" end |
#inline_pageref(id) ⇒ Object
384 385 386 |
# File 'lib/review/builder.rb', line 384 def inline_pageref(id) "[link:#{id}]" end |
#inline_raw(args) ⇒ Object
590 591 592 593 594 595 596 597 598 599 600 601 602 |
# File 'lib/review/builder.rb', line 590 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
314 315 316 317 318 319 320 321 322 323 |
# File 'lib/review/builder.rb', line 314 def inline_ruby(arg) base, *ruby = *arg.scan(/(?:(?:(?:\\\\)*\\,)|[^,\\]+)+/) if base base = base.gsub(/\\,/, ',') end if ruby ruby = ruby.join(',').gsub(/\\,/, ',') end compile_ruby(base, ruby) end |
#inline_table(id) ⇒ Object
282 283 284 285 286 287 288 289 290 291 |
# File 'lib/review/builder.rb', line 282 def inline_table(id) chapter, id = extract_chapter_id(id) if get_chap(chapter) %Q(#{I18n.t('table')}#{I18n.t('format_number', [get_chap(chapter), chapter.table(id).number])}) else %Q(#{I18n.t('table')}#{I18n.t('format_number_without_chapter', [chapter.table(id).number])}) end rescue KeyError error "unknown table: #{id}" end |
#inline_tcy(arg) ⇒ Object
388 389 390 |
# File 'lib/review/builder.rb', line 388 def inline_tcy(arg) "#{arg}[rotate 90 degree]" end |
#inline_title(id) ⇒ Object
244 245 246 247 248 |
# File 'lib/review/builder.rb', line 244 def inline_title(id) compile_inline @book.chapter_index.title(id) rescue KeyError error "unknown chapter: #{id}" end |
#inline_w(s) ⇒ Object
396 397 398 399 400 401 402 403 404 |
# File 'lib/review/builder.rb', line 396 def inline_w(s) translated = @dictionary[s] if translated escape(translated) else warn "word not bound: #{s}" escape("[missing word: #{s}]") end end |
#inline_wb(s) ⇒ Object
406 407 408 |
# File 'lib/review/builder.rb', line 406 def inline_wb(s) inline_b(unescape(inline_w(s))) end |
#line_num ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/review/builder.rb', line 115 def line_num return 1 unless @first_line_num line_n = @first_line_num @first_line_num = nil line_n end |
#list(lines, id, caption, lang = nil) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/review/builder.rb', line 123 def list(lines, id, , lang = nil) begin list_header id, , lang rescue KeyError error "no such list: #{id}" end list_body id, lines, lang end |
#listnum(lines, id, caption, lang = nil) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/review/builder.rb', line 132 def listnum(lines, id, , lang = nil) begin list_header id, , lang rescue KeyError error "no such list: #{id}" end listnum_body lines, lang end |
#load_words(file) ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/review/builder.rb', line 92 def load_words(file) if File.exist?(file) if file =~ /\.csv\Z/i CSV.foreach(file) do |row| @dictionary[row[0]] = row[1] end end end end |
#parse_metric(type, metric) ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/review/builder.rb', line 452 def parse_metric(type, metric) return '' if metric.blank? params = metric.split(/,\s*/) results = [] params.each do |param| if param =~ /\A.+?::/ next unless param =~ /\A#{type}::/ param.sub!(/\A#{type}::/, '') end param2 = handle_metric(param) results.push(param2) end result_metric(results) end |
#post_paragraph ⇒ Object
29 30 31 |
# File 'lib/review/builder.rb', line 29 def post_paragraph nil end |
#pre_paragraph ⇒ Object
25 26 27 |
# File 'lib/review/builder.rb', line 25 def pre_paragraph nil end |
#print(*s) ⇒ Object
80 81 82 |
# File 'lib/review/builder.rb', line 80 def print(*s) @output.print(*s) end |
#puts(*s) ⇒ Object
84 85 86 |
# File 'lib/review/builder.rb', line 84 def puts(*s) @output.puts(*s) end |
#raw(str) ⇒ Object
410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/review/builder.rb', line 410 def raw(str) if matched = str.match(/\|(.*?)\|(.*)/) builders = matched[1].split(',').map { |i| i.gsub(/\s/, '') } c = target_name if builders.include?(c) print matched[2].gsub('\\n', "\n") end else print str.gsub('\\n', "\n") end end |
#result ⇒ Object Also known as: raw_result
74 75 76 |
# File 'lib/review/builder.rb', line 74 def result @output.string end |
#result_metric(array) ⇒ Object
448 449 450 |
# File 'lib/review/builder.rb', line 448 def result_metric(array) array.join(',') end |
#source(lines, caption, lang = nil) ⇒ Object
141 142 143 144 |
# File 'lib/review/builder.rb', line 141 def source(lines, , lang = nil) source_header source_body lines, lang end |
#system_graph(id, *args) ⇒ Object
520 521 522 523 |
# File 'lib/review/builder.rb', line 520 def system_graph(id, *args) @logger.info args.join(' ') Kernel.system(*args) or @logger.error("failed to run command for id #{id}: #{args.join(' ')}") end |
#table(lines, id = nil, caption = nil) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/review/builder.rb', line 155 def table(lines, id = nil, = 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 if .present? table_header id, end rescue KeyError 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 |
#target_name ⇒ Object
88 89 90 |
# File 'lib/review/builder.rb', line 88 def target_name self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase end |
#text(str) ⇒ Object
339 340 341 |
# File 'lib/review/builder.rb', line 339 def text(str) str end |
#tsize(str) ⇒ Object
578 579 580 581 582 583 584 585 586 587 588 |
# File 'lib/review/builder.rb', line 578 def tsize(str) if matched = str.match(/\A\|(.*?)\|(.*)/) builders = matched[1].split(',').map { |i| i.gsub(/\s/, '') } c = self.class.to_s.gsub('ReVIEW::', '').gsub('Builder', '').downcase if builders.include?(c) @tsize = matched[2] end else @tsize = str end end |
#ul_item_begin(lines) ⇒ Object
571 572 573 |
# File 'lib/review/builder.rb', line 571 def ul_item_begin(lines) ul_item(lines) end |
#ul_item_end ⇒ Object
575 576 |
# File 'lib/review/builder.rb', line 575 def ul_item_end end |
#unescape(str) ⇒ Object
632 633 634 |
# File 'lib/review/builder.rb', line 632 def unescape(str) str end |
#warn(msg) ⇒ Object
432 433 434 |
# File 'lib/review/builder.rb', line 432 def warn(msg) @logger.warn "#{@location}: #{msg}" end |