class ReVIEW::Builder

Constants

CAPTION_TITLES

Attributes

doc_status[RW]

Public Class Methods

new(strict = false, *args) click to toggle source
# File lib/review/builder.rb, line 33
def initialize(strict = false, *args)
  @strict = strict
  @output = nil
  @logger = ReVIEW.logger
  @doc_status = {}
  builder_init(*args)
end

Public Instance Methods

bibpaper(lines, id, caption) click to toggle source
# File lib/review/builder.rb, line 293
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) click to toggle source
# File lib/review/builder.rb, line 45
def bind(compiler, chapter, location)
  @compiler = compiler
  @chapter = chapter
  @location = location
  @output = StringIO.new
  @book = @chapter.book if @chapter.present?
  @tabwidth = nil
  @tsize = nil
  @tabwidth = @book.config['tabwidth'] if @book && @book.config && @book.config['tabwidth']
  builder_init_file
end
blankline() click to toggle source

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

# File lib/review/builder.rb, line 200
def blankline
  puts ''
end
captionblock(_type, _lines, _caption, _specialstyle = nil) click to toggle source
# File lib/review/builder.rb, line 418
def captionblock(_type, _lines, _caption, _specialstyle = nil)
  raise NotImplementedError
end
compile_inline(s) click to toggle source
# File lib/review/builder.rb, line 204
def compile_inline(s)
  @compiler.text(s)
end
detab(str, num = nil) click to toggle source

override TextUtils::detab

Calls superclass method ReVIEW::TextUtils#detab
# File lib/review/builder.rb, line 508
def detab(str, num = nil)
  if num
    super(str, num)
  elsif @tabwidth
    super(str, @tabwidth)
  else
    super(str)
  end
end
embed(lines, arg = nil) click to toggle source
# File lib/review/builder.rb, line 354
def embed(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) click to toggle source
# File lib/review/builder.rb, line 184
def emtable(lines, caption = nil)
  table(lines, nil, caption)
end
error(msg) click to toggle source
# File lib/review/builder.rb, line 368
def error(msg)
  if msg =~ /:\d+: error: /
    raise ApplicationError, msg
  else
    raise ApplicationError, "#{@location}: error: #{msg}"
  end
end
extract_chapter_id(chap_ref) click to toggle source
# File lib/review/builder.rb, line 410
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) click to toggle source

for //firstlinenum[num]

# File lib/review/builder.rb, line 89
def firstlinenum(num)
  @first_line_num = num.to_i
end
get_chap(chapter = @chapter) click to toggle source
# File lib/review/builder.rb, line 399
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 = nil) click to toggle source
# File lib/review/builder.rb, line 430
def graph(lines, id, command, caption = nil)
  c = target_name
  dir = File.join(@book.basedir, @book.image_dir, c)
  FileUtils.mkdir_p(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: %Q(echo 'set terminal ) +
    "#{image_ext == 'eps' ? 'postscript eps' : image_ext}\n" +
    %Q( 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) click to toggle source
# File lib/review/builder.rb, line 376
def handle_metric(str)
  str
end
image(lines, id, caption, metric = nil) click to toggle source
# File lib/review/builder.rb, line 124
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_ext() click to toggle source
# File lib/review/builder.rb, line 455
def image_ext
  raise NotImplementedError
end
inline_bou(str) click to toggle source
# File lib/review/builder.rb, line 260
def inline_bou(str)
  text(str)
end
inline_chap(id) click to toggle source
# File lib/review/builder.rb, line 214
def inline_chap(id)
  @book.chapter_index.number(id)
rescue KeyError
  error "unknown chapter: #{id}"
end
inline_chapref(id) click to toggle source
# File lib/review/builder.rb, line 208
def inline_chapref(id)
  compile_inline @book.chapter_index.display_string(id)
rescue KeyError
  error "unknown chapter: #{id}"
end
inline_column(id) click to toggle source
# File lib/review/builder.rb, line 316
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) click to toggle source
# File lib/review/builder.rb, line 330
def inline_column_chap(chapter, id)
  chapter.column(id).caption
end
inline_embed(args) click to toggle source
# File lib/review/builder.rb, line 494
def inline_embed(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_fn(id) click to toggle source
# File lib/review/builder.rb, line 254
def inline_fn(id)
  @chapter.footnote(id).content
rescue KeyError
  error "unknown footnote: #{id}"
end
inline_hd(id) click to toggle source
# File lib/review/builder.rb, line 302
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) click to toggle source
# File lib/review/builder.rb, line 280
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) click to toggle source
# File lib/review/builder.rb, line 232
def inline_img(id)
  "#{I18n.t('image')}#{@chapter.image(id).number}"
rescue KeyError
  error "unknown image: #{id}"
end
inline_imgref(id) click to toggle source
# File lib/review/builder.rb, line 238
def inline_imgref(id)
  img = inline_img(id)

  if @chapter.image(id).caption
    "#{img}#{I18n.t('image_quote', @chapter.image(id).caption)}"
  else
    img
  end
