Class: ReVIEW::MARKDOWNBuilder

Inherits:
Builder show all
Includes:
HTMLUtils, TextUtils
Defined in:
lib/review/markdownbuilder.rb

Direct Known Subclasses

MD2INAOBuilder

Constant Summary

Constants inherited from Builder

Builder::CAPTION_TITLES

Instance Attribute Summary

Attributes inherited from Builder

#doc_status, #location

Attributes included from Loggable

#logger

Instance Method Summary collapse

Methods included from HTMLUtils

#escape, #escape_comment, #highlight, #highlight?, #highlight_pygments, #highlight_rouge, #normalize_id, #strip_html, #unescape

Methods included from TextUtils

#add_space?, #detab, #join_lines_to_paragraph, #split_paragraph

Methods inherited from Builder

#beginchild, #bibpaper, #bind, #blankline, #caption_top?, #check_nest, #check_nested_minicolumn, #check_printendnotes, #compile_inline, #detab, #embed, #emtable, #endchild, #endnote, #endnote_begin, #endnote_end, #escape, #extract_chapter_id, #firstlinenum, #get_chap, #graph, #graph_aafigure, #graph_blockdiag, #graph_gnuplot, #graph_graphviz, #graph_plantuml, #handle_metric, #highlight?, #image, #in_minicolumn?, #initialize, #inline_balloon, #inline_bou, #inline_chap, #inline_chapref, #inline_column, #inline_column_chap, #inline_embed, #inline_eq, #inline_hd, #inline_href, #inline_imgref, #inline_include, #inline_kw, #inline_list, #inline_pageref, #inline_raw, #inline_ruby, #inline_table, #inline_tcy, #inline_title, #inline_w, #inline_wb, #line_num, #list, #listnum, #load_words, #minicolumn_block_name?, #over_secnolevel?, #parse_metric, #parse_table_rows, #post_paragraph, #pre_paragraph, #previous_list_type, #print, #printendnotes, #raw, #result, #result_metric, #solve_nest, #source, #system_graph, #table, #table_row_separator_regexp, #target_name, #text, #tsize

Methods included from Loggable

#app_error, #debug, #error, #error!, #warn

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#blankObject



34
35
36
37
38
39
# File 'lib/review/markdownbuilder.rb', line 34

def blank
  unless @blank_seen
    @output.puts
  end
  @blank_seen = true
end

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



155
156
157
158
159
160
161
# File 'lib/review/markdownbuilder.rb', line 155

