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
200 def amp(opts)
201   "&amp;"
202 end
apos(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
228 def apos(opts)
229   "&#39;"
230 end
arrow(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
173 def arrow(opts)
174   "&#8594;"
175 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
212 def br(opts)
213   if hard_breaks == false
214     "\n"
215   else
216     "<br#{pba(opts)} />\n"
217   end
218 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
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
177 def dim(opts)
178   opts[:text].gsub!('x', '&#215;')
179   opts[:text].gsub!("'", '&#8242;')
180   opts[:text].gsub!('"', '&#8243;')
181   opts[:text]
182 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
161 def ellipsis(opts)
162   "#{opts[:text]}&#8230;"
163 end
emdash(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
165 def emdash(opts)
166   "&#8212;"
167 end
endash(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
169 def endash(opts)
170   " &#8211; "
171 end
entity(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
196 def entity(opts)
197   "&#{opts[:text]};"
198 end
fn(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
138 def fn(opts)
139   no = opts[:id]
140   opts[:id] = "fn#{no}"
141   opts[:class] = ["footnote", opts[:class]].compact.join(" ")
142   "<p#{pba(opts)}><a href=\"#fnr#{no}\"><sup>#{no}</sup></a> #{opts[:text]}</p>\n"
143 end
footno(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
133 def footno(opts)
134   opts[:id] ||= opts[:text]
135   %Q{<sup class="footnote" id=\"fnr#{opts[:id]}\"><a href=\"#fn#{opts[:id]}\">#{opts[:text]}</a></sup>}
136 end
gt(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
204 def gt(opts)
205   "&gt;"
206 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
232 def html(opts)
233   "#{opts[:text]}\n"
234 end
html_block(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
236 def html_block(opts)
237   inline_html(:text => "#{opts[:indent_before_start]}#{opts[:start_tag]}#{opts[:indent_after_start]}") + 
238   "#{opts[:text]}" +
239   inline_html(:text => "#{opts[:indent_before_end]}#{opts[:end_tag]}#{opts[:indent_after_end]}")
240 end
ignored_line(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
258 def ignored_line(opts)
259   opts[:text] + "\n"
260 end
image(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
121 def image(opts)
122   if (filter_html || sanitize_html) && ( opts[:src] =~ /^\s*javascript:/i || opts[:href] =~ /^\s*javascript:/i )
123     opts[:title]
124   else
125     opts.delete(:align)
126     opts[:alt] = opts[:title]
127     img = "<img src=\"#{escape_attribute opts[:src]}\"#{pba(opts)} alt=\"#{escape_attribute opts[:alt].to_s}\" />"  
128     img = "<a href=\"#{escape_attribute opts[:href]}\">#{img}</a>" if opts[:href]
129     img
130   end
131 end
inline_html(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
250 def inline_html(opts)
251   if filter_html
252     html_esc(opts[:text], :html_escape_preformatted)
253   else
254     "#{opts[:text]}" # nil-safe
255   end
256 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
lt(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
208 def lt(opts)
209   "&lt;"
210 end
multi_paragraph_quote(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
157 def multi_paragraph_quote(opts)
158   "&#8220;#{opts[:text]}"
159 end
notextile(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
242 def notextile(opts)
243   if filter_html
244     html_esc(opts[:text], :html_escape_preformatted)
245   else
246     opts[:text]
247   end
248 end
quot(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
220 def quot(opts)
221   "&quot;"
222 end
quote1(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
149 def quote1(opts)
150   "&#8216;#{opts[:text]}&#8217;"
151 end
quote2(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
153 def quote2(opts)
154   "&#8220;#{opts[:text]}&#8221;"
155 end
registered(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
188 def registered(opts)
189   "&#174;"
190 end
snip(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
145 def snip(opts)
146   "<pre#{pba(opts)}><code>#{opts[:text]}</code></pre>\n"
147 end
squot(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
224 def squot(opts)
225   "&#8217;"
226 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
184 def trademark(opts)
185   "&#8482;"
186 end

Private Instance Methods

after_transform(text) click to toggle source
    # File lib/redcloth/formatters/html.rb
279 def after_transform(text)
280   text.chomp!
281 end
before_transform(text) click to toggle source
    # File lib/redcloth/formatters/html.rb
284 def before_transform(text)
285   clean_html(text) if sanitize_html
286 end
clean_html( text, allowed_tags = BASIC_TAGS ) { |m| ... } click to toggle source

Clean unauthorized tags.

    # File lib/redcloth/formatters/html.rb
325 def clean_html( text, allowed_tags = BASIC_TAGS )
326   text.gsub!( /<!\[CDATA\[/, '' )
327   text.gsub!( /<(\/*)([A-Za-z]\w*+)([^>]*?)(\s?\/?)>/ ) do |m|
328     raw = $~
329     tag = raw[2].downcase
330     if allowed_tags.has_key? tag
331       pcs = [tag]
332       allowed_tags[tag].each do |prop|
333         ['"', "'", ''].each do |q|
334           q2 = ( q != '' ? q : '\s' )
335           if raw[3] =~ /#{prop}\s*=\s*#{q}([^#{q2}]+)#{q}/i
336             attrv = $1
337             next if (prop == 'src' or prop == 'href') and not attrv =~ %r{^(http|https|ftp):}
338             pcs << "#{prop}=\"#{attrv.gsub('"', '\\"')}\""
339             break
340           end
341         end
342       end if allowed_tags[tag]
343       "<#{raw[1]}#{pcs.join " "}#{raw[4]}>"
344     else # Unauthorized tag
345       if block_given?
346         yield m
347       else
348         ''
349       end
350     end
351   end
352 end
escape(text) click to toggle source

escapement for regular HTML (not in PRE tag)

    # File lib/redcloth/formatters/html.rb
265 def escape(text)
266   html_esc(text)
267 end
escape_attribute(text) click to toggle source

escaping for HTML attributes

    # File lib/redcloth/formatters/html.rb
275 def escape_attribute(text)
276   html_esc(text, :html_escape_attributes)
277 end
escape_pre(text) click to toggle source

escapement for HTML in a PRE tag

    # File lib/redcloth/formatters/html.rb
270 def escape_pre(text)
271   html_esc(text, :html_escape_preformatted)
272 end