Class: ReVIEW::MARKDOWNBuilder

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

Direct Known Subclasses

MD2INAOBuilder

Constant Summary

Constants included from HTMLUtils

HTMLUtils::ESC

Constants inherited from Builder

Builder::CAPTION_TITLES

Instance Attribute Summary

Attributes inherited from Builder

#doc_status

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

#detab, #split_paragraph

Methods inherited from Builder

#bibpaper, #bind, #blankline, #compile_inline, #detab, #embed, #emtable, #error, #escape, #extract_chapter_id, #firstlinenum, #get_chap, #graph, #graph_aafigure, #graph_blockdiag, #graph_gnuplot, #graph_graphviz, #graph_plantuml, #handle_metric, #image, #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, #parse_metric, #post_paragraph, #pre_paragraph, #print, #raw, #result, #result_metric, #source, #system_graph, #target_name, #text, #tsize, #unescape, #warn

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#blankObject



31
32
33
34
35
36
# File 'lib/review/markdownbuilder.rb', line 31

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

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



143
144
145
146
147
148
149
# File 'lib/review/markdownbuilder.rb', line 143

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")
  puts '</div>'
end

#cmd(lines) ⇒ Object



232
233
234
235
236
237
238
# File 'lib/review/markdownbuilder.rb', line 232

def cmd(lines)
  puts '```shell-session'
  lines.each do |line|
    puts detab(line)
  end
  puts '```'
end

#comment(lines, comment = nil) ⇒ Object



347
348
349
350
351
352
353
354
355
# File 'lib/review/markdownbuilder.rb', line 347

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



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

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

#compile_kw(word, alt) ⇒ Object



337
338
339
340
341
342
343
344
345
# File 'lib/review/markdownbuilder.rb', line 337

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



329
330
331
332
333
334
335
# File 'lib/review/markdownbuilder.rb', line 329

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



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

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

#dl_beginObject



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

def dl_begin
  puts '<dl>'
end

#dl_endObject



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

def dl_end
  puts '</dl>'
end

#dt(line) ⇒ Object



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

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

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



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/review/markdownbuilder.rb', line 128

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

#extnameObject



14
15
16
# File 'lib/review/markdownbuilder.rb', line 14

def extname
  '.md'
end

#flushright(lines) ⇒ Object



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

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

#footnote(id, str) ⇒ Object



312
313
314
315
# File 'lib/review/markdownbuilder.rb', line 312

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

#headline(level, _label, caption) ⇒ Object



38
39
40
41
42
43
# File 'lib/review/markdownbuilder.rb', line 38

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

#hrObject



151
152
153
# File 'lib/review/markdownbuilder.rb', line 151

def hr
  puts '----'
end

#image_dummy(_id, _caption, lines) ⇒ Object



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

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

#image_extObject



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

def image_ext
  'jpg'
end

#image_image(id, caption, _metric) ⇒ Object



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

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



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

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



170
171
172
# File 'lib/review/markdownbuilder.rb', line 170

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

#inline_br(_str) ⇒ Object



321
322
323
# File 'lib/review/markdownbuilder.rb', line 321

def inline_br(_str)
  "\n"
end

#inline_code(str) ⇒ Object



178
179
180
# File 'lib/review/markdownbuilder.rb', line 178

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

#inline_comment(str) ⇒ Object



366
367
368
369
370
371
372
# File 'lib/review/markdownbuilder.rb', line 366

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

#inline_dtp(str) ⇒ Object



214
215
216
# File 'lib/review/markdownbuilder.rb', line 214

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

#inline_em(str) ⇒ Object



166
167
168
# File 'lib/review/markdownbuilder.rb', line 166

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

#inline_fn(id) ⇒ Object



317
318
319
# File 'lib/review/markdownbuilder.rb', line 317

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

#inline_i(str) ⇒ Object



162
163
164
# File 'lib/review/markdownbuilder.rb', line 162

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

#inline_icon(id) ⇒ Object



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

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

#inline_img(id) ⇒ Object



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

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

#inline_strong(str) ⇒ Object



174
175
176
# File 'lib/review/markdownbuilder.rb', line 174

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

#inline_sub(str) ⇒ Object



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

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

#inline_sup(str) ⇒ Object



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

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

#inline_tt(str) ⇒ Object



190
191
192
# File 'lib/review/markdownbuilder.rb', line 190

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

#inline_u(str) ⇒ Object



194
195
196
# File 'lib/review/markdownbuilder.rb', line 194

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

#list_body(_id, lines, _lang) ⇒ Object



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

def list_body(_id, lines, _lang)
  lines.each do |line|
    puts detab(line)
  end
  puts '```'
end

#list_header(id, caption, lang) ⇒ Object



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

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
  lang ||= ''
  puts "```#{lang}"
end

#nofunc_text(str) ⇒ Object



325
326
327
# File 'lib/review/markdownbuilder.rb', line 325

def nofunc_text(str)
  str
end

#noindentObject



62
63
64
# File 'lib/review/markdownbuilder.rb', line 62

def noindent
  @noindent = true
end

#ol_beginObject



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

def ol_begin
  blank
end

#ol_endObject



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

def ol_end
  blank
end

#ol_item(lines, num) ⇒ Object



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

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

#pagebreakObject



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

def pagebreak
  puts '{pagebreak}'
end

#paragraph(lines) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/review/markdownbuilder.rb', line 51

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

#puts(str) ⇒ Object



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

def puts(str)
  @blank_seen = false
  super
end

#quote(lines) ⇒ Object



45
46
47
48
49
# File 'lib/review/markdownbuilder.rb', line 45

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

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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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 240

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 unless caption.nil?
  rescue KeyError
    error "no such table: #{id}"
  end
  table_begin rows.first.size
  return if rows.empty?
  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
  table_end
end

#table_begin(ncols) ⇒ Object



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

def table_begin(ncols)
end

#table_border(ncols) ⇒ Object



296
297
298
# File 'lib/review/markdownbuilder.rb', line 296

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

#table_endObject



308
309
310
# File 'lib/review/markdownbuilder.rb', line 308

def table_end
  blank
end

#table_header(id, caption) ⇒ Object



278
279
280
281
282
283
284
285
286
287
# File 'lib/review/markdownbuilder.rb', line 278

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

#td(str) ⇒ Object



304
305
306
# File 'lib/review/markdownbuilder.rb', line 304

def td(str)
  str
end

#th(str) ⇒ Object



300
301
302
# File 'lib/review/markdownbuilder.rb', line 300

def th(str)
  str
end

#tr(rows) ⇒ Object



292
293
294
# File 'lib/review/markdownbuilder.rb', line 292

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

#ul_beginObject



83
84
85
86
# File 'lib/review/markdownbuilder.rb', line 83

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

#ul_endObject



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

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

#ul_item_begin(lines) ⇒ Object



88
89
90
# File 'lib/review/markdownbuilder.rb', line 88

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

#ul_item_endObject



92
93
# File 'lib/review/markdownbuilder.rb', line 92

def ul_item_end
end