public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
restructured filtered_column processor, fixed escaping issue with textile

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@1933 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Wed Aug 30 07:25:50 -0700 2006
commit  f2b387147adc5f3ed1e25b7dc3ce717836ecd27f
tree    bdcbf13e9a810115f7dc1d68776971197a1a2129
parent  26a6e599ae65496488e71e5edf987be318fc4df8
...
2
3
4
 
 
 
5
6
7
...
2
3
4
5
6
7
8
9
10
0
@@ -2,6 +2,9 @@ module FilteredColumn
0
   module Filters
0
     class Base
0
       class << self
0
+ def filter(text) text end
0
+ def escape(text) text end
0
+
0
         def filter_name
0
           set_name self.name.demodulize.gsub(/Filter$/, '')
0
         end
...
4
5
6
 
 
 
 
7
8
9
10
...
4
5
6
7
8
9
10
11
12
13
14
0
@@ -4,6 +4,10 @@ module FilteredColumn
0
       def self.filter(text)
0
         Object.const_defined?("RedCloth") ? RedCloth.new(text).to_html : text
0
       end
0
+
0
+ def self.escape(text)
0
+ %(<notextile>#{text}</notextile>)
0
+ end
0
     end
0
   end
0
 end
0
\ No newline at end of file
...
9
10
11
12
13
14
15
16
17
 
 
 
 
 
 
 
 
18
19
20
21
22
23
24
25
26
 
 
 
 
 
 
 
27
28
29
 
30
31
32
33
34
 
 
 
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 
 
 
 
 
49
50
 
 
51
52
53
...
9
10
11
 
 
 
 
 
 
12
13
14
15
16
17
18
19
20
 
 
 
 
 
 
 
 
21
22
23
24
25
26
27
28
29
 
30
31
32
33
34
 
35
36
37
38
39
40
 
41
42
 
 
 
 
 
 
 
 
43
44
45
46
47
48
 
49
50
51
52
53
0
@@ -9,44 +9,44 @@ module FilteredColumn
0
       /<(filter|macro|typo):([_a-zA-Z0-9]+)([^>]*)>(.*?)<\/(filter|macro):([_a-zA-Z0-9]+)>/m
0
       ].freeze
0
 
0
- class << self
0
- def process_filter(filter_name, text)
0
- return '' if text.blank?
0
- process_macros(text) if FilteredColumn.macros.any?
0
- filter_text filter_name, text
0
- end
0
+ def self.process_filter(filter_name, text)
0
+ new(filter_name, text).filter
0
+ end
0
+
0
+ def initialize(filter_name, text)
0
+ @filter = FilteredColumn.filters[filter_name.to_sym] rescue nil
0
+ @text = text
0
+ end
0
 
0
- def filter_text(filter_name, text_to_filter)
0
- return text_to_filter if filter_name.blank?
0
- filter_class = FilteredColumn.filters[filter_name.to_sym]
0
- return text_to_filter if filter_class.nil?
0
- filter_class.filter text_to_filter
0
- end
0
-
0
- def process_macros(text_to_filter)
0
+ def filter
0
+ process_macros
0
+ @filter ? @filter.filter(@text) : @text
0
+ end
0
+
0
+ protected
0
+ def process_macros
0
         #RAILS_DEFAULT_LOGGER.warn "PROCESSING MACROS: #{::FilteredColumn.macros.keys.inspect}"
0
         @@patterns.each do |pattern|
0
- text_to_filter.gsub!(pattern) do |match|
0
+ @text.gsub!(pattern) do |match|
0
             #RAILS_DEFAULT_LOGGER.warn "our match: #{$2}"
0
             key = "#{$2}_macro".to_sym
0
             if !$2.blank? && FilteredColumn.macros.has_key?(key)
0
               #RAILS_DEFAULT_LOGGER.warn "It has the key!"
0
- FilteredColumn.macros[key].filter(hash_from_attributes($3), $4.to_s)
0
+ macro = FilteredColumn.macros[key]
0
+ macro_text = macro ? macro.filter(hash_from_attributes($3), $4.to_s) : $4.to_s
0
+ @filter ? @filter.escape(macro_text) : macro_text
0
             end
0
           end
0
         end
0
- text_to_filter
0
       end
0
 