end
inline_include(file_name) click to toggle source
# File lib/review/builder.rb, line 459
def inline_include(file_name)
  compile_inline File.read(file_name, mode: 'rt:BOM|utf-8').chomp
end
inline_kw(arg) click to toggle source
# File lib/review/builder.rb, line 275
def inline_kw(arg)
  word, alt = *arg.split(',', 2)
  compile_kw(word, alt)
end
inline_list(id) click to toggle source
# File lib/review/builder.rb, line 226
def inline_list(id)
  "#{I18n.t('list')}#{@chapter.list(id).number}"
rescue KeyError
  error "unknown list: #{id}"
end
inline_pageref(id) click to toggle source
# File lib/review/builder.rb, line 334
def inline_pageref(id)
  "[link:#{id}]"
end
inline_raw(args) click to toggle source
# File lib/review/builder.rb, line 480
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) click to toggle source
# File lib/review/builder.rb, line 264
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) click to toggle source
# File lib/review/builder.rb, line 248
def inline_table(id)
  "#{I18n.t('table')}#{@chapter.table(id).number}"
rescue KeyError
  error "unknown table: #{id}"
end
inline_tcy(arg) click to toggle source
# File lib/review/builder.rb, line 338
def inline_tcy(arg)
  "#{arg}[rotate 90 degree]"
end
inline_title(id) click to toggle source
# File lib/review/builder.rb, line 220
def inline_title(id)
  compile_inline @book.chapter_index.title(id)
rescue KeyError
  error "unknown chapter: #{id}"
end
line_num() click to toggle source
# File lib/review/builder.rb, line 93
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) click to toggle source
# File lib/review/builder.rb, line 101
def list(lines, id, caption, lang = nil)
  begin
    list_header id, caption, lang
  rescue KeyError
    error "no such list: #{id}"
  end
  list_body id, lines, lang
end
listnum(lines, id, caption, lang = nil) click to toggle source
# File lib/review/builder.rb, line 110
def listnum(lines, id, caption, lang = nil)
  begin
    list_header id, caption, lang
  rescue KeyError
    error "no such list: #{id}"
  end
  listnum_body lines, lang
end
parse_metric(type, metric) click to toggle source
# File lib/review/builder.rb, line 384
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() click to toggle source
# File lib/review/builder.rb, line 27
def post_paragraph
  nil
end
pre_paragraph() click to toggle source
# File lib/review/builder.rb, line 23
def pre_paragraph
  nil
end
raw(str) click to toggle source
# File lib/review/builder.rb, line 342
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
raw_result()
Alias for: result
result() click to toggle source
# File lib/review/builder.rb, line 62
def result
  @output.string
end
Also aliased as: raw_result
result_metric(array) click to toggle source
# File lib/review/builder.rb, line 380
def result_metric(array)
  array.join(',')
end
source(lines, caption, lang = nil) click to toggle source
# File lib/review/builder.rb, line 119
def source(lines, caption, lang = nil)
  source_header caption
  source_body lines, lang
end
table(lines, id = nil, caption = nil) click to toggle source
# File lib/review/builder.rb, line 133
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 if caption.present?
  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() click to toggle source
# File lib/review/builder.rb, line 76
def target_name
  self.class.to_s.gsub(/ReVIEW::/, '').gsub(/Builder/, '').downcase
end
text(str) click to toggle source
# File lib/review/builder.rb, line 289
def text(str)
  str
end
tsize(str) click to toggle source
# File lib/review/builder.rb, line 470
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
    @tsize = matched[2] if builders.include?(c)
  else
    @tsize = str
  end
end
ul_item_begin(lines) click to toggle source
# File lib/review/builder.rb, line 463
def ul_item_begin(lines)
  ul_item(lines)
end
ul_item_end() click to toggle source
# File lib/review/builder.rb, line 467
def ul_item_end
end
warn(msg) click to toggle source
# File lib/review/builder.rb, line 364
def warn(msg)
  @logger.warn "#{@location}: #{msg}"
end

Private Instance Methods

adjust_n_cols(rows) click to toggle source
# File lib/review/builder.rb, line 170
def adjust_n_cols(rows)
  rows.each do |cols|
    while cols.last and cols.last.strip.empty?
      cols.pop
    end
  end
  n_maxcols = rows.map(&:size).max
  rows.each do |cols|
    cols.concat [''] * (n_maxcols - cols.size)
  end
  rows
end
builder_init(*args) click to toggle source
# File lib/review/builder.rb, line 41
def builder_init(*args)
end
builder_init_file() click to toggle source
# File lib/review/builder.rb, line 57
def builder_init_file
  @sec_counter = SecCounter.new(5, @chapter)
end
headline_prefix(level) click to toggle source
# File lib/review/builder.rb, line 80
def headline_prefix(level)
  @sec_counter.inc(level)
  anchor = @sec_counter.anchor(level)
  prefix = @sec_counter.prefix(level, @book.config['secnolevel'])
  [prefix, anchor]
end
print(*s) click to toggle source
puts(*s) click to toggle source
# File lib/review/builder.rb, line 72
def puts(*s)
  @output.puts(*s)
end