Class: Redcarpet::Render::ReVIEW

Inherits:
Base
  • Object
show all
Defined in:
lib/redcarpet/render/review.rb

Instance Method Summary collapse

Constructor Details

#initialize(render_extensions = {}) ⇒ ReVIEW

Returns a new instance of ReVIEW.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/redcarpet/render/review.rb', line 7

def initialize(render_extensions={})
  super()
  @table_num = 0
  @table_id_prefix = "tbl"
  @header_offset = 0
  @link_in_footnote = render_extensions[:link_in_footnote]
  @image_caption = !render_extensions[:disable_image_caption]
  if render_extensions[:empty_image_caption]
    @image_caption = false
    @empty_image_caption = true
  else
    @empty_image_caption = false
  end
  @image_table = render_extensions[:image_table]
  if render_extensions[:header_offset]
    @header_offset = render_extensions[:header_offset]
  end
  @links = {}
  @cmd = render_extensions[:enable_cmd]
  @support_table_caption = render_extensions[:table_caption]
  @table_caption = nil
  @math = render_extensions[:math]
  @math_buf = []
  @sep = nil
end

Instance Method Details



184
185
186
# File 'lib/redcarpet/render/review.rb', line 184

def autolink(link, link_type)
  "@<href>{#{escape_href(link)}}"
end

#block_code(code, language) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/redcarpet/render/review.rb', line 73

def block_code(code, language)
  code_text = normal_text(code).chomp
  caption = ""
  if language
    if language =~ /caption=\"(.*)\"/
      caption = "["+$1+"]"
    else
      caption = "[][#{language}]"
    end
  end

  if @cmd && (language == "shell-session" || language == "console")
    "\n//cmd{\n#{code_text}\n//}\n"
  elsif @math && language == "math"
    "\n//texequation{\n#{code.chomp}\n//}\n"
  else
    "\n//emlist#{caption}{\n#{code_text}\n//}\n"
  end
end

#block_html(raw_html) ⇒ Object



99
100
101
102
103
# File 'lib/redcarpet/render/review.rb', line 99

def block_html(raw_html)
  html_text = raw_html.chomp
  warning = "XXX: BLOCK_HTML: YOU SHOULD REWRITE IT"
  "\n//emlist{\n#{warning}\n#{html_text}\n//}\n"
end

#block_quote(quote) ⇒ Object



93
94
95
96
97
# File 'lib/redcarpet/render/review.rb', line 93

def block_quote(quote)
  quote_text = normal_text(quote).chomp
  quote_text.gsub!(/\A\n\n/, '')
  "\n//quote{\n#{quote_text}\n//}\n"
end

#codespan(code) ⇒ Object



109
110
111
# File 'lib/redcarpet/render/review.rb', line 109

def codespan(code)
  "@<tt>{#{escape_inline(code)}}"
end

#double_emphasis(text) ⇒ Object



199
200
201
# File 'lib/redcarpet/render/review.rb', line 199

def double_emphasis(text)
  "@<strong>{#{escape_inline(text)}}"
end

#emphasis(text) ⇒ Object



203
204
205
# File 'lib/redcarpet/render/review.rb', line 203

def emphasis(text)
  sandwitch_link('b', text)
end

#escape_href(text) ⇒ Object Also known as: escape_comma



67
68
69
# File 'lib/redcarpet/render/review.rb', line 67

def escape_href(text)
  text.to_s.gsub(/,/){ '\\,' }
end

#escape_inline(text) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/redcarpet/render/review.rb', line 51

def escape_inline(text)
  ## }  -> \}
  ## \} -> \\\}
  ## .}  -> .\}
  text.gsub(/(.)?}/) do
    if $1 == '\\'
      replaced = '\\\\\\}'
    elsif $1
      replaced = $1 + '\\}'
    else
      replaced = '\\}'
    end
    replaced
  end
end

#footnote_def(text, number) ⇒ Object



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

def footnote_def(text, number)
  "\n//footnote[#{number}][#{text.strip}]\n"
end

#footnote_ref(number) ⇒ Object



283
284
285
# File 'lib/redcarpet/render/review.rb', line 283

def footnote_ref(number)
  "@<fn>{#{number}}"
end

#footnotes(text) ⇒ Object



287
288
289
# File 'lib/redcarpet/render/review.rb', line 287

def footnotes(text)
  "#{text}"
end

#header(title, level, anchor = "") ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/redcarpet/render/review.rb', line 113

