class ReVIEW::MARKDOWNBuilder

Public Instance Methods

blank() click to toggle source

def puts(str)

@blank_seen = false
"#{str}\n"

end

# File lib/review/markdownbuilder.rb, line 32
def blank
  buf = ""
  unless @blank_seen
    buf = "\n"
  end
  @blank_seen = true
  buf
end
blank_reset() click to toggle source
# File lib/review/markdownbuilder.rb, line 23
def blank_reset
  @blank_seen = false
end
cmd(lines, caption = nil) click to toggle source
# File lib/review/markdownbuilder.rb, line 227
def cmd(lines, caption = nil)
  buf = ""
  buf << "```shell-session" << "\n"
  blank_reset
  lines.each do |line|
    buf << detab(line) << "\n"
  end
  buf << "```" << "\n"
  buf
end
compile_href(url, label) click to toggle source
# File lib/review/markdownbuilder.rb, line 155
def compile_href(url, label)
  label = url if label.blank?
  "[#{label}](#{url})"
end
emlist(lines, caption = nil, lang = nil) click to toggle source
# File lib/review/markdownbuilder.rb, line 132
def emlist(lines, caption = nil, lang = nil)
  buf = ""
  buf << blank
  if caption
    buf << caption << "\n\n"
  end
  lang ||= ""
  buf << "```#{lang}\n"
  blank_reset
  lines.each do |line|
    buf << detab(line) << "\n"
  end
  buf << "```\n"
  buf << blank
  buf
end
extname() click to toggle source
# File lib/review/markdownbuilder.rb, line 13
def extname
  '.md'
end
footnote(id, str) click to toggle source
# File lib/review/markdownbuilder.rb, line 317
def footnote(id, str)
  buf = ""
  buf << "[^#{id}]: #{compile_inline(str)}" << "\n"
  blank_reset
  buf << blank
  buf
end
headline(level, label, caption) click to toggle source
# File lib/review/markdownbuilder.rb, line 41
def headline(level, label, caption)
  buf = ""
  buf << blank
  prefix = "#" * level
  buf << "#{prefix} #{caption}\n"
  blank_reset
  buf << "\n"
  buf
end
hr() click to toggle source
# File lib/review/markdownbuilder.rb, line 149
def hr
  buf << "----\n"
  blank_reset
  buf
end
image_dummy(id, caption, lines) click to toggle source
# File lib/review/markdownbuilder.rb, line 194
def image_dummy(id, caption, lines)
  buf = ""
  buf << lines.join << "\n"
  blank_reset
  buf
end
image_ext() click to toggle source
# File lib/review/markdownbuilder.rb, line 223
def image_ext
  "jpg"
end
image_image(id, caption, metric) click to toggle source
# File lib/review/markdownbuilder.rb, line 185
def image_image(id, caption, metric)
  buf = ""
  buf << blank
  buf << "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(/\A\.\//, "")})" << "\n"
  blank_reset
  buf << blank
  buf
end
indepimage(id, caption="", metric=nil) click to toggle source
# File lib/review/markdownbuilder.rb, line 208
def indepimage(id, caption="", metric=nil)
  buf = ""
  buf << blank
  buf << "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(/\A\.\//, "")})" << "\n"
  blank_reset
  buf << blank
  buf
