Class: Redcarpet::Render::ReVIEW
- Inherits:
-
Base
- Object
- Base
- Redcarpet::Render::ReVIEW
- Defined in:
- lib/redcarpet/render/review.rb
Instance Method Summary collapse
- #autolink(link, link_type) ⇒ Object
- #block_code(code, language) ⇒ Object
- #block_html(raw_html) ⇒ Object
- #block_quote(quote) ⇒ Object
- #codespan(code) ⇒ Object
- #double_emphasis(text) ⇒ Object
- #emphasis(text) ⇒ Object
- #escape_href(text) ⇒ Object
- #escape_inline(text) ⇒ Object
- #footnote_def(text, number) ⇒ Object
- #footnote_ref(number) ⇒ Object
- #footnotes(text) ⇒ Object
- #header(title, level, anchor = "") ⇒ Object
- #hrule ⇒ Object
- #image(link, title, alt_text) ⇒ Object
-
#initialize(render_extensions = {}) ⇒ ReVIEW
constructor
A new instance of ReVIEW.
- #linebreak ⇒ Object
- #link(link, title, content) ⇒ Object
- #list(content, list_type) ⇒ Object
- #list_item(content, list_type) ⇒ Object
- #normal_text(text) ⇒ Object
- #paragraph(text) ⇒ Object
- #postprocess(text) ⇒ Object
- #preprocess(text) ⇒ Object
- #remove_inline_markups(text) ⇒ Object
- #ruby(text) ⇒ Object
- #sandwitch_link(op, text) ⇒ Object
- #strikethrough(text) ⇒ Object
- #table(header, body) ⇒ Object
- #table_cell(content, alignment) ⇒ Object
- #table_id ⇒ Object
- #table_row(content) ⇒ Object
- #tcy(text) ⇒ Object
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 |
# 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] @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
#autolink(link, link_type) ⇒ Object
175 176 177 |
# File 'lib/redcarpet/render/review.rb', line 175 def autolink(link, link_type) "@<href>{#{escape_href(link)}}" end |
#block_code(code, language) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/redcarpet/render/review.rb', line 65 def block_code(code, language) code_text = normal_text(code).chomp lang = "" 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
92 93 94 95 96 |
# File 'lib/redcarpet/render/review.rb', line 92 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
86 87 88 89 90 |
# File 'lib/redcarpet/render/review.rb', line 86 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
102 103 104 |
# File 'lib/redcarpet/render/review.rb', line 102 def codespan(code) "@<tt>{#{escape_inline(code)}}" end |
#double_emphasis(text) ⇒ Object
190 191 192 |
# File 'lib/redcarpet/render/review.rb', line 190 def double_emphasis(text) "@<strong>{#{escape_inline(text)}}" end |
#emphasis(text) ⇒ Object
194 195 196 |
# File 'lib/redcarpet/render/review.rb', line 194 def emphasis(text) sandwitch_link('b', text) end |
#escape_href(text) ⇒ Object
61 62 63 |
# File 'lib/redcarpet/render/review.rb', line 61 def escape_href(text) text.to_s.gsub(/,/){ '\\,' } end |
#escape_inline(text) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/redcarpet/render/review.rb', line 45 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
278 279 280 |
# File 'lib/redcarpet/render/review.rb', line 278 def footnote_def(text, number) "\n//footnote[#{number}][#{text.strip}]\n" end |
#footnote_ref(number) ⇒ Object
270 271 272 |
# File 'lib/redcarpet/render/review.rb', line 270 def footnote_ref(number) "@<fn>{#{number}}" end |
#footnotes(text) ⇒ Object
274 275 276 |
# File 'lib/redcarpet/render/review.rb', line 274 def footnotes(text) "#{text}" end |
#header(title, level, anchor = "") ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/redcarpet/render/review.rb', line 106 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 raise "too long header" end buf end |
#hrule ⇒ Object
98 99 100 |
# File 'lib/redcarpet/render/review.rb', line 98 def hrule "\n//hr\n" end |
#image(link, title, alt_text) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/redcarpet/render/review.rb', line 163 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 "//image[#{filename}][#{alt_text}]{\n//}\n" else "//indepimage[#{filename}]\n" end end |
#linebreak ⇒ Object
202 203 204 |
# File 'lib/redcarpet/render/review.rb', line 202 def linebreak "@<br>{}\n" end |
#link(link, title, content) ⇒ Object
179 180 181 182 183 184 185 186 187 188 |
# File 'lib/redcarpet/render/review.rb', line 179 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)},#{content}}" end end |
#list(content, list_type) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/redcarpet/render/review.rb', line 215 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 raise "invalid type: #{list_type}" end end ret << "\n" ret end |
#list_item(content, list_type) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/redcarpet/render/review.rb', line 240 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 raise "invalid type: #{list_type}" end end |
#normal_text(text) ⇒ Object
41 42 43 |
# File 'lib/redcarpet/render/review.rb', line 41 def normal_text(text) text end |
#paragraph(text) ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/redcarpet/render/review.rb', line 206 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
282 283 284 285 286 287 288 289 290 |
# File 'lib/redcarpet/render/review.rb', line 282 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
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/redcarpet/render/review.rb', line 27 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
292 293 294 |
# File 'lib/redcarpet/render/review.rb', line 292 def remove_inline_markups(text) text.gsub(/@<(?:b|strong|tt)>{([^}]*)}/, '\1') end |
#ruby(text) ⇒ Object
261 262 263 264 |
# File 'lib/redcarpet/render/review.rb', line 261 def ruby(text) rt, rb = text.split(/\|/, 2) "@<ruby>{#{escape_inline(rt)},#{escape_inline(rb)}}" end |
#sandwitch_link(op, text) ⇒ Object
296 297 298 299 300 301 302 303 304 |
# File 'lib/redcarpet/render/review.rb', line 296 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
198 199 200 |
# File 'lib/redcarpet/render/review.rb', line 198 def strikethrough(text) "@<del>{#{escape_inline(text)}}" end |
#table(header, body) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/redcarpet/render/review.rb', line 132 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
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/redcarpet/render/review.rb', line 152 def table_cell(content, alignment) sep = @sep @sep = "\t" if content == "" content = "." elsif content =~ /\A\./ content = "." + content end "#{sep}#{content}" end |
#table_id ⇒ Object
256 257 258 259 |
# File 'lib/redcarpet/render/review.rb', line 256 def table_id @table_num += 1 "#{@table_id_prefix}#{@table_num}" end |
#table_row(content) ⇒ Object
147 148 149 150 |
# File 'lib/redcarpet/render/review.rb', line 147 def table_row(content) @sep = nil content+"\n" end |
#tcy(text) ⇒ Object
266 267 268 |
# File 'lib/redcarpet/render/review.rb', line 266 def tcy(text) "@<tcy>{#{escape_inline(text)}}" end |