Module: RedCloth::Formatters::HTML
- Includes:
- Base
- Defined in:
- lib/redcloth/formatters/html.rb
Constant Summary collapse
- SWEARWORDS =
[ {:code=>/bullshit/,:replace=>"bullcrap"}, {:code=>/f[^a-zA-Z0-9]*[u*]+[^a-zA-Z0-9]*c[^a-zA-Z0-9]*k(er)?/,:replace=>"****"}, {:code=>/sh[i1!]t(t?y)?/,:replace=>"****"}, {:code=>/tits/,:replace=>"****"}, {:code=>/motherfucker/,:replace=>"******"}, {:code=>/bitch/,:replace=>"****"}, {:code=>/cunt/,:replace=>"****"}, {:code=>/asshole/,:replace=>"*******"}, {:code=>/cocksucker/,:replace=>"****"}, {:code=>/cock/,:replace=>"****"}, {:code=>/n(!|\||\!|i|1|\*)gg((!|3|e|\*)(!|r|4)|a)/,:replace=>"African-American"}, {:code=>/fag(got)?/,:replace=>"***"} ]
- LINK_TEXT_WITH_TITLE_RE =
/ ([^"]+?) # $text \s? \(([^)]+?)\) # $title $ /x
- BASIC_TAGS =
HTML cleansing stuff
{ 'a' => ['href', 'title'], 'img' => ['src', 'alt', 'title'], 'br' => [], 'i' => nil, 'u' => nil, 'b' => nil, 'pre' => nil, 'kbd' => nil, 'code' => ['lang'], 'cite' => nil, 'strong' => nil, 'em' => nil, 'ins' => nil, 'sup' => nil, 'sub' => nil, 'del' => nil, 'table' => nil, 'tr' => nil, 'td' => ['colspan', 'rowspan'], 'th' => nil, 'ol' => ['start'], 'ul' => nil, 'li' => nil, 'p' => nil, 'h1' => nil, 'h2' => nil, 'h3' => nil, 'h4' => nil, 'h5' => nil, 'h6' => nil, 'blockquote' => ['cite'], 'notextile' => nil }
Instance Method Summary collapse
- #acronym(opts) ⇒ Object
- #after_transform(text) ⇒ Object
- #amp(opts) ⇒ Object
- #apos(opts) ⇒ Object
- #arrow(opts) ⇒ Object
- #autolink(opts) ⇒ Object
- #bb_block_pre(opts) ⇒ Object
- #bb_block_spoiler(opts) ⇒ Object
- #bb_pre(opts) ⇒ Object
- #bb_spoiler(opts) ⇒ Object
- #bbalign(opts) ⇒ Object
- #bbquote(opts) ⇒ Object
- #bbsize(opts) ⇒ Object
- #bc_close(opts) ⇒ Object
- #bc_open(opts) ⇒ Object
- #before_transform(text) ⇒ Object
- #bq_close(opts) ⇒ Object
- #bq_open(opts) ⇒ Object
- #br(opts) ⇒ Object
- #caps(opts) ⇒ Object
-
#clean_html(text, allowed_tags = BASIC_TAGS) ⇒ Object
Clean unauthorized tags.
- #color(opts) ⇒ Object
- #copyright(opts) ⇒ Object
- #del(opts) ⇒ Object
- #dim(opts) ⇒ Object
- #dl_close(opts = nil) ⇒ Object
- #dl_open(opts) ⇒ Object
- #ellipsis(opts) ⇒ Object
- #emdash(opts) ⇒ Object
- #endash(opts) ⇒ Object
- #entity(opts) ⇒ Object
-
#escape(text) ⇒ Object
escapement for regular HTML (not in PRE tag).
-
#escape_attribute(text) ⇒ Object
escaping for HTML attributes.
-
#escape_pre(text) ⇒ Object
escapement for HTML in a PRE tag.
- #escape_pre_bb(text) ⇒ Object
- #fn(opts) ⇒ Object
- #footno(opts) ⇒ Object
- #gt(opts) ⇒ Object
- #hr(opts) ⇒ Object
- #html(opts) ⇒ Object
- #html_block(opts) ⇒ Object
- #ignored_line(opts) ⇒ Object
- #image(opts) ⇒ Object
- #inline_html(opts) ⇒ Object
- #li_close(opts = nil) ⇒ Object
- #li_open(opts) ⇒ Object
- #link(opts) ⇒ Object
- #lt(opts) ⇒ Object
- #multi_paragraph_quote(opts) ⇒ Object
- #notextile(opts) ⇒ Object
- #quot(opts) ⇒ Object
- #quote1(opts) ⇒ Object
- #quote2(opts) ⇒ Object
- #registered(opts) ⇒ Object
- #snip(opts) ⇒ Object
- #squot(opts) ⇒ Object
- #table_close(opts) ⇒ Object
- #table_open(opts) ⇒ Object
- #td(opts) ⇒ Object
- #tr_close(opts) ⇒ Object
- #tr_open(opts) ⇒ Object
- #trademark(opts) ⇒ Object
- #vimeo(opts) ⇒ Object
- #youtube(opts) ⇒ Object
Methods included from Base
#ignore, #inline_redcloth_version, #method_missing, #pba, #redcloth_version
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RedCloth::Formatters::Base
Instance Method Details
#acronym(opts) ⇒ Object
76 77 78 79 |
# File 'lib/redcloth/formatters/html.rb', line 76 def acronym(opts) opts[:block] = true "<acronym#{pba(opts)}>#{caps(:text => opts[:text])}</acronym>" end |
#after_transform(text) ⇒ Object
55 56 57 |
# File 'lib/redcloth/formatters/html.rb', line 55 def after_transform(text) text.chomp! end |
#amp(opts) ⇒ Object
301 302 303 |
# File 'lib/redcloth/formatters/html.rb', line 301 def amp(opts) "&" end |
#apos(opts) ⇒ Object
329 330 331 |
# File 'lib/redcloth/formatters/html.rb', line 329 def apos(opts) "'" end |
#arrow(opts) ⇒ Object
274 275 276 |
# File 'lib/redcloth/formatters/html.rb', line 274 def arrow(opts) "→" end |
#autolink(opts) ⇒ Object
185 186 187 |
# File 'lib/redcloth/formatters/html.rb', line 185 def autolink(opts) " #{link(opts)}" end |
#bb_block_pre(opts) ⇒ Object
215 216 217 218 219 220 |
# File 'lib/redcloth/formatters/html.rb', line 215 def bb_block_pre(opts) #quote = "<blockquote>" #quote += "<cite>#{opts[:cite]}</cite>" if opts[:cite] #quote += opts[:text]+"</blockquote>\n" "#{bb_pre(opts)}\n" end |
#bb_block_spoiler(opts) ⇒ Object
226 227 228 |
# File 'lib/redcloth/formatters/html.rb', line 226 def bb_block_spoiler(opts) "<div class=\"spoiler_container\"><div class=\"spoileroncontainer\"><button type=\"button\" class=\"button spoileron\" title=\"Click to show the spoiler.\">#{opts[:title] || "Show Spoiler"}</button></div><div class=\"spoiler\"><div class=\"spoileroffcontainer\"><button type=\"button\" class=\"button spoileroff\" title=\"Click to hide the spoiler.\">hide spoiler</button><br/></div><div class=\"spoilertext\">#{opts[:text]}</div></div></div>" end |
#bb_pre(opts) ⇒ Object
230 231 232 |
# File 'lib/redcloth/formatters/html.rb', line 230 def bb_pre(opts) "<pre><code>#{opts[:text]}</code></pre>" end |
#bb_spoiler(opts) ⇒ Object
222 223 224 |
# File 'lib/redcloth/formatters/html.rb', line 222 def bb_spoiler(opts) "<span class=\"inline_spoiler\" style=\"color:#333;background-color:#333;\">#{"<span class=\"inline_spoiler_title\" style=\"color:#F00\">#{opts[:title]}</span>" if opts[:title]}#{opts[:text]}</span>" end |
#bbalign(opts) ⇒ Object
205 206 207 |
# File 'lib/redcloth/formatters/html.rb', line 205 def bbalign(opts) "<div style=\"text-align:#{opts[:bbalign]};\">#{opts[:text]}</div>" end |
#bbquote(opts) ⇒ Object
209 210 211 212 213 |
# File 'lib/redcloth/formatters/html.rb', line 209 def bbquote(opts) quote = "<blockquote>" quote += "<cite>#{opts[:cite]}</cite>" if opts[:cite] quote += "#{opts[:text]}</blockquote>\n" end |
#bbsize(opts) ⇒ Object
201 202 203 |
# File 'lib/redcloth/formatters/html.rb', line 201 def bbsize(opts) "<span style=\"font-size:#{opts[:size]}#{"em" unless %w(em px pt %).include?(opts[:size][-2..-1])};\">#{opts[:text]}</span>" end |
#bc_close(opts) ⇒ Object
154 155 156 |
# File 'lib/redcloth/formatters/html.rb', line 154 def bc_close(opts) "</pre>\n" end |
#bc_open(opts) ⇒ Object
149 150 151 152 |
# File 'lib/redcloth/formatters/html.rb', line 149 def bc_open(opts) opts[:block] = true "<pre#{pba(opts)}>" end |
#before_transform(text) ⇒ Object
418 419 420 |
# File 'lib/redcloth/formatters/html.rb', line 418 def before_transform(text) clean_html(text) if sanitize_html end |
#bq_close(opts) ⇒ Object
164 165 166 |
# File 'lib/redcloth/formatters/html.rb', line 164 def bq_close(opts) "</blockquote>\n" end |
#bq_open(opts) ⇒ Object
158 159 160 161 162 |
# File 'lib/redcloth/formatters/html.rb', line 158 def bq_open(opts) opts[:block] = true cite = opts[:cite] ? " cite=\"#{ escape_attribute opts[:cite] }\"" : '' "<blockquote#{cite}#{pba(opts)}>\n" end |
#br(opts) ⇒ Object
313 314 315 316 317 318 319 |
# File 'lib/redcloth/formatters/html.rb', line 313 def br(opts) if hard_breaks == false "\n" else "<br />\n" end end |
#caps(opts) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/redcloth/formatters/html.rb', line 81 def caps(opts) if no_span_caps opts[:text] else opts[:class] = 'caps' span(opts) end end |
#clean_html(text, allowed_tags = BASIC_TAGS) ⇒ Object
Clean unauthorized tags.
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/redcloth/formatters/html.rb', line 459 def clean_html( text, = BASIC_TAGS ) text.gsub!( /<!\[CDATA\[/, '' ) text.gsub!( /<(\/*)([A-Za-z]\w*)([^>]*?)(\s?\/?)>/ ) do |m| raw = $~ tag = raw[2].downcase if .has_key? tag pcs = [tag] [tag].each do |prop| ['"', "'", ''].each do |q| q2 = ( q != '' ? q : '\s' ) if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i attrv = $1 next if (prop == 'src' or prop == 'href') and not attrv =~ %r{^(http|https|ftp):} pcs << "#{prop}=\"#{attrv.gsub('"', '\\"')}\"" break end end end if [tag] "<#{raw[1]}#{pcs.join " "}#{raw[4]}>" else # Unauthorized tag if block_given? yield m else '' end end end end |
#color(opts) ⇒ Object
197 198 199 |
# File 'lib/redcloth/formatters/html.rb', line 197 def color(opts) "<span style=\"color:#{opts[:color]};\">#{opts[:text]}</span>" end |
#copyright(opts) ⇒ Object
293 294 295 |
# File 'lib/redcloth/formatters/html.rb', line 293 def copyright(opts) "©" end |
#del(opts) ⇒ Object
90 91 92 93 |
# File 'lib/redcloth/formatters/html.rb', line 90 def del(opts) opts[:block] = true "<del#{pba(opts)}>#{opts[:text]}</del>" end |
#dim(opts) ⇒ Object
278 279 280 281 282 283 |
# File 'lib/redcloth/formatters/html.rb', line 278 def dim(opts) opts[:text].gsub!('x', '×') opts[:text].gsub!("'", '′') opts[:text].gsub!('"', '″') opts[:text] end |
#dl_close(opts = nil) ⇒ Object
118 119 120 |
# File 'lib/redcloth/formatters/html.rb', line 118 def dl_close(opts=nil) "</dl>\n" end |
#dl_open(opts) ⇒ Object
113 114 115 116 |
# File 'lib/redcloth/formatters/html.rb', line 113 def dl_open(opts) opts[:block] = true "<dl#{pba(opts)}>\n" end |
#ellipsis(opts) ⇒ Object
262 263 264 |
# File 'lib/redcloth/formatters/html.rb', line 262 def ellipsis(opts) "#{opts[:text]}…" end |
#emdash(opts) ⇒ Object
266 267 268 |
# File 'lib/redcloth/formatters/html.rb', line 266 def emdash(opts) "—" end |
#endash(opts) ⇒ Object
270 271 272 |
# File 'lib/redcloth/formatters/html.rb', line 270 def endash(opts) " – " end |
#entity(opts) ⇒ Object
297 298 299 |
# File 'lib/redcloth/formatters/html.rb', line 297 def entity(opts) "&#{opts[:text]};" end |
#escape(text) ⇒ Object
escapement for regular HTML (not in PRE tag)
32 33 34 35 36 37 38 39 |
# File 'lib/redcloth/formatters/html.rb', line 32 def escape(text) if self.filter_swears == true for swear in SWEARWORDS text.gsub!(swear[:code],swear[:replace]) end end html_esc(text) end |
#escape_attribute(text) ⇒ Object
escaping for HTML attributes
51 52 53 |
# File 'lib/redcloth/formatters/html.rb', line 51 def escape_attribute(text) html_esc(text, :html_escape_attributes) end |
#escape_pre(text) ⇒ Object
escapement for HTML in a PRE tag
42 43 44 |
# File 'lib/redcloth/formatters/html.rb', line 42 def escape_pre(text) html_esc(text, :html_escape_preformatted) end |
#escape_pre_bb(text) ⇒ Object
46 47 48 |
# File 'lib/redcloth/formatters/html.rb', line 46 def escape_pre_bb(text) text.gsub(/\"/,""") end |
#fn(opts) ⇒ Object
239 240 241 242 243 244 |
# File 'lib/redcloth/formatters/html.rb', line 239 def fn(opts) no = opts[:id] opts[:id] = "fn#{no}" opts[:class] = ["footnote", opts[:class]].compact.join(" ") "<p#{pba(opts)}><sup>#{no}</sup> #{opts[:text]}</p>\n" end |
#footno(opts) ⇒ Object
234 235 236 237 |
# File 'lib/redcloth/formatters/html.rb', line 234 def footno(opts) opts[:id] ||= opts[:text] %Q{<sup class="footnote"><a href=\"#fn#{opts[:id]}\">#{opts[:text]}</a></sup>} end |
#gt(opts) ⇒ Object
305 306 307 |
# File 'lib/redcloth/formatters/html.rb', line 305 def gt(opts) ">" end |
#hr(opts) ⇒ Object
72 73 74 |
# File 'lib/redcloth/formatters/html.rb', line 72 def hr(opts) "<hr />\n" end |
#html(opts) ⇒ Object
333 334 335 |
# File 'lib/redcloth/formatters/html.rb', line 333 def html(opts) "#{opts[:text]}\n" end |
#html_block(opts) ⇒ Object
392 393 394 395 396 |
# File 'lib/redcloth/formatters/html.rb', line 392 def html_block(opts) inline_html(:text => "#{opts[:indent_before_start]}#{opts[:start_tag]}#{opts[:indent_after_start]}") + "#{opts[:text]}" + inline_html(:text => "#{opts[:indent_before_end]}#{opts[:end_tag]}#{opts[:indent_after_end]}") end |
#ignored_line(opts) ⇒ Object
414 415 416 |
# File 'lib/redcloth/formatters/html.rb', line 414 def ignored_line(opts) opts[:text] + "\n" end |
#image(opts) ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/redcloth/formatters/html.rb', line 189 def image(opts) opts.delete(:align) opts[:alt] = opts[:title] img = "<img src=\"#{escape_attribute opts[:src]}\"#{pba(opts)} alt=\"#{escape_attribute opts[:alt].to_s}\" />" img = "<a href=\"#{escape_attribute opts[:href]}\">#{img}</a>" if opts[:href] img end |
#inline_html(opts) ⇒ Object
406 407 408 409 410 411 412 |
# File 'lib/redcloth/formatters/html.rb', line 406 def inline_html(opts) if filter_html html_esc(opts[:text], :html_escape_preformatted) else "#{opts[:text]}" # nil-safe end end |
#li_close(opts = nil) ⇒ Object
109 110 111 |
# File 'lib/redcloth/formatters/html.rb', line 109 def li_close(opts=nil) "</li>\n" end |
#li_open(opts) ⇒ Object
105 106 107 |
# File 'lib/redcloth/formatters/html.rb', line 105 def li_open(opts) "#{li_close unless opts.delete(:first)}#{"\t" * opts[:nest]}<li#{pba(opts)}>#{opts[:text]}" end |
#link(opts) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/redcloth/formatters/html.rb', line 174 def link(opts) if opts[:name] =~ LINK_TEXT_WITH_TITLE_RE md = LINK_TEXT_WITH_TITLE_RE.match(opts[:name]) opts[:name] = md[1] opts[:title] = md[2] elsif opts[:name].nil? && !opts[:href].nil? opts[:name] = opts[:href] end "<a href=\"#{escape_attribute opts[:href]}\"#{pba(opts)}>#{opts[:name]}</a>" end |
#lt(opts) ⇒ Object
309 310 311 |
# File 'lib/redcloth/formatters/html.rb', line 309 def lt(opts) "<" end |
#multi_paragraph_quote(opts) ⇒ Object
258 259 260 |
# File 'lib/redcloth/formatters/html.rb', line 258 def multi_paragraph_quote(opts) "“#{opts[:text]}" end |
#notextile(opts) ⇒ Object
398 399 400 401 402 403 404 |
# File 'lib/redcloth/formatters/html.rb', line 398 def notextile(opts) if filter_html html_esc(opts[:text], :html_escape_preformatted) else opts[:text] end end |
#quot(opts) ⇒ Object
321 322 323 |
# File 'lib/redcloth/formatters/html.rb', line 321 def quot(opts) """ end |
#quote1(opts) ⇒ Object
250 251 252 |
# File 'lib/redcloth/formatters/html.rb', line 250 def quote1(opts) "‘#{opts[:text]}’" end |
#quote2(opts) ⇒ Object
254 255 256 |
# File 'lib/redcloth/formatters/html.rb', line 254 def quote2(opts) "“#{opts[:text]}”" end |
#registered(opts) ⇒ Object
289 290 291 |
# File 'lib/redcloth/formatters/html.rb', line 289 def registered(opts) "®" end |
#snip(opts) ⇒ Object
246 247 248 |
# File 'lib/redcloth/formatters/html.rb', line 246 def snip(opts) "<pre#{pba(opts)}><code>#{opts[:text]}</code></pre>\n" end |
#squot(opts) ⇒ Object
325 326 327 |
# File 'lib/redcloth/formatters/html.rb', line 325 def squot(opts) "’" end |
#table_close(opts) ⇒ Object
145 146 147 |
# File 'lib/redcloth/formatters/html.rb', line 145 def table_close(opts) "</table>\n" end |
#table_open(opts) ⇒ Object
141 142 143 |
# File 'lib/redcloth/formatters/html.rb', line 141 def table_open(opts) "<table#{pba(opts)}>\n" end |
#td(opts) ⇒ Object
128 129 130 131 |
# File 'lib/redcloth/formatters/html.rb', line 128 def td(opts) tdtype = opts[:th] ? 'th' : 'td' "\t\t<#{tdtype}#{pba(opts)}>#{opts[:text]}</#{tdtype}>\n" end |
#tr_close(opts) ⇒ Object
137 138 139 |
# File 'lib/redcloth/formatters/html.rb', line 137 def tr_close(opts) "\t</tr>\n" end |
#tr_open(opts) ⇒ Object
133 134 135 |
# File 'lib/redcloth/formatters/html.rb', line 133 def tr_open(opts) "\t<tr#{pba(opts)}>\n" end |
#trademark(opts) ⇒ Object
285 286 287 |
# File 'lib/redcloth/formatters/html.rb', line 285 def trademark(opts) "™" end |
#vimeo(opts) ⇒ Object
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/redcloth/formatters/html.rb', line 371 def vimeo(opts) begin #http://www.vimeo.com/api/v2/video/6583886.xml url = URI.parse("http://www.vimeo.com/api/v2/video/#{opts[:vimeoid]}.xml") req = Net::HTTP::Get.new(url.path) res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) } d = REXML::Document.new(res.body) title = d.elements["videos/video/title"].text out = "#{title}" width = 480 height = 320 out = "<div class=\"youtube\"><div class=\"youtube_title\"><a href=\"http://vimeo.com/#{opts[:vimeoid]}\">#{title}</a></div><div class=\"youtube_video\"><object width=\"#{width}\" height=\"#{height}\"><param name=\"allowfullscreen\" value=\"true\" /><param name=\"allowscriptaccess\" value=\"always\" /><param name=\"movie\" value=\"http://vimeo.com/moogaloop.swf?clip_id=#{opts[:vimeoid]}&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1\" /><embed src=\"http://vimeo.com/moogaloop.swf?clip_id=#{opts[:vimeoid]}&server=vimeo.com&show_title=1&show_byline=1&show_portrait=0&color=&fullscreen=1\" type=\"application/x-shockwave-flash\" allowfullscreen=\"true\" allowscriptaccess=\"always\" width=\"#{width}\" height=\"#{height}\"></embed></object></div></div>" rescue out = "ERROR WITH VIMEO API! VIDEO DOES NOT EXIST? VIDEO NOT ALLOWED TO EMBED?" end out end |
#youtube(opts) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/redcloth/formatters/html.rb', line 337 def youtube(opts) #gdata.youtube.com/feeds/api/videos/Gkwhxh5txJ0 begin url = URI.parse("http://gdata.youtube.com/feeds/api/videos/#{opts[:youtubeid]}") req = Net::HTTP::Get.new(url.path) res = Net::HTTP.start(url.host, url.port) {|http| http.request(req) } d = REXML::Document.new(res.body) title = d.elements["entry/title"].text rescue title = "http://www.youtube.com/watch?v=#{opts[:youtubeid]}" end case opts[:youtubefmt] when "22" width = 700 height = 395 when "18" width = 480 height = 320 when "6" width = 480 height = 320 else width = 320 height = 240 end out = "<div class=\"youtube\"><div class=\"youtube_title\"><a href=\"http://www.youtube.com/watch?v=#{opts[:youtubeid]}#{"&fmt=#{opts[:youtubefmt]}" if opts[:youtubefmt]}\">#{title}</a></div><div class=\"youtube_video\"><object width=\"#{width}\" height=\"#{height}\"><param name=\"movie\" value=\"http://www.youtube.com/v/#{opts[:youtubeid]}&hl=en&fs=1&\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/#{opts[:youtubeid]}&hl=en&fs=1&\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"#{width}\" height=\"#{height}\"></embed></object></div></div>\n" out end |