end
inline_b(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 168
def inline_b(str)
  "**#{str.gsub(/\*/, '\*')}**"
end
inline_br(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 329
def inline_br(str)
  "\n"
end
inline_code(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 176
def inline_code(str)
  "`#{str}`"
end
inline_em(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 164
def inline_em(str)
  "*#{str.gsub(/\*/, '\*')}*"
end
inline_fn(id) click to toggle source
# File lib/review/markdownbuilder.rb, line 325
def inline_fn(id)
  "[^#{id}]"
end
inline_i(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 160
def inline_i(str)
  "*#{str.gsub(/\*/, '\*')}*"
end
inline_img(id) click to toggle source
# File lib/review/markdownbuilder.rb, line 201
def inline_img(id)
  "#{I18n.t("image")}#{@chapter.image(id).number}"
rescue KeyError
  error "unknown image: #{id}"
  "[UnknownImage:#{id}]"
end
inline_strong(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 172
def inline_strong(str)
  "**#{str.gsub(/\*/, '\*')}**"
end
inline_tt(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 180
def inline_tt(str)
  "`#{str}`"
end
list_body(id, lines, lang) click to toggle source
# File lib/review/markdownbuilder.rb, line 76
def list_body(id, lines, lang)
  buf = ""
  lines.each do |line|
    buf << detab(line) << "\n"
  end
  buf << '```' << "\n"
  buf
end
list_header(id, caption, lang) click to toggle source
# File lib/review/markdownbuilder.rb, line 67
def list_header(id, caption, lang)
  lang ||= ""
  if get_chap.nil?
    %Q[リスト#{@chapter.list(id).number} #{compile_inline(caption)}\n\n] + "```#{lang}\n"
  else
    %Q[リスト#{get_chap}.#{@chapter.list(id).number} #{compile_inline(caption)}\n\n] + "```#{lang}\n"
  end
end
nofunc_text(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 333
def nofunc_text(str)
  str
end
ol_begin() click to toggle source
# File lib/review/markdownbuilder.rb, line 113
def ol_begin
  buf = ""
  buf << blank
  buf
end
ol_end() click to toggle source
# File lib/review/markdownbuilder.rb, line 126
def ol_end
  buf = ""
  buf << blank
  buf
end
ol_item(lines, num) click to toggle source
# File lib/review/markdownbuilder.rb, line 119
def ol_item(lines, num)
  buf = ""
  buf << "#{num}. #{lines.join}" << "\n"
  blank_reset
  buf
end
pagebreak() click to toggle source
# File lib/review/markdownbuilder.rb, line 217
def pagebreak
  buf = ""
  buf << "{pagebreak}" << "\n"
  buf
end
paragraph(lines) click to toggle source
# File lib/review/markdownbuilder.rb, line 60
def paragraph(lines)
  buf = lines.join << "\n"
  blank_reset
  buf << "\n"
  buf
end
quote(lines) click to toggle source
# File lib/review/markdownbuilder.rb, line 51
def quote(lines)
  buf = ""
  buf << blank
  buf << lines.map{|line| line.chomp!;line.chomp!;"> #{line}"}.join("\n") << "\n"
  blank_reset
  buf << "\n"
  buf
end
table(lines, id = nil, caption = nil) click to toggle source
# File lib/review/markdownbuilder.rb, line 238
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
  buf << table_begin(rows.first.size)
  return buf if rows.empty?
  if sepidx
    sepidx.times do
      buf << tr(rows.shift.map{|s| th(s) })
    end
    buf << table_border(rows.first.size)
    rows.each do |cols|
      buf << 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
  buf << table_end
  buf
end
table_begin(ncols) click to toggle source
# File lib/review/markdownbuilder.rb, line 290
def table_begin(ncols)
  ""
end
table_border(ncols) click to toggle source
# File lib/review/markdownbuilder.rb, line 298
def table_border(ncols)
  blank_reset
  (0..ncols).map{"|"}.join(":--") + "\n"
end
table_end() click to toggle source
# File lib/review/markdownbuilder.rb, line 311
def table_end
  buf = ""
  buf << blank
  buf
end
table_header(id, caption) click to toggle source
# File lib/review/markdownbuilder.rb, line 278
def table_header(id, caption)
  buf = ""
  if get_chap.nil?
    buf << %Q[#{I18n.t("table")}#{I18n.t("format_number_header_without_chapter", [@chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] << "\n"
  else
    buf << %Q[#{I18n.t("table")}#{I18n.t("format_number_header", [get_chap, @chapter.table(id).number])}#{I18n.t("caption_prefix")}#{compile_inline(caption)}] << "\n"
  end
  blank_reset
  buf << blank
  buf
end
td(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 307
def td(str)
  "#{str}"
end
th(str) click to toggle source
# File lib/review/markdownbuilder.rb, line 303
def th(str)
  "#{str}"
end
tr(rows) click to toggle source
# File lib/review/markdownbuilder.rb, line 294
def tr(rows)
  "|#{rows.join("|")}|\n"
end
ul_begin() click to toggle source
# File lib/review/markdownbuilder.rb, line 85
def ul_begin
  buf = ""
  buf << blank if @ul_indent == 0
  @ul_indent += 1
  buf
end
ul_end() click to toggle source
# File lib/review/markdownbuilder.rb, line 106
def ul_end
  buf = ""
  @ul_indent -= 1
  buf << blank if @ul_indent == 0
  buf
end
ul_item(lines) click to toggle source
# File lib/review/markdownbuilder.rb, line 92
def ul_item(lines)
  blank_reset
  "  " * (@ul_indent - 1) + "* " + "#{lines.join}" + "\n"
end
ul_item_begin(lines) click to toggle source
# File lib/review/markdownbuilder.rb, line 97
def ul_item_begin(lines)
  blank_reset
  "  " * (@ul_indent - 1) + "* " + "#{lines.join}" + "\n"
end
ul_item_end() click to toggle source
# File lib/review/markdownbuilder.rb, line 102
def ul_item_end
  ""
end

Private Instance Methods

builder_init_file() click to toggle source
# File lib/review/markdownbuilder.rb, line 17
def builder_init_file
  @ul_indent = 0
  @chapter.book.image_types = %w( .png .jpg .jpeg .gif .svg )
end