def header(title, level, anchor="")
  buf = ""
  if /\s+(\{.*?\})\s*$/ =~ title
    buf << "\n#@# header_attribute: #{$1}"
    title = $`
  end
  l = level - @header_offset
  case l
  when 1
    buf << "\n= #{title}\n"
  when 2
    buf << "\n== #{title}\n"
  when 3
    buf << "\n=== #{title}\n"
  when 4
    buf << "\n==== #{title}\n"
  when 5
    buf << "\n===== #{title}\n"
  when 6
    buf << "\n====== #{title}\n"
  else
    # :nocov:
    raise "too long header"
    # :nocov:
  end
  buf
end

#hruleObject



105
106
107
# File 'lib/redcarpet/render/review.rb', line 105

def hrule
  "\n//hr\n"
end

#image(link, title, alt_text) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
# File 'lib/redcarpet/render/review.rb', line 172

def image(link, title, alt_text)
  filename = File.basename(link, ".*")
  if @image_table && alt_text =~ /\ATable:\s*(.*)/
    caption = $1
    "//imgtable[#{filename}][#{caption}]{\n//}\n"
  elsif @image_caption || (@empty_image_caption && alt_text.to_s.size > 0)
    "//image[#{filename}][#{alt_text}]{\n//}\n"
  else
    "//indepimage[#{filename}]\n"
  end
end

#linebreakObject



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

def linebreak
  "@<br>{}\n"
end


188
189
190
191
192
193
194
195
196
197
# File 'lib/redcarpet/render/review.rb', line 188

def link(link, title, content)
  if @link_in_footnote
    key = Digest::MD5.hexdigest(link)
    @links[key] ||= link
    footnotes(content) + footnote_ref(key)
  else
    content = escape_inline(remove_inline_markups(content))
    "@<href>{#{escape_href(link)},#{escape_comma(content)}}"
  end
end

#list(content, list_type) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/redcarpet/render/review.rb', line 224

def list(content, list_type)
  ret = ""
  content.each_line do |item|
    case list_type
    when :ordered
      if item =~ /^ +(\d+\.) (.*)/
        ## XXX not support yet in Re:VIEW
        ret << " #{$1} #{$2.chomp}" << "\n"
      else
        ret << " 1. " << item
      end
    when :unordered
      if item =~ /^ (\*+) (.*)/
        ret << " *#{$1} #{$2.chomp}" << "\n"
      else
        ret << " * " << item
      end
    else
      # :nocov:
      raise "invalid type: #{list_type}"
      # :nocov:
    end
  end
  ret << "\n"
  ret
end

#list_item(content, list_type) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/redcarpet/render/review.rb', line 251

def list_item(content, list_type)
  content.gsub!(%r<\n//(image|indepimage)\[([^\]]*?)\][^\{]*({\n//})?\n>){
    "@<icon>{"+$2+"}\n"
  }
  case list_type
  when :ordered
    item = content.gsub(/\n(\s+[^0-9])/){ $1 }.gsub(/\n(\s+[0-9]+[^.])/){ $1 }.strip
    "#{item}\n"
  when :unordered
    item = content.gsub(/\n(\s*[^* ])/){ $1 }.strip
    "#{item}\n"
  else
    # :nocov:
    raise "invalid type: #{list_type}"
    # :nocov:
  end
end

#normal_text(text) ⇒ Object



47
48
49
# File 'lib/redcarpet/render/review.rb', line 47

def normal_text(text)
  text
end

#paragraph(text) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/redcarpet/render/review.rb', line 215

def paragraph(text)
  if @support_table_caption && text =~ /\ATable:(.*)\z/
    @table_caption = $1  ## and no output line
    ""
  else
    "\n\n#{text}\n\n"
  end
end

#postprocess(text) ⇒ Object



295
296
297
298
299
300
301
302
303
# File 'lib/redcarpet/render/review.rb', line 295

def postprocess(text)
  text = text.gsub(%r|^[ \t]+(//image\[[^\]]+\]\[[^\]]+\]{$\n^//})|, '\1')
  if @math
    while %r|〓MATH:(\d+):〓| =~ text
      text.sub!(%r|〓MATH:(\d+):〓|){ "@<m>{" + escape_inline(@math_buf[$1.to_i]) + "}" }
    end
  end
  text + @links.map { |key, link| footnote_def(link, key) }.join
end

#preprocess(text) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/redcarpet/render/review.rb', line 33

def preprocess(text)
  counter = -1
  if @math
    while %r|\$\$(.+?)\$\$| =~ text
      text.sub!(%r|\$\$(.+?)\$\$|) do
        counter += 1
        @math_buf[counter] = $1
        "〓MATH:#{counter}:〓"
      end
    end
  end
  text
end

#remove_inline_markups(text) ⇒ Object



305
306
307
# File 'lib/redcarpet/render/review.rb', line 305

def remove_inline_markups(text)
  text.gsub(/@<(?:b|strong|tt)>{([^}]*)}/, '\1')
end

#ruby(text) ⇒ Object



274
275
276
277
# File 'lib/redcarpet/render/review.rb', line 274

def ruby(text)
  rt, rb = text.split(/\|/, 2)
  "@<ruby>{#{escape_inline(rt)},#{escape_inline(rb)}}"
end


309
310
311
312
313
314
315
316
317
# File 'lib/redcarpet/render/review.rb', line 309

def sandwitch_link(op, text)
  head, match, tail = text.partition(/@<href>{(?:\\,|[^}])*}/)

  if match.empty? && tail.empty?
    return "@<#{op}>{#{escape_inline(text)}}"
  end

  sandwitch_link(op, head) + match + sandwitch_link(op, tail)
end

#strikethrough(text) ⇒ Object



207
208
209
# File 'lib/redcarpet/render/review.rb', line 207

def strikethrough(text)
  "@<del>{#{escape_inline(text)}}"
end

#table(header, body) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/redcarpet/render/review.rb', line 141

def table(header, body)
  @sep = nil
  header_text = ""
  if header
    header_text = "#{header}-----------------\n"
  end
  body.chomp!
  caption = nil
  if @table_caption
    caption = @table_caption.strip
    @table_caption = nil
  end
  "//table[#{table_id()}][#{caption}]{\n#{header_text}#{body}\n//}\n"
end

#table_cell(content, alignment) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/redcarpet/render/review.rb', line 161

def table_cell(content, alignment)
  sep = @sep
  @sep = "\t"
  if content == ""
    content = "."
  elsif content =~ /\A\./
    content = "." + content
  end
  "#{sep}#{content}"
end

#table_idObject



269
270
271
272
# File 'lib/redcarpet/render/review.rb', line 269

def table_id
  @table_num += 1
  "#{@table_id_prefix}#{@table_num}"
end

#table_row(content) ⇒ Object



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

def table_row(content)
  @sep = nil
  content+"\n"
end

#tcy(text) ⇒ Object



279
280
281
# File 'lib/redcarpet/render/review.rb', line 279

def tcy(text)
  "@<tcy>{#{escape_inline(text)}}"
end