public
Fork of halorgium/mephisto
Description: A refactored Mephisto that has multiple spam detection engines.
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/francois/mephisto.git
allow space AND comma delimited tags

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@3055 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Fri Nov 23 21:00:41 -0800 2007
commit  f1f452cedb159e01353c5da6dcd2015d973a6189
tree    ca50ea5db121657d700cd09c69e028c15fa1490f
parent  dd9dab0acec75dcdbaad92a5340b435f7c96464f
...
1
2
3
4
5
...
7
8
9
10
11
12
13
14
15
16
 
17
18
19
 
 
 
 
 
 
20
21
22
23
24
25
26
 
27
28
29
...
44
45
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
48
49
...
1
 
2
3
4
...
6
7
8
 
 
 
 
 
 
 
9
10
11
12
13
14
15
16
17
18
19
20
 
 
 
 
 
21
22
23
24
...
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
0
@@ -1,5 +1,4 @@
0
 class Tag < ActiveRecord::Base
0
- @@tag_parse_regex = /((?: |)['"]{0,1})['"]?\s*(.*?)\s*(?:[,'"]|$)(?:\1(?: |$))/
0
   has_many :taggings
0
 
0
   class << self
0
@@ -7,23 +6,19 @@ class Tag < ActiveRecord::Base
0
       find_by_name(tag.to_s)
0
     end
0
 
0
- # parses a comma separated list of tags into tag names
0
- # handles all kinds of different tags. (comma seperated, space seperated (in quotation marks))
0
- # should handle most the common keyword formats.
0
- #
0
- # e.g.: b'log, emacs fun, rails, ruby => "b'log", "emacs fun", "rails", "ruby"
0
- # "b'log" "emacs fun" "rails" "ruby" => "b'log", "emacs fun", "rails", "ruby"
0
- # 'b\'log' 'emacs fun' 'rails' 'ruby' => "b'log", "emacs fun", "rails", "ruby"
0
+ # parses a list of tags into tag names
0
     #
0
     # Tag.parse('a, b, c')
0
     # # => ['a', 'b', 'c']
0
+ #
0
+ # Tag.parse("a b c")
0
+ # # => ['a', 'b', 'c']
0
+ #
0
+ # Tag.parse(%(a "b c"))
0
+ # # => ['a', 'b c']
0
     def parse(list)
0
       return list if list.is_a?(Array)
0
- returning list.scan(@@tag_parse_regex) do |tags|
0
- tags.collect! { |t| t.last.strip!; t.last }
0
- tags.uniq!
0
- tags.delete_if &:blank?
0
- end
0
+ list.include?(',') ? parse_with_commas(list) : parse_with_spaces(list)
0
     end
0
 
0
     # Parses comma separated tag list and returns tags for them.
0
@@ -44,6 +39,38 @@ class Tag < ActiveRecord::Base
0
         found_tags + (tag_names - found_tags.collect(&:name)).collect { |s| create!(:name => s) }
0
       end
0
     end
0
+
0
+ private
0
+ def parse_with_commas(list)
0
+ cleanup_tags(list.split(','))
0
+ end
0
+
0
+ def parse_with_spaces(list)
0
+ tags = []
0
+
0
+ # first, pull out the quoted tags
0
+ list.gsub!(/\"(.*?)\"\s*/ ) { tags << $1; "" }
0
+
0
+ # then, get whatever's left
0
+ tags.concat list.split(/\s/)
0
+
0
+ cleanup_tags(tags)
0
+ end
0
+
0
+ def cleanup_tags(tags)
0
+ tags.tap do |t|
0
+ t.collect! do |tag|
0
+ unless tag.blank?
0
+ tag.downcase!
0
+ tag.gsub!(/:/, '')
0
+ tag.strip!
0
+ tag
0
+ end
0
+ end
0
+ t.compact!
0
+ t.uniq!
0
+ end
0
+ end
0
   end
0
 
0
   def ==(comparison_object)

Comments

    No one has commented yet.