filters 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.rdoc ADDED
@@ -0,0 +1,4 @@
1
+
2
+ === 1.0.0 / 2009-10-08
3
+
4
+ * Initial release
data/Manifest ADDED
@@ -0,0 +1,24 @@
1
+ History.rdoc
2
+ Manifest
3
+ README.rdoc
4
+ Rakefile
5
+ filters.gemspec
6
+ lib/filters.rb
7
+ lib/filters/all.rb
8
+ lib/filters/email.rb
9
+ lib/filters/filter.rb
10
+ lib/filters/markup.rb
11
+ lib/filters/phrase.rb
12
+ lib/filters/profanity.rb
13
+ lib/filters/uri.rb
14
+ lib/filters/version.rb
15
+ spec/filter_email_spec.rb
16
+ spec/filter_markup_spec.rb
17
+ spec/filter_phrase_spec.rb
18
+ spec/filter_profanity_spec.rb
19
+ spec/filter_uri_spec.rb
20
+ spec/spec.opts
21
+ spec/spec_helper.rb
22
+ tasks/docs.rake
23
+ tasks/gemspec.rake
24
+ tasks/spec.rake
data/README.rdoc ADDED
@@ -0,0 +1,109 @@
1
+
2
+ = Filters
3
+
4
+ Ruby text filters consisting of the following core filters:
5
+
6
+ * phrase
7
+ * profanity (uses phrase)
8
+ * email (converts emails to mailto: anchor tags)
9
+ * uri (converts uris to anchor tags)
10
+ * markup (mini-markup language)
11
+
12
+ == Filter::Phrase
13
+
14
+ Filter::Phrase('bad word!', :phrases => ['bad'])
15
+ # => 'word!'
16
+
17
+ Filter::Phrase('bad word!', :phrases => ['bad'], :mask => '*')
18
+ # => '*** word!'
19
+
20
+ Filter::Phrase('bad word!', :phrases => ['bad'], :mask => '[removed]')
21
+ # => '[removed] word!'
22
+
23
+ Filter::Phrase('foobar', :phrases => ['foo(bar)?'])
24
+ # => ''
25
+
26
+ Filter::Phrase('foobar', :phrases => [/foo(bar)?/])
27
+ # => ''
28
+
29
+ Filter::Phrase('some foo bar', :phrases => [['foo', '*'], ['bar', '[removed]']])
30
+ # => 'some *** [removed]'
31
+
32
+ == Filter::Profanity < Filter::Phrase
33
+
34
+ Filter::Profanity('fuck this shit is so awesome i cant fucking believe it')
35
+ # => 'this is so awesome i cant believe it'
36
+
37
+ Filter::Profanity('fuck this shit is so awesome i cant fucking believe it', :mask => '*')
38
+ # => '**** this **** is so awesome i cant ******* believe it'
39
+
40
+ == Filter::URI
41
+
42
+ Filter::URI('hey checkout http://vision-media.ca sometime')
43
+ # => 'hey checkout <a href="http://vision-media.ca">sometime</a>'
44
+
45
+ == Filter::Email
46
+
47
+ Filter::Email('hey email me@foo.com sometime')
48
+ # => 'hey email <a href="mailto:me@foo.com">sometime</a> sometime'
49
+
50
+ == Filter::Markup
51
+
52
+ Filter::Markup <<-EOF
53
+ = Welcome
54
+
55
+ == Article
56
+
57
+ This article is meant to show how a simple *markup* language
58
+ can be generated using the filter gem.
59
+
60
+ === Source
61
+
62
+ @@@ javascript
63
+ foo = function(){
64
+ bar()
65
+ }
66
+ @@@
67
+ EOF
68
+
69
+ Would output:
70
+
71
+ <h1>Welcome</h1>
72
+
73
+ <h2>Article</h2>
74
+
75
+ <p> This article is meant to show how a simple <strong>markup</strong> language
76
+ can be generated using the filter gem. </p>
77
+
78
+ <h3>Source</h3>
79
+
80
+ <code class="javascript">
81
+ foo = function(){
82
+ bar()
83
+ }
84
+ </code>
85
+
86
+ == License:
87
+
88
+ (The MIT License)
89
+
90
+ Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
91
+
92
+ Permission is hereby granted, free of charge, to any person obtaining
93
+ a copy of this software and associated documentation files (the
94
+ 'Software'), to deal in the Software without restriction, including
95
+ without limitation the rights to use, copy, modify, merge, publish,
96
+ distribute, sublicense, an d/or sell copies of the Software, and to
97
+ permit persons to whom the Software is furnished to do so, subject to
98
+ the following conditions:
99
+
100
+ The above copyright notice and this permission notice shall be
101
+ included in all copies or substantial portions of the Software.
102
+
103
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
104
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
105
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
106
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
107
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
108
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
109
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+
2
+ $:.unshift 'lib'
3
+ require 'filters'
4
+ require 'rubygems'
5
+ require 'rake'
6
+ require 'echoe'
7
+
8
+ Echoe.new "filters", Filter::VERSION do |p|
9
+ p.author = "TJ Holowaychuk"
10
+ p.email = "tj@vision-media.ca"
11
+ p.summary = "Text filters including phrases, profanity, mini-markdown, uri, email, etc"
12
+ p.url = "http://github.com/visionmedia/filters"
13
+ p.runtime_dependencies = []
14
+ end
15
+
16
+ Dir['tasks/**/*.rake'].sort.each { |f| load f }
data/filters.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{filters}
5
+ s.version = "1.0.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["TJ Holowaychuk"]
9
+ s.date = %q{2009-10-10}
10
+ s.description = %q{Text filters including phrases, profanity, mini-markdown, uri, email, etc}
11
+ s.email = %q{tj@vision-media.ca}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/filters.rb", "lib/filters/all.rb", "lib/filters/email.rb", "lib/filters/filter.rb", "lib/filters/markup.rb", "lib/filters/phrase.rb", "lib/filters/profanity.rb", "lib/filters/uri.rb", "lib/filters/version.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
13
+ s.files = ["History.rdoc", "Manifest", "README.rdoc", "Rakefile", "filters.gemspec", "lib/filters.rb", "lib/filters/all.rb", "lib/filters/email.rb", "lib/filters/filter.rb", "lib/filters/markup.rb", "lib/filters/phrase.rb", "lib/filters/profanity.rb", "lib/filters/uri.rb", "lib/filters/version.rb", "spec/filter_email_spec.rb", "spec/filter_markup_spec.rb", "spec/filter_phrase_spec.rb", "spec/filter_profanity_spec.rb", "spec/filter_uri_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "tasks/docs.rake", "tasks/gemspec.rake", "tasks/spec.rake"]
14
+ s.homepage = %q{http://github.com/visionmedia/filters}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Filters", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{filters}
18
+ s.rubygems_version = %q{1.3.5}
19
+ s.summary = %q{Text filters including phrases, profanity, mini-markdown, uri, email, etc}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
26
+ else
27
+ end
28
+ else
29
+ end
30
+ end
data/lib/filters.rb ADDED
@@ -0,0 +1,25 @@
1
+ #--
2
+ # Copyright (c) 2009 TJ Holowaychuk <tj@vision-media.ca>
3
+ #
4
+ # Permission is hereby granted, free of charge, to any person obtaining
5
+ # a copy of this software and associated documentation files (the
6
+ # "Software"), to deal in the Software without restriction, including
7
+ # without limitation the rights to use, copy, modify, merge, publish,
8
+ # distribute, sublicense, and/or sell copies of the Software, and to
9
+ # permit persons to whom the Software is furnished to do so, subject to
10
+ # the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be
13
+ # included in all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
+ # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
+ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
+ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ #++
23
+
24
+ require 'filters/version'
25
+ require 'filters/filter'
@@ -0,0 +1,6 @@
1
+
2
+ require 'filters/phrase'
3
+ require 'filters/profanity'
4
+ require 'filters/markup'
5
+ require 'filters/email'
6
+ require 'filters/uri'
@@ -0,0 +1,20 @@
1
+
2
+ class Filter
3
+ class Email < self
4
+
5
+ #--
6
+ # Constants
7
+ #++
8
+
9
+ EMAIL = /[\w\-\.]+@(?:(?:[\w\-]+\.)+)[a-z]{2,4}\b/
10
+
11
+ ##
12
+ # Convert emails to anchor tags.
13
+
14
+ def filter!
15
+ string.gsub! EMAIL, '<a href="mailto:\0">\0</a>'
16
+ string
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,26 @@
1
+
2
+ class Filter
3
+
4
+ ##
5
+ # Input string.
6
+
7
+ attr_reader :string
8
+
9
+ ##
10
+ # Initialize with _string_ and _options_.
11
+
12
+ def initialize string, options = {}
13
+ @string, @options = string, options
14
+ end
15
+
16
+ ##
17
+ # Auto generate Filter::Subclass() methods which
18
+ # act as a shortcut to Filter::Subclass.new(...).filter!
19
+
20
+ def self.inherited subclass
21
+ (class << self; self end).send :define_method, subclass.name.split('::').last do |*args|
22
+ subclass.new(*args).filter!
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,26 @@
1
+
2
+ class Filter
3
+ class Markup < self
4
+
5
+ ##
6
+ # Filter mini-markup language.
7
+
8
+ def filter!
9
+ string.gsub! /\*([^*]+)\*/, '<strong>\1</strong>'
10
+ string.gsub! /\/([^*]+)\//, '<em>\1</em>'
11
+ string.gsub! /_([^*]+)_/, '<sub>\1</sub>'
12
+ string.gsub! /-([^*]+)-/, '<sup>\1</sup>'
13
+ string.gsub! /['"](.*?)['"]:([\w\-\.]+@\S+)/, '<a href="mailto:\2">\1</a>'
14
+ string.gsub! /['"](.*?)['"]:(\S+)/, '<a href="\2">\1</a>'
15
+ string.gsub! /@@@\s*\n(.*?)@@@/m, "<pre>\n\\1</pre>"
16
+ string.gsub! /@@@\s*(\w+)(.*?)@@@/m, '<code class="\1">\2</code>'
17
+ string.gsub! /h(\d)\s*([^\n]+)/, '<h\1>\2</h\1>'
18
+ string.gsub! /^\s*(={1,5})\s*([^\n]+)/ do |heading|
19
+ bar, contents = heading.split
20
+ "<h#{bar.length}>#{contents}</h#{bar.length}>"
21
+ end
22
+ string
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+
2
+ class Filter
3
+ class Phrase < self
4
+
5
+ ##
6
+ # Array of phrases.
7
+
8
+ attr_reader :phrases
9
+
10
+ ##
11
+ # Mask string.
12
+
13
+ attr_reader :mask
14
+
15
+ ##
16
+ # Initialize with _string_ and _options_.
17
+
18
+ def initialize string, options = {}
19
+ super
20
+ @phrases = options.fetch :phrases, []
21
+ @mask = options.fetch :mask, nil
22
+ end
23
+
24
+ ##
25
+ # Filter phrases.
26
+
27
+ def filter!
28
+ phrases.each do |phrase|
29
+ if phrase.is_a? Array
30
+ filter_with phrase.shift, phrase.shift
31
+ else
32
+ filter_with phrase, mask
33
+ end
34
+ end
35
+ string.strip
36
+ end
37
+
38
+ private
39
+
40
+ def filter_with phrase, mask = nil
41
+ if mask && mask.length == 1
42
+ string.gsub! /\b#{phrase}(\s+|$)/, mask * phrase.to_s.length + '\1'
43
+ elsif mask
44
+ string.gsub! /\b#{phrase}(\s+|$)/, mask + '\1'
45
+ else
46
+ string.gsub! /\b#{phrase}(?:\s+|$)/, ''
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,306 @@
1
+
2
+ require 'filters/phrase'
3
+
4
+ class Filter
5
+ class Profanity < Phrase
6
+
7
+ #--
8
+ # Constants
9
+ #++
10
+
11
+ PHRASES = [
12
+ :'ass',
13
+ :'ass lick',
14
+ :'asses',
15
+ :'asshole',
16
+ :'assholes',
17
+ :'asskisser',
18
+ :'asswipe',
19
+ :'balls',
20
+ :'bastard',
21
+ :'beastial',
22
+ :'beastiality',
23
+ :'beastility',
24
+ :'beaver',
25
+ :'belly whacker',
26
+ :'bestial',
27
+ :'bestiality',
28
+ :'bitch',
29
+ :'bitcher',
30
+ :'bitchers',
31
+ :'bitches',
32
+ :'bitchin',
33
+ :'bitching',
34
+ :'blow job',
35
+ :'blowjob',
36
+ :'blowjobs',
37
+ :'bonehead',
38
+ :'boner',
39
+ :'brown eye',
40
+ :'browneye',
41
+ :'browntown',
42
+ :'bucket cunt',
43
+ :'bull shit',
44
+ :'bullshit',
45
+ :'bum',
46
+ :'bung hole',
47
+ :'butch',
48
+ :'butt',
49
+ :'butt breath',
50
+ :'butt fucker',
51
+ :'butt hair',
52
+ :'buttface',
53
+ :'buttfuck',
54
+ :'buttfucker',
55
+ :'butthead',
56
+ :'butthole',
57
+ :'buttpicker',
58
+ :'chink',
59
+ :'christ',
60
+ :'circle jerk',
61
+ :'clam',
62
+ :'clit',
63
+ :'cobia',
64
+ :'cock',
65
+ :'cocks',
66
+ :'cocksuck',
67
+ :'cocksucked',
68
+ :'cocksucker',
69
+ :'cocksucking',
70
+ :'cocksucks',
71
+ :'cooter',
72
+ :'crap',
73
+ :'cum',
74
+ :'cummer',
75
+ :'cumming',
76
+ :'cums',
77
+ :'cumshot',
78
+ :'cunilingus',
79
+ :'cunillingus',
80
+ :'cunnilingus',
81
+ :'cunt',
82
+ :'cuntlick',
83
+ :'cuntlicker',
84
+ :'cuntlicking',
85
+ :'cunts',
86
+ :'cyberfuc',
87
+ :'cyberfuck',
88
+ :'cyberfucked',
89
+ :'cyberfucker',
90
+ :'cyberfuckers',
91
+ :'cyberfucking',
92
+ :'damn',
93
+ :'dick',
94
+ :'dike',
95
+ :'dildo',
96
+ :'dildos',
97
+ :'dink',
98
+ :'dinks',
99
+ :'dipshit',
100
+ :'dong',
101
+ :'douche bag',
102
+ :'dumbass',
103
+ :'dyke',
104
+ :'ejaculate',
105
+ :'ejaculated',
106
+ :'ejaculates',
107
+ :'ejaculating',
108
+ :'ejaculatings',
109
+ :'ejaculation',
110
+ :'fag',
111
+ :'fagget',
112
+ :'fagging',
113
+ :'faggit',
114
+ :'faggot',
115
+ :'faggs',
116
+ :'fagot',
117
+ :'fagots',
118
+ :'fags',
119
+ :'fart',
120
+ :'farted',
121
+ :'farting',
122
+ :'fartings',
123
+ :'farts',
124
+ :'farty',
125
+ :'fatass',
126
+ :'fatso',
127
+ :'felatio',
128
+ :'fellatio',
129
+ :'fingerfuck',
130
+ :'fingerfucked',
131
+ :'fingerfucker',
132
+ :'fingerfuckers',
133
+ :'fingerfucking',
134
+ :'fingerfucks',
135
+ :'fistfuck',
136
+ :'fistfucked',
137
+ :'fistfucker',
138
+ :'fistfuckers',
139
+ :'fistfucking',
140
+ :'fistfuckings',
141
+ :'fistfucks',
142
+ :'fuck',
143
+ :'fucked',
144
+ :'fucker',
145
+ :'fuckers',
146
+ :'fuckin',
147
+ :'fucking',
148
+ :'fuckings',
149
+ :'fuckme',
150
+ :'fucks',
151
+ :'fuk',
152
+ :'fuks',
153
+ :'furburger',
154
+ :'gangbang',
155
+ :'gangbanged',
156
+ :'gangbangs',
157
+ :'gaysex',
158
+ :'gazongers',
159
+ :'goddamn',
160
+ :'gonads',
161
+ :'gook',
162
+ :'guinne',
163
+ :'hard on',
164
+ :'hardcoresex',
165
+ :'hell',
166
+ :'homo',
167
+ :'hooker',
168
+ :'horniest',
169
+ :'horny',
170
+ :'hotsex',
171
+ :'hussy',
172
+ :'jack off',
173
+ :'jackass',
174
+ :'jacking off',
175
+ :'jackoff',
176
+ :'jack-off',
177
+ :'jap',
178
+ :'jerk',
179
+ :'jerk-off',
180
+ :'jesus',
181
+ :'jesus christ',
182
+ :'jew',
183
+ :'jism',
184
+ :'jiz',
185
+ :'jizm',
186
+ :'jizz',
187
+ :'kike',
188
+ :'knob',
189
+ :'kock',
190
+ :'kondum',
191
+ :'kondums',
192
+ :'kraut',
193
+ :'kum',
194
+ :'kummer',
195
+ :'kumming',
196
+ :'kums',
197
+ :'kunilingus',
198
+ :'lesbian',
199
+ :'lesbo',
200
+ :'loser',
201
+ :'lust',
202
+ :'lusting',
203
+ :'merde',
204
+ :'mick',
205
+ :'mothafuck',
206
+ :'mothafucka',
207
+ :'mothafuckas',
208
+ :'mothafuckaz',
209
+ :'mothafucked',
210
+ :'mothafucker',
211
+ :'mothafuckers',
212
+ :'mothafuckin',
213
+ :'mothafucking',
214
+ :'mothafuckings',
215
+ :'mothafucks',
216
+ :'motherfuck',
217
+ :'motherfucked',
218
+ :'motherfucker',
219
+ :'motherfuckers',
220
+ :'motherfuckin',
221
+ :'motherfucking',
222
+ :'motherfuckings',
223
+ :'motherfucks',
224
+ :'mound',
225
+ :'muff',
226
+ :'nerd',
227
+ :'nigger',
228
+ :'niggers',
229
+ :'orgasim',
230
+ :'orgasims',
231
+ :'orgasm',
232
+ :'orgasms',
233
+ :'pecker',
234
+ :'penis',
235
+ :'phonesex',
236
+ :'phuk',
237
+ :'phuked',
238
+ :'phuking',
239
+ :'phukked',
240
+ :'phukking',
241
+ :'phuks',
242
+ :'phuq',
243
+ :'pimp',
244
+ :'piss',
245
+ :'pissed',
246
+ :'pisser',
247
+ :'pissers',
248
+ :'pisses',
249
+ :'pissin',
250
+ :'pissing',
251
+ :'pissoff',
252
+ :'porn',
253
+ :'porno',
254
+ :'pornography',
255
+ :'pornos',
256
+ :'prick',
257
+ :'pricks',
258
+ :'prostitute',
259
+ :'punk',
260
+ :'pussies',
261
+ :'pussy',
262
+ :'pussys',
263
+ :'queer',
264
+ :'retard',
265
+ :'schlong',
266
+ :'screw',
267
+ :'sheister',
268
+ :'shit',
269
+ :'shited',
270
+ :'shitfull',
271
+ :'shiting',
272
+ :'shitings',
273
+ :'shits',
274
+ :'shitted',
275
+ :'shitter',
276
+ :'shitters',
277
+ :'shitting',
278
+ :'shittings',
279
+ :'shitty',
280
+ :'slag',
281
+ :'sleaze',
282
+ :'slut',
283
+ :'sluts',
284
+ :'smut',
285
+ :'snatch',
286
+ :'spunk',
287
+ :'twat',
288
+ :'wetback',
289
+ :'whore',
290
+ :'wop'
291
+ ]
292
+
293
+ ##
294
+ # Initialize with _string_ and _options_.
295
+
296
+ def initialize string, options = {}
297
+ super
298
+ @phrases |= PHRASES
299
+ end
300
+
301
+ end
302
+
303
+ def self.Profanity *args
304
+ Profanity.new(*args).filter!
305
+ end
306
+ end
@@ -0,0 +1,33 @@
1
+
2
+ class Filter
3
+ class URI < self
4
+
5
+ ##
6
+ # Weither or not to open in a new window.
7
+
8
+ attr_reader :new_window
9
+
10
+ ##
11
+ # Initialize with _string_ and _options_.
12
+
13
+ def initialize string, options = {}
14
+ super
15
+ @new_window = options.fetch :new_window, false
16
+ end
17
+
18
+ ##
19
+ # Convert URIs to anchor tags.
20
+
21
+ def filter!
22
+ string.gsub! /\w+:\/\/(.*?)(\s+|$)/ do |uri|
23
+ if new_window
24
+ %(<a href="#{uri.strip}" target="_blank">#{uri}</a>)
25
+ else
26
+ %(<a href="#{uri.strip}">#{uri}</a>)
27
+ end
28
+ end
29
+ string
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+
2
+ class Filter
3
+ VERSION = '1.0.0'
4
+ end
@@ -0,0 +1,17 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+ require 'filters/email'
4
+
5
+ describe Filter::Email do
6
+ it "should convert URIs to clickable anchor tags" do
7
+ string = 'my email is tj@vision-media.ca'
8
+ Filter::Email(string).
9
+ should == 'my email is <a href="mailto:tj@vision-media.ca">tj@vision-media.ca</a>'
10
+ end
11
+
12
+ it "should retain whitespace" do
13
+ string = 'email tj@vision-media.ca for awesome web-dev'
14
+ Filter::Email(string).
15
+ should == 'email <a href="mailto:tj@vision-media.ca">tj@vision-media.ca</a> for awesome web-dev'
16
+ end
17
+ end
@@ -0,0 +1,98 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+ require 'filters/markup'
4
+
5
+ describe Filter::Markup do
6
+ it "should treat *STR* as strong" do
7
+ Filter::Markup('this *is* sweet').should == 'this <strong>is</strong> sweet'
8
+ Filter::Markup('this *is sweet').should == 'this *is sweet'
9
+ end
10
+
11
+ it "should treat /STR/ as em" do
12
+ Filter::Markup('this /is/ sweet').should == 'this <em>is</em> sweet'
13
+ Filter::Markup('this /is sweet').should == 'this /is sweet'
14
+ end
15
+
16
+ it "should treat _STR_ as sub" do
17
+ Filter::Markup('this _is_ sweet').should == 'this <sub>is</sub> sweet'
18
+ Filter::Markup('this _is sweet').should == 'this _is sweet'
19
+ end
20
+
21
+ it "should treat -STR- as sup" do
22
+ Filter::Markup('this -is- sweet').should == 'this <sup>is</sup> sweet'
23
+ Filter::Markup('this -is sweet').should == 'this -is sweet'
24
+ end
25
+
26
+ it "should treat 'STR':URI as an anchor tag" do
27
+ Filter::Markup('visit "Vision Media":http://vision-media.ca').should == 'visit <a href="http://vision-media.ca">Vision Media</a>'
28
+ Filter::Markup('visit \'Vision Media\':http://vision-media.ca').should == 'visit <a href="http://vision-media.ca">Vision Media</a>'
29
+ end
30
+
31
+ it "should treat 'STR':EMAIL as a mailto anchor tag" do
32
+ Filter::Markup('visit "Vision Media":tj@vision-media.ca').should == 'visit <a href="mailto:tj@vision-media.ca">Vision Media</a>'
33
+ Filter::Markup('visit \'Vision Media\':tj@vision-media.ca').should == 'visit <a href="mailto:tj@vision-media.ca">Vision Media</a>'
34
+ end
35
+
36
+ it "should treat @@@ LANG as code" do
37
+ string = <<-EOF
38
+ @@@ javascript
39
+ foo = function(){
40
+ bar()
41
+ }
42
+ @@@
43
+ EOF
44
+
45
+ expected = <<-EOF
46
+ <code class="javascript">
47
+ foo = function(){
48
+ bar()
49
+ }
50
+ </code>
51
+ EOF
52
+
53
+ Filter::Markup(string).should == expected
54
+ end
55
+
56
+ it "should treat @@@ as pre" do
57
+ string = <<-EOF
58
+ @@@
59
+ foo = function(){
60
+ bar()
61
+ }
62
+ @@@
63
+ EOF
64
+
65
+ expected = <<-EOF
66
+ <pre>
67
+ foo = function(){
68
+ bar()
69
+ }
70
+ </pre>
71
+ EOF
72
+
73
+ Filter::Markup(string).should == expected
74
+ end
75
+
76
+ it "should treat =, ==, h1., h2. etc as headings" do
77
+ string = <<-EOF
78
+ = Welcome
79
+ == Article
80
+ === Sub Article
81
+ EOF
82
+
83
+ string2 = <<-EOF
84
+ h1. Welcome
85
+ h2. Article
86
+ h3. Sub Article
87
+ EOF
88
+
89
+ expected = <<-EOF
90
+ <h1>Welcome</h1>
91
+ <h2>Article</h2>
92
+ <h3>Article</h3>
93
+ EOF
94
+
95
+ Filter::Markup(string).should == expected
96
+ Filter::Markup(string2).should == expected
97
+ end
98
+ end
@@ -0,0 +1,44 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+ require 'filters/phrase'
4
+
5
+ describe Filter::Phrase do
6
+ it "should not modify the string when no phrases are passed" do
7
+ string = 'everything is good here!'
8
+ Filter::Phrase(string).should == string
9
+ end
10
+
11
+ it "should remove phrases by default" do
12
+ string = 'fuck! this is so fucking awesome! fuck fuck'
13
+ result = Filter::Phrase(string, :phrases => %w( fucking fuck ))
14
+ result.should == 'fuck! this is so awesome!'
15
+ end
16
+
17
+ it "should substitute with an optional mask" do
18
+ string = 'fuck! this is so fucking awesome! fuck fuck'
19
+ result = Filter::Phrase(string, :phrases => %w( fucking fuck ), :mask => '*')
20
+ result.should == 'fuck! this is so ******* awesome! **** ****'
21
+ end
22
+
23
+ it "should substitute with associated masks" do
24
+ string = 'fuck! this is so fucking awesome! fuck shit'
25
+ result = Filter::Phrase(string, :phrases => [
26
+ [:fucking, '[bad]'],
27
+ [:fuck, '%'],
28
+ :shit
29
+ ], :mask => '*')
30
+ result.should == 'fuck! this is so [bad] awesome! %%%% ****'
31
+ end
32
+
33
+ it "should allow literal regexps" do
34
+ string = 'fuck! fuck fucking'
35
+ result = Filter::Phrase(string, :phrases => [/fuck(ing|!)?/])
36
+ result.should be_empty
37
+ end
38
+
39
+ it "should allow regexp-like strings" do
40
+ string = 'fuck! fuck fucking'
41
+ result = Filter::Phrase(string, :phrases => ['fuck(ing|!)?'])
42
+ result.should be_empty
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+ require 'filters/profanity'
4
+
5
+ describe Filter::Profanity do
6
+ it "should remove profanity" do
7
+ string = 'fuck this shit is so awesome i just fucking! love it cock bitch slut'
8
+ results = Filter::Profanity(string, :phrases => %w( fucking! ) )
9
+ results.should == 'this is so awesome i just love it'
10
+ end
11
+ end
@@ -0,0 +1,27 @@
1
+
2
+ require File.dirname(__FILE__) + '/spec_helper'
3
+ require 'filters/uri'
4
+
5
+ describe Filter::URI do
6
+ it "should return string when no uris are present" do
7
+ Filter::URI('foo bar').should == 'foo bar'
8
+ end
9
+
10
+ it "should convert URIs to clickable anchor tags" do
11
+ string = 'hey check out http://vision-media.ca'
12
+ Filter::URI(string).
13
+ should == 'hey check out <a href="http://vision-media.ca">http://vision-media.ca</a>'
14
+ end
15
+
16
+ it "should optionally open in a new window" do
17
+ string = 'hey check out http://vision-media.ca'
18
+ Filter::URI(string, :new_window => true).
19
+ should == 'hey check out <a href="http://vision-media.ca" target="_blank">http://vision-media.ca</a>'
20
+ end
21
+
22
+ it "should retain whitespace" do
23
+ string = 'goto ftp://foo.com to do stuff'
24
+ Filter::URI(string).
25
+ should == 'goto <a href="ftp://foo.com">ftp://foo.com </a>to do stuff'
26
+ end
27
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format specdoc
@@ -0,0 +1,3 @@
1
+
2
+ $:.unshift File.dirname(__FILE__) + '/../lib'
3
+ require 'filters'
data/tasks/docs.rake ADDED
@@ -0,0 +1,13 @@
1
+
2
+ namespace :docs do
3
+
4
+ desc 'Remove rdoc products'
5
+ task :remove => [:clobber_docs]
6
+
7
+ desc 'Build docs, and open in browser for viewing (specify BROWSER)'
8
+ task :open do
9
+ browser = ENV["BROWSER"] || "safari"
10
+ sh "open -a #{browser} doc/index.html"
11
+ end
12
+
13
+ end
@@ -0,0 +1,3 @@
1
+
2
+ desc 'Build gemspec file'
3
+ task :gemspec => [:build_gemspec]
data/tasks/spec.rake ADDED
@@ -0,0 +1,25 @@
1
+
2
+ require 'spec/rake/spectask'
3
+
4
+ desc "Run all specifications"
5
+ Spec::Rake::SpecTask.new(:spec) do |t|
6
+ t.libs << "lib"
7
+ t.spec_opts = ["--color", "--require", "spec/spec_helper.rb"]
8
+ end
9
+
10
+ namespace :spec do
11
+
12
+ desc "Run all specifications verbosely"
13
+ Spec::Rake::SpecTask.new(:verbose) do |t|
14
+ t.libs << "lib"
15
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
16
+ end
17
+
18
+ desc "Run specific specification verbosely (specify SPEC)"
19
+ Spec::Rake::SpecTask.new(:select) do |t|
20
+ t.libs << "lib"
21
+ t.spec_files = [ENV["SPEC"]]
22
+ t.spec_opts = ["--color", "--format", "specdoc", "--require", "spec/spec_helper.rb"]
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filters
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - TJ Holowaychuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-10 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Text filters including phrases, profanity, mini-markdown, uri, email, etc
17
+ email: tj@vision-media.ca
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.rdoc
24
+ - lib/filters.rb
25
+ - lib/filters/all.rb
26
+ - lib/filters/email.rb
27
+ - lib/filters/filter.rb
28
+ - lib/filters/markup.rb
29
+ - lib/filters/phrase.rb
30
+ - lib/filters/profanity.rb
31
+ - lib/filters/uri.rb
32
+ - lib/filters/version.rb
33
+ - tasks/docs.rake
34
+ - tasks/gemspec.rake
35
+ - tasks/spec.rake
36
+ files:
37
+ - History.rdoc
38
+ - Manifest
39
+ - README.rdoc
40
+ - Rakefile
41
+ - filters.gemspec
42
+ - lib/filters.rb
43
+ - lib/filters/all.rb
44
+ - lib/filters/email.rb
45
+ - lib/filters/filter.rb
46
+ - lib/filters/markup.rb
47
+ - lib/filters/phrase.rb
48
+ - lib/filters/profanity.rb
49
+ - lib/filters/uri.rb
50
+ - lib/filters/version.rb
51
+ - spec/filter_email_spec.rb
52
+ - spec/filter_markup_spec.rb
53
+ - spec/filter_phrase_spec.rb
54
+ - spec/filter_profanity_spec.rb
55
+ - spec/filter_uri_spec.rb
56
+ - spec/spec.opts
57
+ - spec/spec_helper.rb
58
+ - tasks/docs.rake
59
+ - tasks/gemspec.rake
60
+ - tasks/spec.rake
61
+ has_rdoc: true
62
+ homepage: http://github.com/visionmedia/filters
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options:
67
+ - --line-numbers
68
+ - --inline-source
69
+ - --title
70
+ - Filters
71
+ - --main
72
+ - README.rdoc
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "1.2"
86
+ version:
87
+ requirements: []
88
+
89
+ rubyforge_project: filters
90
+ rubygems_version: 1.3.5
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Text filters including phrases, profanity, mini-markdown, uri, email, etc
94
+ test_files: []
95
+