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

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

Methods inherited from Builder

#bibpaper, #bind, #blankline, #caption_top?, #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, #highlight?, #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, #parse_table_rows, #post_paragraph, #pre_paragraph, #print, #raw, #result, #result_metric, #source, #system_graph, #table, #table_row_separator_regexp, #target_name, #text, #tsize, #warn

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#blankObject



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

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

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



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

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, caption = nil) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/review/markdownbuilder.rb', line 243

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



341
342
343
344
345
346
347
348
349
# File 'lib/review/markdownbuilder.rb', line 341

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



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

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

#compile_kw(word, alt) ⇒ Object



331
332
333
334
335
336
337
338
339
# File 'lib/review/markdownbuilder.rb', line 331

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



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

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



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

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

#dl_beginObject



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

def dl_begin
  puts '<dl>'
end

#dl_endObject



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

def dl_end
  puts '</dl>'
end

#dt(line) ⇒ Object



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

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

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



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

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

#extnameObject



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

def extname
  '.md'
end

#flushright(lines) ⇒ Object



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

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

#footnote(id, str) ⇒ Object



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

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

#headline(level, _label, caption) ⇒ Object



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

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

#hrObject



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

def hr
  puts '----'
end

#image_dummy(_id, _caption, lines) ⇒ Object



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

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

#image_extObject



239
240
241
# File 'lib/review/markdownbuilder.rb', line 239

def image_ext
  'jpg'
end

#image_image(id, caption, _metric) ⇒ Object



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

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



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

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



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

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

#inline_br(_str) ⇒ Object



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

def inline_br(_str)
  "\n"
end

#inline_code(str) ⇒ Object



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

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

#inline_comment(str) ⇒ Object



360
361
362
363
364
365
366
# File 'lib/review/markdownbuilder.rb', line 360

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

#inline_dtp(str) ⇒ Object



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

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

#inline_em(str) ⇒ Object



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

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

#inline_fn(id) ⇒ Object



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

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

#inline_i(str) ⇒ Object



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

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

#inline_icon(id) ⇒ Object



351
352
353
354
355
356
357
358
# File 'lib/review/markdownbuilder.rb', line 351

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



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

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

#inline_strong(str) ⇒ Object



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

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

#inline_sub(str) ⇒ Object



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

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

#inline_sup(str) ⇒ Object



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

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

#inline_tt(str) ⇒ Object



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

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

#inline_u(str) ⇒ Object



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

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

#list_body(_id, lines, lang) ⇒ Object



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

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



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

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



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

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



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

def nofunc_text(str)
  str
end

#noindentObject



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

def noindent
  @noindent = true
end

#ol_beginObject



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

def ol_begin
  blank
end

#ol_endObject



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

def ol_end
  blank
end

#ol_item(lines, num) ⇒ Object



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

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

#pagebreakObject



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

def pagebreak
  puts '{pagebreak}'
end

#paragraph(lines) ⇒ Object



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

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



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

def puts(str)
  @blank_seen = false
  super
end

#quote(lines) ⇒ Object



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

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

#table_begin(ncols) ⇒ Object



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

def table_begin(ncols)
end

#table_border(ncols) ⇒ Object



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

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

#table_endObject



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

def table_end
  blank
end

#table_header(id, caption) ⇒ Object



272
273
274
275
276
277
278
279
280
281
# File 'lib/review/markdownbuilder.rb', line 272

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



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/review/markdownbuilder.rb', line 255

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



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

def td(str)
  str
end

#th(str) ⇒ Object



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

def th(str)
  str
end

#tr(rows) ⇒ Object



286
287
288
# File 'lib/review/markdownbuilder.rb', line 286

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

#ul_beginObject



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

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

#ul_endObject



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

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

#ul_item_begin(lines) ⇒ Object



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

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

#ul_item_endObject



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

def ul_item_end
end