module RedCloth::Formatters::HTML
Constants
- BASIC_TAGS
HTML
cleansing stuff
Public Instance Methods
acronym(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 21 def acronym(opts) 22 opts[:block] = true 23 "<acronym#{pba(opts)}>#{caps(:text => opts[:text])}</acronym>" 24 end
amp(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 208 def amp(opts) 209 "&" 210 end
apos(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 236 def apos(opts) 237 "'" 238 end
arrow(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 181 def arrow(opts) 182 "→" 183 end
auto_link(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 123 def auto_link(opts) 124 return opts[:href] unless auto_link_urls 125 href_with_proto = opts[:href] 126 return if !href_with_proto 127 href_with_proto = 'http://' + href_with_proto unless href_with_proto.index('http') == 0 128 text = opts[:href] 129 text = truncate(text[0, text.length - 6] + text[-5,3].replace("...") + text[-2..-1], 50) 130 "<a href=\"#{escape_attribute href_with_proto}\">#{escape_attribute text}</a>" 131 end
bc_close(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 99 def bc_close(opts) 100 "</pre>\n" 101 end
bc_open(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 94 def bc_open(opts) 95 opts[:block] = true 96 "<pre#{pba(opts)}>" 97 end
bq_close(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 109 def bq_close(opts) 110 "</blockquote>\n" 111 end
bq_open(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 103 def bq_open(opts) 104 opts[:block] = true 105 cite = opts[:cite] ? " cite=\"#{ escape_attribute opts[:cite] }\"" : '' 106 "<blockquote#{cite}#{pba(opts)}>\n" 107 end
br(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 220 def br(opts) 221 if hard_breaks == false 222 "\n" 223 else 224 "<br#{pba(opts)} />\n" 225 end 226 end
caps(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 26 def caps(opts) 27 if no_span_caps 28 opts[:text] 29 else 30 opts[:class] = 'caps' 31 span(opts) 32 end 33 end
copyright(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 200 def copyright(opts) 201 "©" 202 end
del(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 35 def del(opts) 36 opts[:block] = true 37 "<del#{pba(opts)}>#{opts[:text]}</del>" 38 end
dim(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 185 def dim(opts) 186 opts[:text].gsub!('x', '×') 187 opts[:text].gsub!("'", '′') 188 opts[:text].gsub!('"', '″') 189 opts[:text] 190 end
dl_close(opts=nil)
click to toggle source
# File lib/redcloth/formatters/html.rb 63 def dl_close(opts=nil) 64 "</dl>\n" 65 end
dl_open(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 58 def dl_open(opts) 59 opts[:block] = true 60 "<dl#{pba(opts)}>\n" 61 end
ellipsis(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 169 def ellipsis(opts) 170 "#{opts[:text]}…" 171 end
emdash(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 173 def emdash(opts) 174 "—" 175 end
endash(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 177 def endash(opts) 178 " – " 179 end
entity(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 204 def entity(opts) 205 "&#{opts[:text]};" 206 end
fn(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 146 def fn(opts) 147 no = opts[:id] 148 opts[:id] = "fn#{no}" 149 opts[:class] = ["footnote", opts[:class]].compact.join(" ") 150 "<p#{pba(opts)}><a href=\"#fnr#{no}\"><sup>#{no}</sup></a> #{opts[:text]}</p>\n" 151 end
footno(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 141 def footno(opts) 142 opts[:id] ||= opts[:text] 143 %Q{<sup class="footnote" id=\"fnr#{opts[:id]}\"><a href=\"#fn#{opts[:id]}\">#{opts[:text]}</a></sup>} 144 end
gt(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 212 def gt(opts) 213 ">" 214 end
hr(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 17 def hr(opts) 18 "<hr#{pba(opts)} />\n" 19 end
html(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 240 def html(opts) 241 "#{opts[:text]}\n" 242 end
html_block(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 244 def html_block(opts) 245 inline_html(:text => "#{opts[:indent_before_start]}#{opts[:start_tag]}#{opts[:indent_after_start]}") + 246 "#{opts[:text]}" + 247 inline_html(:text => "#{opts[:indent_before_end]}#{opts[:end_tag]}#{opts[:indent_after_end]}") 248 end
ignored_line(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 266 def ignored_line(opts) 267 opts[:text] + "\n" 268 end
image(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 133 def image(opts) 134 opts.delete(:align) 135 opts[:alt] = opts[:title] 136 img = "<img src=\"#{escape_attribute opts[:src]}\"#{pba(opts)} alt=\"#{escape_attribute opts[:alt].to_s}\" />" 137 img = "<a href=\"#{escape_attribute opts[:href]}\">#{img}</a>" if opts[:href] 138 img 139 end
inline_html(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 258 def inline_html(opts) 259 if filter_html 260 html_esc(opts[:text], :html_escape_preformatted) 261 else 262 "#{opts[:text]}" # nil-safe 263 end 264 end
li_close(opts=nil)
click to toggle source
# File lib/redcloth/formatters/html.rb 54 def li_close(opts=nil) 55 "</li>\n" 56 end
li_open(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 50 def li_open(opts) 51 "#{"\t" * opts[:nest]}<li#{pba(opts)}>#{opts[:text]}" 52 end
link(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 113 def link(opts) 114 "<a href=\"#{escape_attribute opts[:href]}\"#{pba(opts)}>#{opts[:name]}</a>" 115 end
lt(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 216 def lt(opts) 217 "<" 218 end
multi_paragraph_quote(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 165 def multi_paragraph_quote(opts) 166 "“#{opts[:text]}" 167 end
notextile(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 250 def notextile(opts) 251 if filter_html 252 html_esc(opts[:text], :html_escape_preformatted) 253 else 254 opts[:text] 255 end 256 end
quot(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 228 def quot(opts) 229 """ 230 end
quote1(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 157 def quote1(opts) 158 "‘#{opts[:text]}’" 159 end
quote2(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 161 def quote2(opts) 162 "“#{opts[:text]}”" 163 end
registered(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 196 def registered(opts) 197 "®" 198 end
snip(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 153 def snip(opts) 154 "<pre#{pba(opts)}><code>#{opts[:text]}</code></pre>\n" 155 end
squot(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 232 def squot(opts) 233 "’" 234 end
table_close(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 90 def table_close(opts) 91 "</table>\n" 92 end
table_open(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 86 def table_open(opts) 87 "<table#{pba(opts)}>\n" 88 end
td(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 73 def td(opts) 74 tdtype = opts[:th] ? 'th' : 'td' 75 "\t\t<#{tdtype}#{pba(opts)}>#{opts[:text]}</#{tdtype}>\n" 76 end
tr_close(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 82 def tr_close(opts) 83 "\t</tr>\n" 84 end
tr_open(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 78 def tr_open(opts) 79 "\t<tr#{pba(opts)}>\n" 80 end
trademark(opts)
click to toggle source
# File lib/redcloth/formatters/html.rb 192 def trademark(opts) 193 "™" 194 end
truncate(text, length = 30, truncate_string = "...")
click to toggle source
# File lib/redcloth/formatters/html.rb 117 def truncate(text, length = 30, truncate_string = "...") 118 if text.nil? then return end 119 l = length - truncate_string.chars.to_a.size 120 (text.chars.to_a.size > length ? text.chars.to_a[0...l].join + truncate_string : text).to_s 121 end
Private Instance Methods
after_transform(text)
click to toggle source
# File lib/redcloth/formatters/html.rb 287 def after_transform(text) 288 text.chomp! 289 end
before_transform(text)
click to toggle source
# File lib/redcloth/formatters/html.rb 292 def before_transform(text) 293 clean_html(text) if sanitize_html 294 end
clean_html( text, allowed_tags = BASIC_TAGS ) { |m| ... }
click to toggle source
Clean unauthorized tags.
# File lib/redcloth/formatters/html.rb 333 def clean_html( text, allowed_tags = BASIC_TAGS ) 334 text.gsub!( /<!\[CDATA\[/, '' ) 335 text.gsub!( /<(\/*)([A-Za-z]\w*)([^>]*?)(\s?\/?)>/ ) do |m| 336 raw = $~ 337 tag = raw[2].downcase 338 if allowed_tags.has_key? tag 339 pcs = [tag] 340 allowed_tags[tag].each do |prop| 341 ['"', "'", ''].each do |q| 342 q2 = ( q != '' ? q : '\s' ) 343 if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i 344 attrv = $1 345 next if (prop == 'src' or prop == 'href') and not attrv =~ %r{^(http|https|ftp):} 346 pcs << "#{prop}=\"#{attrv.gsub('"', '\\"')}\"" 347 break 348 end 349 end 350 end if allowed_tags[tag] 351 "<#{raw[1]}#{pcs.join " "}#{raw[4]}>" 352 else # Unauthorized tag 353 if block_given? 354 yield m 355 else 356 '' 357 end 358 end 359 end 360 end
escape(text)
click to toggle source
escapement for regular HTML
(not in PRE tag)
# File lib/redcloth/formatters/html.rb 273 def escape(text) 274 html_esc(text) 275 end
escape_attribute(text)
click to toggle source
escaping for HTML
attributes
# File lib/redcloth/formatters/html.rb 283 def escape_attribute(text) 284 html_esc(text, :html_escape_attributes) 285 end
escape_pre(text)
click to toggle source
escapement for HTML
in a PRE tag
# File lib/redcloth/formatters/html.rb 278 def escape_pre(text) 279 html_esc(text, :html_escape_preformatted) 280 end