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, #over_secnolevel?, #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



150
151
152
153
154
155
156
# File 'lib/review/markdownbuilder.rb', line 150

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



239
240
241
242
243
244
245
# File 'lib/review/markdownbuilder.rb', line 239

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

#comment(lines, comment = nil) ⇒ Object



354
355
356
357
358
359
360
361
362
# File 'lib/review/markdownbuilder.rb', line 354

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



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

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

#compile_kw(word, alt) ⇒ Object



344
345
346
347
348
349
350
351
352
# File 'lib/review/markdownbuilder.rb', line 344

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



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

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



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

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

#dl_beginObject



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

def dl_begin
  puts '<dl>'
end

#dl_endObject



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

def dl_end
  puts '</dl>'
end

#dt(line) ⇒ Object



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

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

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/review/markdownbuilder.rb', line 135

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



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

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

#footnote(id, str) ⇒ Object



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

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



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

def hr
  puts '----'
end

#image_dummy(_id, _caption, lines) ⇒ Object



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

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

#image_extObject



235
236
237
# File 'lib/review/markdownbuilder.rb', line 235

def image_ext
  'jpg'
end

#image_image(id, caption, _metric) ⇒ Object



205
206
207
208
209
# File 'lib/review/markdownbuilder.rb', line 205

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



225
226
227
228
229
# File 'lib/review/markdownbuilder.rb', line 225

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



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

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

#inline_br(_str) ⇒ Object



328
329
330
# File 'lib/review/markdownbuilder.rb', line 328

def inline_br(_str)
  "\n"
end

#inline_code(str) ⇒ Object



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

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

#inline_comment(str) ⇒ Object



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

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

#inline_dtp(str) ⇒ Object



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

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

#inline_em(str) ⇒ Object



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

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

#inline_fn(id) ⇒ Object



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

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

#inline_i(str) ⇒ Object



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

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

#inline_icon(id) ⇒ Object



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

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



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

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

#inline_strong(str) ⇒ Object



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

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

#inline_sub(str) ⇒ Object



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

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

#inline_sup(str) ⇒ Object



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

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

#inline_tt(str) ⇒ Object



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

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

#inline_u(str) ⇒ Object



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

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

#listnum_body(lines, _lang) ⇒ Object



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

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

#nofunc_text(str) ⇒ Object



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

def nofunc_text(str)
  str
end

#noindentObject



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

def noindent
  @noindent = true
end

#ol_beginObject



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

def ol_begin
  blank
end

#ol_endObject



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

def ol_end
  blank
end

#ol_item(lines, num) ⇒ Object



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

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

#pagebreakObject



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

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



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
277
278
279
280
281
282
283
# File 'lib/review/markdownbuilder.rb', line 247

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)
  error 'no rows in the table' if rows.empty?

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



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

def table_begin(ncols)
end

#table_border(ncols) ⇒ Object



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

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

#table_endObject



315
316
317
# File 'lib/review/markdownbuilder.rb', line 315

def table_end
  blank
end

#table_header(id, caption) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'lib/review/markdownbuilder.rb', line 285

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



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

def td(str)
  str
end

#th(str) ⇒ Object



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

def th(str)
  str
end

#tr(rows) ⇒ Object



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

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

#ul_beginObject



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

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

#ul_endObject



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

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

#ul_item_begin(lines) ⇒ Object



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

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

#ul_item_endObject



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

def ul_item_end
end