def captionblock(type, lines, caption, _specialstyle = nil)
  puts %Q(<div class="#{type}">)
  puts %Q(<p class="caption">#{compile_inline(caption)}</p>) if caption.present?
  blocked_lines = split_paragraph(lines)
  puts blocked_lines.join("\n\n")
  puts '</div>'
end

#cmd(lines, caption = nil) ⇒ Object



292
293
294
295
296
297
298
299
300
301
302
# File 'lib/review/markdownbuilder.rb', line 292

def cmd(lines, caption = nil)
  if caption.present?
    puts caption
    blank
  end
  puts '```shell-session'
  lines.each do |line|
    puts detab(line)
  end
  puts '```'
end

#comment(lines, comment = nil) ⇒ Object



398
399
400
401
402
403
404
405
406
407
# File 'lib/review/markdownbuilder.rb', line 398

def comment(lines, comment = nil)
  return unless @book.config['draft']

  lines ||= []
  unless comment.blank?
    lines.unshift(comment)
  end
  str = lines.join('<br />')
  puts %Q(<div class="red">#{escape(str)}</div>)
end

#compile_href(url, label) ⇒ Object



185
186
187
188
189
190
# File 'lib/review/markdownbuilder.rb', line 185

def compile_href(url, label)
  if label.blank?
    label = url
  end
  "[#{label}](#{url})"
end

#compile_kw(word, alt) ⇒ Object



388
389
390
391
392
393
394
395
396
# File 'lib/review/markdownbuilder.rb', line 388

def compile_kw(word, alt)
  %Q(<b class="kw">) +
    if alt
      escape_html(word + " (#{alt.strip})")
    else
      escape_html(word)
    end +
    "</b><!-- IDX:#{escape_comment(escape_html(word))} -->"
end

#compile_ruby(base, ruby) ⇒ Object



380
381
382
383
384
385
386
# File 'lib/review/markdownbuilder.rb', line 380

def compile_ruby(base, ruby)
  if @book.htmlversion == 5
    %Q(<ruby>#{escape(base)}<rp>#{I18n.t('ruby_prefix')}</rp><rt>#{escape(ruby)}</rt><rp>#{I18n.t('ruby_postfix')}</rp></ruby>)
  else
    %Q(<ruby><rb>#{escape(base)}</rb><rp>#{I18n.t('ruby_prefix')}</rp><rt>#{ruby}</rt><rp>#{I18n.t('ruby_postfix')}</rp></ruby>)
  end
end

#dd(lines) ⇒ Object



132
133
134
# File 'lib/review/markdownbuilder.rb', line 132

def dd(lines)
  puts "<dd>#{join_lines_to_paragraph(lines)}</dd>"
end

#dl_beginObject



124
125
126
# File 'lib/review/markdownbuilder.rb', line 124

def dl_begin
  puts '<dl>'
end

#dl_endObject



136
137
138
# File 'lib/review/markdownbuilder.rb', line 136

def dl_end
  puts '</dl>'
end

#dt(line) ⇒ Object



128
129
130
# File 'lib/review/markdownbuilder.rb', line 128

def dt(line)
  puts "<dt>#{line}</dt>"
end

#emlist(lines, caption = nil, lang = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/review/markdownbuilder.rb', line 140

def emlist(lines, caption = nil, lang = nil)
  blank
  if caption.present?
    puts caption
    blank
  end
  lang ||= ''
  puts "```#{lang}"
  lines.each do |line|
    puts detab(line)
  end
  puts '```'
  blank
end

#endnote_item(id) ⇒ Object



368
369
370
# File 'lib/review/markdownbuilder.rb', line 368

def endnote_item(id)
  puts "#{I18n.t('html_endnote_textmark', @chapter.endnote(id).number)}#{compile_inline(@chapter.endnote(id).content)}"
end

#extnameObject



16
17
18
# File 'lib/review/markdownbuilder.rb', line 16

def extname
  '.md'
end

#flushright(lines) ⇒ Object



426
427
428
429
430
# File 'lib/review/markdownbuilder.rb', line 426

def flushright(lines)
  puts %Q(<div class="flushright">)
  puts split_paragraph(lines).join("\n")
  puts %Q(</div>)
end

#footnote(id, str) ⇒ Object



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

def footnote(id, str)
  puts "[^#{id}]: #{compile_inline(str)}"
  blank
end

#headline(level, _label, caption) ⇒ Object



41
42
43
44
45
46
# File 'lib/review/markdownbuilder.rb', line 41

def headline(level, _label, caption)
  blank
  prefix = '#' * level
  puts "#{prefix} #{caption}"
  blank
end

#hrObject



181
182
183
# File 'lib/review/markdownbuilder.rb', line 181

def hr
  puts '----'
end

#image_dummy(_id, _caption, lines) ⇒ Object



242
243
244
# File 'lib/review/markdownbuilder.rb', line 242

def image_dummy(_id, _caption, lines)
  puts lines.join
end

#image_extObject



288
289
290
# File 'lib/review/markdownbuilder.rb', line 288

def image_ext
  'jpg'
end

#image_image(id, caption, _metric) ⇒ Object



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

def image_image(id, caption, _metric)
  blank
  puts "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(%r{\A\./}, '')})"
  blank
end

#indepimage(_lines, id, caption = '', _metric = nil) ⇒ Object



278
279
280
281
282
# File 'lib/review/markdownbuilder.rb', line 278

def indepimage(_lines, id, caption = '', _metric = nil)
  blank
  puts "![#{compile_inline(caption)}](#{@chapter.image(id).path.sub(%r{\A\./}, '')})"
  blank
end

#inline_b(str) ⇒ Object



200
201
202
# File 'lib/review/markdownbuilder.rb', line 200

def inline_b(str)
  "**#{str.gsub(/\*/, '\*')}**"
end

#inline_br(_str) ⇒ Object



372
373
374
# File 'lib/review/markdownbuilder.rb', line 372

def inline_br(_str)
  "\n"
end

#inline_code(str) ⇒ Object



208
209
210
# File 'lib/review/markdownbuilder.rb', line 208

def inline_code(str)
  "`#{str}`"
end

#inline_comment(str) ⇒ Object



418
419
420
421
422
423
424
# File 'lib/review/markdownbuilder.rb', line 418

def inline_comment(str)
  if @book.config['draft']
    %Q(<span class="red">#{escape(str)}</span>)
  else
    ''
  end
end

#inline_del(str) ⇒ Object



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

def inline_del(str)
  "~~#{str}~~"
end

#inline_dtp(str) ⇒ Object



252
253
254
# File 'lib/review/markdownbuilder.rb', line 252

def inline_dtp(str)
  "<!-- DTP:#{str} -->"
end

#inline_em(str) ⇒ Object



196
197
198
# File 'lib/review/markdownbuilder.rb', line 196

def inline_em(str)
  "*#{str.gsub(/\*/, '\*')}*"
end

#inline_endnote(id) ⇒ Object



364
365
366
# File 'lib/review/markdownbuilder.rb', line 364

def inline_endnote(id)
  "<sup>#{I18n.t('html_endnote_refmark', @chapter.endnote(id).number)}</sup>"
end

#inline_fn(id) ⇒ Object



360
361
362
# File 'lib/review/markdownbuilder.rb', line 360

def inline_fn(id)
  "[^#{id}]"
end

#inline_hd_chap(chap, id) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/review/markdownbuilder.rb', line 256

def inline_hd_chap(chap, id)
  n = chap.headline_index.number(id)
  if n.present? && chap.number && over_secnolevel?(n)
    str = I18n.t('hd_quote', [n, compile_inline(chap.headline(id).caption)])
  else
    str = I18n.t('hd_quote_without_number', compile_inline(chap.headline(id).caption))
  end
  if @book.config['chapterlink']
    if @chapter == chap
      anchor = 'h' + n.tr('.', '-')
      %Q(<a href="##{anchor}">#{str}</a>)
    else
      warn 'MARKDOWNBuilder does not support links to other chapters', location: location
      str
    end
  else
    str
  end
rescue KeyError
  app_error "unknown headline: #{id}"
end

#inline_i(str) ⇒ Object



192
193
194
# File 'lib/review/markdownbuilder.rb', line 192

def inline_i(str)
  "*#{str.gsub(/\*/, '\*')}*"
end

#inline_icon(id) ⇒ Object



409
410
411
412
413
414
415
416
# File 'lib/review/markdownbuilder.rb', line 409

def inline_icon(id)
  begin
    "![](#{@chapter.image(id).path.sub(%r{\A\./}, '')})"
  rescue
    warn "image not bound: #{id}", location: location
    %Q(<pre>missing image: #{id}</pre>)
  end
end

#inline_img(id) ⇒ Object



246
247
248
249
250
# File 'lib/review/markdownbuilder.rb', line 246

def inline_img(id)
  "#{I18n.t('image')}#{@chapter.image(id).number}"
rescue KeyError
  app_error "unknown image: #{id}"
end

#inline_ins(str) ⇒ Object



228
229
230
# File 'lib/review/markdownbuilder.rb', line 228

def inline_ins(str)
  "<ins>#{str}</ins>"
end

#inline_strong(str) ⇒ Object



204
205
206
# File 'lib/review/markdownbuilder.rb', line 204

def inline_strong(str)
  "**#{str.gsub(/\*/, '\*')}**"
end

#inline_sub(str) ⇒ Object



212
213
214
# File 'lib/review/markdownbuilder.rb', line 212

def inline_sub(str)
  "<sub>#{str}</sub>"
end

#inline_sup(str) ⇒ Object



216
217
218
# File 'lib/review/markdownbuilder.rb', line 216

def inline_sup(str)
  "<sup>#{str}</sup>"
end

#inline_tt(str) ⇒ Object



220
221
222
# File 'lib/review/markdownbuilder.rb', line 220

def inline_tt(str)
  "`#{str}`"
end

#inline_u(str) ⇒ Object



224
225
226
# File 'lib/review/markdownbuilder.rb', line 224

def inline_u(str)
  "<u>#{str}</u>"
end

#list_body(_id, lines, lang) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/review/markdownbuilder.rb', line 77

def list_body(_id, lines, lang)
  lang ||= ''
  puts "```#{lang}"
  lines.each do |line|
    puts detab(line)
  end
  puts '```'
end

#list_header(id, caption, _lang) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/review/markdownbuilder.rb', line 69

def list_header(id, caption, _lang)
  if get_chap.nil?
    print %Q(リスト#{@chapter.list(id).number} #{compile_inline(caption)}\n\n)
  else
    print %Q(リスト#{get_chap}.#{@chapter.list(id).number} #{compile_inline(caption)}\n\n)
  end
end

#listnum_body(lines, lang) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/review/markdownbuilder.rb', line 86

def listnum_body(lines, lang)
  lang ||= ''
  puts "```#{lang}"
  lines.each_with_index do |line, i|
    puts((i + 1).to_s.rjust(2) + ": #{detab(line)}")
  end
  puts '```'
end

#nofunc_text(str) ⇒ Object



376
377
378
# File 'lib/review/markdownbuilder.rb', line 376

def nofunc_text(str)
  str
end

#noindentObject



65
66
67
# File 'lib/review/markdownbuilder.rb', line 65

def noindent
  @noindent = true
end

#ol_beginObject



112
113
114
# File 'lib/review/markdownbuilder.rb', line 112

def ol_begin
  blank
end

#ol_endObject



120
121
122
# File 'lib/review/markdownbuilder.rb', line 120

def ol_end
  blank
end

#ol_item(lines, num) ⇒ Object



116
117
118
# File 'lib/review/markdownbuilder.rb', line 116

def ol_item(lines, num)
  puts "#{num}. #{join_lines_to_paragraph(lines)}"
end

#pagebreakObject



284
285
286
# File 'lib/review/markdownbuilder.rb', line 284

def pagebreak
  puts '{pagebreak}'
end

#paragraph(lines) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/review/markdownbuilder.rb', line 54

def paragraph(lines)
  if @noindent
    puts %Q(<p class="noindent">#{join_lines_to_paragraph(lines)}</p>)
    puts "\n"
    @noindent = nil
  else
    puts join_lines_to_paragraph(lines)
    puts "\n"
  end
end

#puts(str) ⇒ Object



29
30
31
32
# File 'lib/review/markdownbuilder.rb', line 29

def puts(str)
  @blank_seen = false
  super
end

#quote(lines) ⇒ Object



48
49
50
51
52
# File 'lib/review/markdownbuilder.rb', line 48

def quote(lines)
  blank
  puts split_paragraph(lines).map { |line| "> #{line}" }.join("\n> \n")
  blank
end

#table_begin(ncols) ⇒ Object



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

def table_begin(ncols)
end

#table_border(ncols) ⇒ Object



339
340
341
# File 'lib/review/markdownbuilder.rb', line 339

def table_border(ncols)
  puts((0..ncols).map { '|' }.join(':--'))
end

#table_endObject



351
352
353
# File 'lib/review/markdownbuilder.rb', line 351

def table_end
  blank
end

#table_header(id, caption) ⇒ Object



321
322
323
324
325
326
327
328
329
330
# File 'lib/review/markdownbuilder.rb', line 321

def table_header(id, caption)
  if id.nil?
    puts compile_inline(caption)
  elsif get_chap
    puts %Q(#{I18n.t('table')}#{I18n.t('format_number_header', [get_chap, @chapter.table(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)})
  else
    puts %Q(#{I18n.t('table')}#{I18n.t('format_number_header_without_chapter', [@chapter.table(id).number])}#{I18n.t('caption_prefix')}#{compile_inline(caption)})
  end
  blank
end

#table_rows(sepidx, rows) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/review/markdownbuilder.rb', line 304

def table_rows(sepidx, rows)
  if sepidx
    sepidx.times do
      tr(rows.shift.map { |s| th(s) })
    end
    table_border(rows.first.size)
    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
end

#td(str) ⇒ Object



347
348
349
# File 'lib/review/markdownbuilder.rb', line 347

def td(str)
  str
end

#th(str) ⇒ Object



343
344
345
# File 'lib/review/markdownbuilder.rb', line 343

def th(str)
  str
end

#tr(rows) ⇒ Object



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

def tr(rows)
  puts "|#{rows.join('|')}|"
end

#ul_beginObject



95
96
97
98
# File 'lib/review/markdownbuilder.rb', line 95

def ul_begin
  blank if @ul_indent == 0
  @ul_indent += 1
end

#ul_endObject



107
108
109
110
# File 'lib/review/markdownbuilder.rb', line 107

def ul_end
  @ul_indent -= 1
  blank if @ul_indent == 0
end

#ul_item_begin(lines) ⇒ Object



100
101
102
# File 'lib/review/markdownbuilder.rb', line 100

def ul_item_begin(lines)
  puts ('  ' * (@ul_indent - 1)) + '* ' + join_lines_to_paragraph(lines)
end

#ul_item_endObject



104
105
# File 'lib/review/markdownbuilder.rb', line 104

def ul_item_end
end