| Module | RedCloth::Formatters::HTML |
| In: |
lib/redcloth/formatters/html.rb
lib/redcloth/formatters/html.rb |
| LINK_TEXT_WITH_TITLE_RE | = | / ([^"]+?) # $text \s? \(([^)]+?)\) # $title $ /x | ||
| BASIC_TAGS | = | { '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 | HTML cleansing stuff | |
| LINK_TEXT_WITH_TITLE_RE | = | / ([^"]+?) # $text \s? \(([^)]+?)\) # $title $ /x | ||
| BASIC_TAGS | = | { '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 | HTML cleansing stuff |
# File lib/redcloth/formatters/html.rb, line 40
40: def acronym(opts)
41: opts[:block] = true
42: "<acronym#{pba(opts)}>#{caps(:text => opts[:text])}</acronym>"
43: end
# File lib/redcloth/formatters/html.rb, line 40
40: def acronym(opts)
41: opts[:block] = true
42: "<acronym#{pba(opts)}>#{caps(:text => opts[:text])}</acronym>"
43: end
# File lib/redcloth/formatters/html.rb, line 19
19: def after_transform(text)
20: text.chomp!
21: end
# File lib/redcloth/formatters/html.rb, line 19
19: def after_transform(text)
20: text.chomp!
21: end
# File lib/redcloth/formatters/html.rb, line 113
113: def bc_open(opts)
114: opts[:block] = true
115: "<pre#{pba(opts)}>"
116: end
# File lib/redcloth/formatters/html.rb, line 113
113: def bc_open(opts)
114: opts[:block] = true
115: "<pre#{pba(opts)}>"
116: end
# File lib/redcloth/formatters/html.rb, line 284
284: def before_transform(text)
285: clean_html(text) if sanitize_html
286: end
# File lib/redcloth/formatters/html.rb, line 284
284: def before_transform(text)
285: clean_html(text) if sanitize_html
286: end
# File lib/redcloth/formatters/html.rb, line 128
128: def bq_close(opts)
129: "</blockquote>\n"
130: end
# File lib/redcloth/formatters/html.rb, line 128
128: def bq_close(opts)
129: "</blockquote>\n"
130: end
# File lib/redcloth/formatters/html.rb, line 122
122: def bq_open(opts)
123: opts[:block] = true
124: cite = opts[:cite] ? " cite=\"#{ escape_attribute opts[:cite] }\"" : ''
125: "<blockquote#{cite}#{pba(opts)}>\n"
126: end
# File lib/redcloth/formatters/html.rb, line 122
122: def bq_open(opts)
123: opts[:block] = true
124: cite = opts[:cite] ? " cite=\"#{ escape_attribute opts[:cite] }\"" : ''
125: "<blockquote#{cite}#{pba(opts)}>\n"
126: end
# File lib/redcloth/formatters/html.rb, line 234
234: def br(opts)
235: if hard_breaks == false
236: "\n"
237: else
238: "<br />\n"
239: end
240: end
# File lib/redcloth/formatters/html.rb, line 234
234: def br(opts)
235: if hard_breaks == false
236: "\n"
237: else
238: "<br />\n"
239: end
240: end
# File lib/redcloth/formatters/html.rb, line 45
45: def caps(opts)
46: if no_span_caps
47: opts[:text]
48: else
49: opts[:class] = 'caps'
50: span(opts)
51: end
52: end
# File lib/redcloth/formatters/html.rb, line 45
45: def caps(opts)
46: if no_span_caps
47: opts[:text]
48: else
49: opts[:class] = 'caps'
50: span(opts)
51: end
52: end
Clean unauthorized tags.
# File lib/redcloth/formatters/html.rb, line 325
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
Clean unauthorized tags.
# File lib/redcloth/formatters/html.rb, line 325
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
# File lib/redcloth/formatters/html.rb, line 54
54: def del(opts)
55: opts[:block] = true
56: "<del#{pba(opts)}>#{opts[:text]}</del>"
57: end
# File lib/redcloth/formatters/html.rb, line 54
54: def del(opts)
55: opts[:block] = true
56: "<del#{pba(opts)}>#{opts[:text]}</del>"
57: end
# File lib/redcloth/formatters/html.rb, line 199
199: def dim(opts)
200: opts[:text].gsub!('x', '×')
201: opts[:text].gsub!("'", '′')
202: opts[:text].gsub!('"', '″')
203: opts[:text]
204: end
# File lib/redcloth/formatters/html.rb, line 199
199: def dim(opts)
200: opts[:text].gsub!('x', '×')
201: opts[:text].gsub!("'", '′')
202: opts[:text].gsub!('"', '″')
203: opts[:text]
204: end
# File lib/redcloth/formatters/html.rb, line 77
77: def dl_open(opts)
78: opts[:block] = true
79: "<dl#{pba(opts)}>\n"
80: end
# File lib/redcloth/formatters/html.rb, line 77
77: def dl_open(opts)
78: opts[:block] = true
79: "<dl#{pba(opts)}>\n"
80: end
# File lib/redcloth/formatters/html.rb, line 183
183: def ellipsis(opts)
184: "#{opts[:text]}…"
185: end
# File lib/redcloth/formatters/html.rb, line 183
183: def ellipsis(opts)
184: "#{opts[:text]}…"
185: end
# File lib/redcloth/formatters/html.rb, line 218
218: def entity(opts)
219: "&#{opts[:text]};"
220: end
# File lib/redcloth/formatters/html.rb, line 218
218: def entity(opts)
219: "&#{opts[:text]};"
220: end
# File lib/redcloth/formatters/html.rb, line 160
160: def fn(opts)
161: no = opts[:id]
162: opts[:id] = "fn#{no}"
163: opts[:class] = ["footnote", opts[:class]].compact.join(" ")
164: "<p#{pba(opts)}><sup>#{no}</sup> #{opts[:text]}</p>\n"
165: end
# File lib/redcloth/formatters/html.rb, line 160
160: def fn(opts)
161: no = opts[:id]
162: opts[:id] = "fn#{no}"
163: opts[:class] = ["footnote", opts[:class]].compact.join(" ")
164: "<p#{pba(opts)}><sup>#{no}</sup> #{opts[:text]}</p>\n"
165: end
# File lib/redcloth/formatters/html.rb, line 155
155: def footno(opts)
156: opts[:id] ||= opts[:text]
157: %Q{<sup class="footnote"><a href=\"#fn#{opts[:id]}\">#{opts[:text]}</a></sup>}
158: end
# File lib/redcloth/formatters/html.rb, line 155
155: def footno(opts)
156: opts[:id] ||= opts[:text]
157: %Q{<sup class="footnote"><a href=\"#fn#{opts[:id]}\">#{opts[:text]}</a></sup>}
158: end
# File lib/redcloth/formatters/html.rb, line 254
254: def html(opts)
255: "#{opts[:text]}\n"
256: end
# File lib/redcloth/formatters/html.rb, line 254
254: def html(opts)
255: "#{opts[:text]}\n"
256: end
# File lib/redcloth/formatters/html.rb, line 258
258: def html_block(opts)
259: inline_html(:text => "#{opts[:indent_before_start]}#{opts[:start_tag]}#{opts[:indent_after_start]}") +
260: "#{opts[:text]}" +
261: inline_html(:text => "#{opts[:indent_before_end]}#{opts[:end_tag]}#{opts[:indent_after_end]}")
262: end
# File lib/redcloth/formatters/html.rb, line 258
258: def html_block(opts)
259: inline_html(:text => "#{opts[:indent_before_start]}#{opts[:start_tag]}#{opts[:indent_after_start]}") +
260: "#{opts[:text]}" +
261: inline_html(:text => "#{opts[:indent_before_end]}#{opts[:end_tag]}#{opts[:indent_after_end]}")
262: end
# File lib/redcloth/formatters/html.rb, line 280
280: def ignored_line(opts)
281: opts[:text] + "\n"
282: end
# File lib/redcloth/formatters/html.rb, line 280
280: def ignored_line(opts)
281: opts[:text] + "\n"
282: end
# File lib/redcloth/formatters/html.rb, line 147
147: def image(opts)
148: opts.delete(:align)
149: opts[:alt] = opts[:title]
150: img = "<img src=\"#{escape_attribute opts[:src]}\"#{pba(opts)} alt=\"#{escape_attribute opts[:alt].to_s}\" />"
151: img = "<a href=\"#{escape_attribute opts[:href]}\">#{img}</a>" if opts[:href]
152: img
153: end
# File lib/redcloth/formatters/html.rb, line 147
147: def image(opts)
148: opts.delete(:align)
149: opts[:alt] = opts[:title]
150: img = "<img src=\"#{escape_attribute opts[:src]}\"#{pba(opts)} alt=\"#{escape_attribute opts[:alt].to_s}\" />"
151: img = "<a href=\"#{escape_attribute opts[:href]}\">#{img}</a>" if opts[:href]
152: img
153: end
# File lib/redcloth/formatters/html.rb, line 272
272: def inline_html(opts)
273: if filter_html
274: html_esc(opts[:text], :html_escape_preformatted)
275: else
276: "#{opts[:text]}" # nil-safe
277: end
278: end
# File lib/redcloth/formatters/html.rb, line 272
272: def inline_html(opts)
273: if filter_html
274: html_esc(opts[:text], :html_escape_preformatted)
275: else
276: "#{opts[:text]}" # nil-safe
277: end
278: end
# File lib/redcloth/formatters/html.rb, line 69
69: def li_open(opts)
70: "#{li_close unless opts.delete(:first)}#{"\t" * opts[:nest]}<li#{pba(opts)}>#{opts[:text]}"
71: end
# File lib/redcloth/formatters/html.rb, line 69
69: def li_open(opts)
70: "#{li_close unless opts.delete(:first)}#{"\t" * opts[:nest]}<li#{pba(opts)}>#{opts[:text]}"
71: end
# File lib/redcloth/formatters/html.rb, line 138
138: def link(opts)
139: if opts[:name] =~ LINK_TEXT_WITH_TITLE_RE
140: md = LINK_TEXT_WITH_TITLE_RE.match(opts[:name])
141: opts[:name] = md[1]
142: opts[:title] = md[2]
143: end
144: "<a href=\"#{escape_attribute opts[:href]}\"#{pba(opts)}>#{opts[:name]}</a>"
145: end
# File lib/redcloth/formatters/html.rb, line 138
138: def link(opts)
139: if opts[:name] =~ LINK_TEXT_WITH_TITLE_RE
140: md = LINK_TEXT_WITH_TITLE_RE.match(opts[:name])
141: opts[:name] = md[1]
142: opts[:title] = md[2]
143: end
144: "<a href=\"#{escape_attribute opts[:href]}\"#{pba(opts)}>#{opts[:name]}</a>"
145: end
# File lib/redcloth/formatters/html.rb, line 179
179: def multi_paragraph_quote(opts)
180: "“#{opts[:text]}"
181: end
# File lib/redcloth/formatters/html.rb, line 179
179: def multi_paragraph_quote(opts)
180: "“#{opts[:text]}"
181: end
# File lib/redcloth/formatters/html.rb, line 264
264: def notextile(opts)
265: if filter_html
266: html_esc(opts[:text], :html_escape_preformatted)
267: else
268: opts[:text]
269: end
270: end
# File lib/redcloth/formatters/html.rb, line 264
264: def notextile(opts)
265: if filter_html
266: html_esc(opts[:text], :html_escape_preformatted)
267: else
268: opts[:text]
269: end
270: end
# File lib/redcloth/formatters/html.rb, line 171
171: def quote1(opts)
172: "‘#{opts[:text]}’"
173: end
# File lib/redcloth/formatters/html.rb, line 171
171: def quote1(opts)
172: "‘#{opts[:text]}’"
173: end
# File lib/redcloth/formatters/html.rb, line 175
175: def quote2(opts)
176: "“#{opts[:text]}”"
177: end
# File lib/redcloth/formatters/html.rb, line 175
175: def quote2(opts)
176: "“#{opts[:text]}”"
177: end
# File lib/redcloth/formatters/html.rb, line 167
167: def snip(opts)
168: "<pre#{pba(opts)}><code>#{opts[:text]}</code></pre>\n"
169: end
# File lib/redcloth/formatters/html.rb, line 167
167: def snip(opts)
168: "<pre#{pba(opts)}><code>#{opts[:text]}</code></pre>\n"
169: end
# File lib/redcloth/formatters/html.rb, line 109
109: def table_close(opts)
110: "</table>\n"
111: end
# File lib/redcloth/formatters/html.rb, line 109
109: def table_close(opts)
110: "</table>\n"
111: end
# File lib/redcloth/formatters/html.rb, line 105
105: def table_open(opts)
106: "<table#{pba(opts)}>\n"
107: end
# File lib/redcloth/formatters/html.rb, line 105
105: def table_open(opts)
106: "<table#{pba(opts)}>\n"
107: end
# File lib/redcloth/formatters/html.rb, line 92
92: def td(opts)
93: tdtype = opts[:th] ? 'th' : 'td'
94: "\t\t<#{tdtype}#{pba(opts)}>#{opts[:text]}</#{tdtype}>\n"
95: end
# File lib/redcloth/formatters/html.rb, line 92
92: def td(opts)
93: tdtype = opts[:th] ? 'th' : 'td'
94: "\t\t<#{tdtype}#{pba(opts)}>#{opts[:text]}</#{tdtype}>\n"
95: end
# File lib/redcloth/formatters/html.rb, line 97
97: def tr_open(opts)
98: "\t<tr#{pba(opts)}>\n"
99: end
# File lib/redcloth/formatters/html.rb, line 97
97: def tr_open(opts)
98: "\t<tr#{pba(opts)}>\n"
99: end