Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 452
- Log:
Fix small comment typing error.
- Author:
- rool
- Date:
- Wed Sep 04 09:33:52 +0100 2013
- Size:
- 3257 Bytes
1 | ActiveRecord::Base.class_eval do |
2 | include ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, WhiteListHelper |
3 | def self.format_attribute(attr_name) |
4 | class << self; include ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper, WhiteListHelper; end |
5 | define_method(:body) { read_attribute attr_name } |
6 | define_method(:body_html) { read_attribute "#{attr_name}_html" } |
7 | define_method(:body_html=) { |value| write_attribute "#{attr_name}_html", value } |
8 | before_save :format_content |
9 | end |
10 | |
11 | def dom_id |
12 | [self.class.name.downcase.pluralize.dasherize, id] * '-' |
13 | end |
14 | |
15 | protected |
16 | def format_content |
17 | body.strip! if body.respond_to?(:strip!) |
18 | self.body_html = body.blank? ? '' : body_html_with_formatting |
19 | end |
20 | |
21 | # 2013-09-04 (ADH): See "body_html_with_formatting" for ddetails. |
22 | # |
23 | FOOTNOTE_NAME_REGEXP = Regexp.new('^fnr?\d+$') # E.g. "fn30" or "fnr30" |
24 | FOOTNOTE_HREF_REGEXP = Regexp.new('^#fnr?\d+$') # E.g. "#fn30" or "#fnr30" |
25 | |
26 | def body_html_with_formatting |
27 | |
28 | # 2013-09-04 (ADH): |
29 | # |
30 | # On the assumption we are called within a new Post or an edited Post, |
31 | # generate a reasonably-likely-to-be-unique ID. We can't use the model |
32 | # ID as in the "new" case it hasn't been saved yet so doesn't have one. |
33 | # |
34 | # We'll use this to patch up Textile footnote references. There's no |
35 | # way to ask Textile to add a suffix to the IDs and names it generates, |
36 | # so instead postprocess the output since we're being called for all |
37 | # generated HTML nodes by the white list engine anyway (see |
38 | # WhiteListHelper stuff in "config/environment.rb" for details). If we |
39 | # don't do this, multiple posts on a page can contain the same foonote |
40 | # IDs/names resulting in invalid HTML and useless HTML anchors. |
41 | # |
42 | # Since the ID is only used for that specific case, we're not *too* |
43 | # worried if it turns out to be non-unique, but given it's based on |
44 | # the time of day down to the microsecond and the post's user ID, it |
45 | # is *extremely* unlikely that a real user would be able to generate |
46 | # two posts with the same ID suffix for footnotes! |
47 | # |
48 | now = Time.now |
49 | fn_id_sfx = "#{now.to_i}#{now.usec}#{self.user_id || 0}" |
50 | |
51 | # Generate the body by auto-linking, running through RedCloth and then |
52 | # passing it to the white list engine which in turn calls back for all |
53 | # of the HTML nodes. |
54 | # |
55 | body_html = auto_link body { |text| truncate(text, 50) } |
56 | white_list(RedCloth.new(body_html).to_html) do | node, bad | |
57 | if WhiteListHelper.bad_tags.include?(bad) |
58 | node.to_s.gsub(/</, '<') |
59 | else |
60 | node.class == HTML::Tag && node.attributes && case(bad) |
61 | when "sup", "p" |
62 | if (node.attributes['class'] == 'footnote') |
63 | match = FOOTNOTE_NAME_REGEXP.match(node.attributes['id'] || '') |
64 | node.attributes['id'] << fn_id_sfx if (match) |
65 | end |
66 | when "a" |
67 | match = FOOTNOTE_HREF_REGEXP.match(node.attributes['href'] || '') |
68 | node.attributes['href'] << fn_id_sfx if (match) |
69 | end |
70 | |
71 | node.to_s |
72 | end |
73 | end |
74 | end |
75 | end |