0
- protected
0
- def hash_from_attributes(string)
0
- attributes = {}
0
- string.gsub(/([^ =]+="[^"]*")/) do |match|
0
- key, value = match.split(/=/, 2)
0
- attributes[key] = value.gsub(/"/, '')
0
- end
0
- attributes.symbolize_keys!
0
+ def hash_from_attributes(string)
0
+ attributes = {}
0
+ string.gsub(/([^ =]+="[^"]*")/) do |match|
0
+ key, value = match.split(/=/, 2)
0
+ attributes[key] = value.gsub(/"/, '')
0
         end
0
- end
0
+ attributes.symbolize_keys!
0
+ end
0
   end
0
 end
0
\ No newline at end of file
...
9
10
11
12
 
13
14
15
16
17
 
 
 
 
 
 
 
 
18
19
20
...
25
26
27
28
 
29
30
31
32
33
 
 
 
34
35
 
36
37
38
...
9
10
11
 
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
...
33
34
35
 
36
37
38
 
 
 
39
40
41
42
 
43
44
45
46
0
@@ -9,12 +9,20 @@ Test::Unit::TestCase.class_eval do
0
     FilteredColumn::Processor.called_filters = []
0
     filtered = yield
0
     filtered.save if filtered
0
- assert_equal filters.length, (FilteredColumn::Processor.called_filters & filters.collect(&:to_s)).length, "#{filters.join(', ')} expected, #{FilteredColumn::Processor.called_filters.join(', ')} called"
0
+ assert_equal filters.length, (FilteredColumn::Processor.called_filters & filters).length, "#{filters.map(&:inspect).join(', ')} expected, #{FilteredColumn::Processor.called_filters.map(&:inspect).join(', ')} called"
0
   end
0
 
0
   def assert_no_filters_called_on(klass, &block)
0
     assert_filters_called_on &block
0
   end
0
+
0
+ def process_filter(filter, text)
0
+ FilteredColumn::Processor.new(filter, text).filter
0
+ end
0
+
0
+ def process_macros(text)
0
+ process_filter nil, text
0
+ end
0
 end
0
 
0
 class SampleMacro < FilteredColumn::Macros::Base
0
@@ -25,14 +33,14 @@ end
0
 
0
 FilteredColumn.macros[:sample_macro] = SampleMacro
0
 
0
-class << FilteredColumn::Processor
0
+FilteredColumn::Processor.class_eval do
0
   @@called_filters = []
0
   cattr_accessor :called_filters
0
- def filter_text_with_audit(filter_name, text_to_filter)
0
- (called_filters << filter_name).uniq!
0
- filter_text_without_audit(filter_name, text_to_filter)
0
+ def filter_with_audit
0
+ (called_filters << @filter.filter_key).uniq! if @filter
0
+ filter_without_audit
0
   end
0
- alias_method_chain :filter_text, :audit
0
+ alias_method_chain :filter, :audit
0
 end
0
 
0
 class Article < ActiveRecord::Base
...
6
7
8
9
 
10
11
12
...
40
41
42
 
 
 
 
43
...
6
7
8
 
9
10
11
12
...
40
41
42
43
44
45
46
47
0
@@ -6,7 +6,7 @@ class FilteredColumnTest < Test::Unit::TestCase
0
     :markdown => { :input => "# bar\n\nfoo", :output => "<h1>bar</h1>\n\n<p>foo</p>" }
0
   }.each do |filter_name, values|
0
     define_method "test_should_filter_with_#{filter_name}" do
0
- assert_equal values[:output], FilteredColumn::Processor.filter_text("#{filter_name}_filter", values[:input])
0
+ assert_equal values[:output], FilteredColumn::Processor.new("#{filter_name}_filter", values[:input]).filter
0
     end
0
 
0
     define_method "test_should_filter_model_attribute_with_#{filter_name}" do
0
@@ -40,4 +40,8 @@ class FilteredColumnTest < Test::Unit::TestCase
0
   def test_should_call_no_filters_with_no_data
0
     assert_no_filters_called_on(Article) { Article.new }
0
   end
0
+
0
+ def test_should_escape_textile_filter
0
+ assert_equal '<notextile>foo</notextile>', FilteredColumn::Filters::TextileFilter.escape('foo')
0
+ end
0
 end
...
16
17
18
19
20
21
22
23
 
 
 
 
24
25
...
16
17
18
 
 
 
 
 
19
20
21
22
23
24
0
@@ -16,9 +16,8 @@ class MacroFilterTest < Test::Unit::TestCase
0
   def test_sample_macro_with_underscored_attributes
0
     assert_equal %(foo: foo - flip: bar - text: test), process_macros(%(<macro:sample foo_bar="foo" flip="bar">test</macro:sample>))
0
   end
0
-
0
- private
0
- def process_macros(text)
0
- FilteredColumn::Processor.process_macros(text)
0
- end
0
+
0
+ def test_should_escape_macros_with_textile
0
+ assert_equal %(foo: - flip: - text: <tt>test</tt>), process_filter(:textile_filter, "<macro:sample><tt>test</tt></macro:sample>")
0
+ end
0
 end
0
\ No newline at end of file
...
50
51
52
53
 
54
55
56
...
50
51
52
 
53
54
55
56
0
@@ -50,6 +50,6 @@ class CodeMacroTest < Test::Unit::TestCase
0
   
0
   private
0
     def process_macros(text)
0
- FilteredColumn::Processor.process_macros(text)
0
+ FilteredColumn::Processor.new(nil, text).filter
0
     end
0
 end
0
\ No newline at end of file
...
13
14
15
16
 
17
18
19
...
13
14
15
 
16
17
18
19
0
@@ -13,6 +13,6 @@ class FlickrMacroTest < Test::Unit::TestCase
0
   
0
   private
0
     def process_macros(text)
0
- FilteredColumn::Processor.process_macros(text)
0
+ FilteredColumn::Processor.new(nil, text).filter
0
     end
0
 end
0
\ No newline at end of file

Comments

    No one has commented yet.