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
199 def amp(opts)
200   "&amp;"
201 end
apos(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
227 def apos(opts)
228   "&#39;"
229 end
arrow(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
172 def arrow(opts)
173   "&#8594;"
174 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
211 def br(opts)
212   if hard_breaks == false
213     "\n"
214   else
215     "<br#{pba(opts)} />\n"
216   end
217 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
176 def dim(opts)
177   opts[:text].gsub!('x', '&#215;')
178   opts[:text].gsub!("'", '&#8242;')
179   opts[:text].gsub!('"', '&#8243;')
180   opts[:text]
181 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
160 def ellipsis(opts)
161   "#{opts[:text]}&#8230;"
162 end
emdash(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
164 def emdash(opts)
165   "&#8212;"
166 end
endash(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
168 def endash(opts)
169   " &#8211; "
170 end
entity(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
195 def entity(opts)
196   "&#{opts[:text]};"
197 end
fn(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
137 def fn(opts)
138   no = opts[:id]
139   opts[:id] = "fn#{no}"
140   opts[:class] = ["footnote", opts[:class]].compact.join(" ")
141   "<p#{pba(opts)}><a href=\"#fnr#{no}\"><sup>#{no}</sup></a> #{opts[:text]}</p>\n"
142 end
footno(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
132 def footno(opts)
133   opts[:id] ||= opts[:text]
134   %Q{<sup class="footnote" id=\"fnr#{opts[:id]}\"><a href=\"#fn#{opts[:id]}\">#{opts[:text]}</a></sup>}
135 end
gt(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
203 def gt(opts)
204   "&gt;"
205 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
231 def html(opts)
232   "#{opts[:text]}\n"
233 end
html_block(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
235 def html_block(opts)
236   inline_html(:text => "#{opts[:indent_before_start]}#{opts[:start_tag]}#{opts[:indent_after_start]}") + 
237   "#{opts[:text]}" +
238   inline_html(:text => "#{opts[:indent_before_end]}#{opts[:end_tag]}#{opts[:indent_after_end]}")
239 end
ignored_line(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
257 def ignored_line(opts)
258   opts[:text] + "\n"
259 end
image(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
124 def image(opts)
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
inline_html(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
249 def inline_html(opts)
250   if filter_html
251     html_esc(opts[:text], :html_escape_preformatted)
252   else
253     "#{opts[:text]}" # nil-safe
254   end
255 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
207 def lt(opts)
208   "&lt;"
209 end
multi_paragraph_quote(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
156 def multi_paragraph_quote(opts)
157   "&#8220;#{opts[:text]}"
158 end
notextile(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
241 def notextile(opts)
242   if filter_html
243     html_esc(opts[:text], :html_escape_preformatted)
244   else
245     opts[:text]
246   end
247 end
quot(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
219 def quot(opts)
220   "&quot;"
221 end
quote1(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
148 def quote1(opts)
149   "&#8216;#{opts[:text]}&#8217;"
150 end
quote2(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
152 def quote2(opts)
153   "&#8220;#{opts[:text]}&#8221;"
154 end
registered(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
187 def registered(opts)
188   "&#174;"
189 end
snip(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
144 def snip(opts)
145   "<pre#{pba(opts)}><code>#{opts[:text]}</code></pre>\n"
146 end
squot(opts) click to toggle source
    # File lib/redcloth/formatters/html.rb
223 def squot(opts)
224   "&#8217;"
225 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
183 def trademark(opts)
184   "&#8482;"
185 end

Private Instance Methods

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

Clean unauthorized tags.

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

escapement for regular HTML (not in PRE tag)

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

escaping for HTML attributes

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

escapement for HTML in a PRE tag

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