Class: ReVIEW::MARKDOWNBuilder

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

Constant Summary

Constants inherited from Builder

Builder::CAPTION_TITLES

Instance Attribute Summary

Attributes inherited from Builder

#ast, #output

Instance Method Summary collapse

Methods included from TextUtils

#detab

Methods inherited from Builder

#bibpaper, #bind, #captionblock, #detab, #error, #extract_chapter_id, #get_chap, #graph, #handle_metric, #image, #include, #initialize, #inline_bou, #inline_chap, #inline_chapref, #inline_column, #inline_href, #inline_include, #inline_kw, #inline_list, #inline_raw, #inline_ruby, #inline_table, #inline_title, #list, #listnum, #node_inline_hd, #node_inline_img, #node_inline_imgref, #parse_metric, #post_paragraph, #pre_paragraph, #print, #puts, #raw, #result, #result_metric, #source, #target_name, #text, #warn

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#blankObject

def puts(str)

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


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

def blank
  buf = ""
  unless @blank_seen
    buf = "\n"
  end
  @blank_seen = true
  buf
end

#blank_resetObject



24
25
26
# File 'lib/review/markdownbuilder.rb', line 24

def blank_reset
  @blank_seen = false
end

#cmd(lines, caption = nil) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/review/markdownbuilder.rb', line 228

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) ⇒ Object



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

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

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



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

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

#extnameObject



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

def extname
  '.md'
end

#footnote(id, str) ⇒ Object



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

def footnote(id, str)
  buf = ""
  buf << "[^#{id}]: #{compile_inline(str)}" << "\n"
  blank_reset
  buf << blank
  buf
end

#headline(level, label, caption) ⇒ Object



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

def headline(level, label, caption)
  buf = ""
  buf << blank
  prefix = "#" * level
  buf << "#{prefix} #{caption}\n"
  blank_reset
  buf << "\n"
  buf
end

#hrObject



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

def hr
  buf << "----\n"
  blank_reset
  buf
end

#image_dummy(id, caption, lines) ⇒ Object



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

def image_dummy(id, caption, lines)
  buf = ""
  buf << lines.join << "\n"
  blank_reset
  buf
end

#image_extObject



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

def image_ext
  "jpg"
end

#image_image(id, caption, metric) ⇒ Object



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

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) ⇒ Object



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

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) ⇒ Object



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

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

#inline_br(str) ⇒ Object



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

def inline_br(str)
  "\n"
end

#inline_code(str) ⇒ Object



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

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

#inline_em(str) ⇒ Object



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

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

#inline_fn(id) ⇒ Object



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

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

#inline_i(str) ⇒ Object



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

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

#inline_img(id) ⇒ Object



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

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

#inline_strong(str) ⇒ Object



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

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

#inline_tt(str) ⇒ Object



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

def inline_tt(str)
  "`#{str}`"
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)
  buf = ""
  lines.each do |line|
    buf << detab(line) << "\n"
  end
  buf << '```' << "\n"
  buf
end

#list_header(id, caption, lang) ⇒ Object



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

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) ⇒ Object



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

def nofunc_text(str)
  str
end

#ol_beginObject



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

def ol_begin
  buf = ""
  buf << blank
  buf
end

#ol_endObject



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

def ol_end
  buf = ""
  buf << blank
  buf
end

#ol_item(lines, num) ⇒ Object



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

def ol_item(lines, num)
  buf = ""
  buf << "#{num}. #{lines.join}" << "\n"
  blank_reset
  buf
end

#pagebreakObject



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

def pagebreak
  buf = ""
  buf << "{pagebreak}" << "\n"
  buf
end

#paragraph(lines) ⇒ Object



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

def paragraph(lines)
  buf = lines.join << "\n"
  blank_reset
  buf << "\n"
  buf
end

#quote(lines) ⇒ Object



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

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) ⇒ Object



239
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
277
# File 'lib/review/markdownbuilder.rb', line 239

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) ⇒ Object



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

def table_begin(ncols)
  ""
end

#table_border(ncols) ⇒ Object



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

def table_border(ncols)
  blank_reset
  (0..ncols).map{"|"}.join(":--") + "\n"
end

#table_endObject



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

def table_end
  buf = ""
  buf << blank
  buf
end

#table_header(id, caption) ⇒ Object



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

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) ⇒ Object



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

def td(str)
  "#{str}"
end

#th(str) ⇒ Object



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

def th(str)
  "#{str}"
end

#tr(rows) ⇒ Object



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

def tr(rows)
  "|#{rows.join("|")}|\n"
end

#ul_beginObject



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

def ul_begin
  buf = ""
  buf << blank if @ul_indent == 0
  @ul_indent += 1
  buf
end

#ul_endObject



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

def ul_end
  buf = ""
  @ul_indent -= 1
  buf << blank if @ul_indent == 0
  buf
end

#ul_item(lines) ⇒ Object



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

def ul_item(lines)
  blank_reset
  "  " * (@ul_indent - 1) + "* " + "#{lines.join}" + "\n"
end

#ul_item_begin(lines) ⇒ Object



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

def ul_item_begin(lines)
  blank_reset
  "  " * (@ul_indent - 1) + "* " + "#{lines.join}" + "\n"
end

#ul_item_endObject



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

def ul_item_end
  ""
end