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 Method Summary collapse

Methods included from TextUtils

#detab, #split_paragraph

Methods inherited from Builder

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

Constructor Details

This class inherits a constructor from ReVIEW::Builder

Instance Method Details

#blankObject



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

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

#cmd(lines) ⇒ Object



182
183
184
185
186
187
188
# File 'lib/review/markdownbuilder.rb', line 182

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

#compile_href(url, label) ⇒ Object



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

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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/review/markdownbuilder.rb', line 102

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

#footnote(id, str) ⇒ Object



260
261
262
263
# File 'lib/review/markdownbuilder.rb', line 260

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

#headline(level, label, caption) ⇒ Object



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

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

#hrObject



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

def hr
  puts "----"
end

#image_dummy(id, caption, lines) ⇒ Object



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

def image_dummy(id, caption, lines)
  puts lines.join
end

#image_extObject



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

def image_ext
  "jpg"
end

#image_image(id, caption, metric) ⇒ Object



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

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

#indepimage(id, caption = "", metric = nil) ⇒ Object



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

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

#inline_b(str) ⇒ Object



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

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

#inline_br(str) ⇒ Object



269
270
271
# File 'lib/review/markdownbuilder.rb', line 269

def inline_br(str)
  "\n"
end

#inline_code(str) ⇒ Object



142
143
144
# File 'lib/review/markdownbuilder.rb', line 142

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

#inline_em(str) ⇒ Object



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

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

#inline_fn(id) ⇒ Object



265
266
267
# File 'lib/review/markdownbuilder.rb', line 265

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

#inline_i(str) ⇒ Object



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

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

#inline_img(id) ⇒ Object



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

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

#inline_strong(str) ⇒ Object



138
139
140
# File 'lib/review/markdownbuilder.rb', line 138

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

#inline_tt(str) ⇒ Object



146
147
148
# File 'lib/review/markdownbuilder.rb', line 146

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

#list_body(id, lines, lang) ⇒ Object



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

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

#list_header(id, caption, lang) ⇒ Object



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

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



273
274
275
# File 'lib/review/markdownbuilder.rb', line 273

def nofunc_text(str)
  str
end

#ol_beginObject



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

def ol_begin
  blank
end

#ol_endObject



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

def ol_end
  blank
end

#ol_item(lines, num) ⇒ Object



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

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

#pagebreakObject



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

def pagebreak
  puts "{pagebreak}"
end

#paragraph(lines) ⇒ Object



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

def paragraph(lines)
  puts lines.join
  puts "\n"
end

#puts(str) ⇒ Object



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

def puts(str)
  @blank_seen = false
  super
end

#quote(lines) ⇒ Object



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

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

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/review/markdownbuilder.rb', line 190

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



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

def table_begin(ncols)
end

#table_border(ncols) ⇒ Object



244
245
246
# File 'lib/review/markdownbuilder.rb', line 244

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

#table_endObject



256
257
258
# File 'lib/review/markdownbuilder.rb', line 256

def table_end
  blank
end

#table_header(id, caption) ⇒ Object



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

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

#td(str) ⇒ Object



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

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

#th(str) ⇒ Object



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

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

#tr(rows) ⇒ Object



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

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

#ul_beginObject



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

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

#ul_endObject



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

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

#ul_item(lines) ⇒ Object



74
75
76
# File 'lib/review/markdownbuilder.rb', line 74

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

#ul_item_begin(lines) ⇒ Object



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

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

#ul_item_endObject



82
83
# File 'lib/review/markdownbuilder.rb', line 82

def ul_item_end
end