Filters

Ruby text filters consisting of the following core filters:

  • phrase

  • profanity (uses phrase)

  • email (converts emails to mailto: anchor tags)

  • uri (converts uris to anchor tags)

  • markup (mini-markup language)

Mutative

All filters return a new string by default, however when you bang! it, the string object initially passed will be modified. This method is often ideal, as it is easier to chain filters together.

Filter::Profanity!(text)
Filter::Email!(text)
...

Filter::Phrase

Filter::Phrase('bad word!', :phrases => ['bad'])
# => 'word!'

Filter::Phrase('bad word!', :phrases => ['bad'], :mask => '*')
# => '*** word!'

Filter::Phrase('bad word!', :phrases => ['bad'], :mask => '[removed]')
# => '[removed] word!'

Filter::Phrase('foobar', :phrases => ['foo(bar)?'])
# => ''

Filter::Phrase('foobar', :phrases => [/foo(bar)?/])
# => ''

Filter::Phrase('some foo bar', :phrases => [['foo', '*'], ['bar', '[removed]']])
# => 'some *** [removed]'

Filter::Profanity < Filter::Phrase

Filter::Profanity('fuck this shit is so awesome i cant fucking believe it')
# => 'this is so awesome i cant believe it'

Filter::Profanity('fuck this shit is so awesome i cant fucking believe it', :mask => '*')
# => '**** this **** is so awesome i cant ******* believe it'

Filter::URI

Filter::URI('hey checkout http://vision-media.ca sometime')
# => 'hey checkout <a href="http://vision-media.ca">http://vision-media.ca</a> sometime'

Filter::Email

Filter::Email('hey email [email protected] sometime')
# => 'hey email <a href="mailto:[email protected]">[email protected]</a> sometime'

Filter::Markup

Filter::Markup <<-EOF
  = Welcome

  == Article

  This article is meant to show how a simple *markup* language
  can be generated using the filter gem.

  === Source

  @@@ javascript
    foo = function(){
      bar()
    }
  @@@
EOF

Would output:

<h1>Welcome</h1>

<h2>Article</h2>

<p> This article is meant to show how a simple <strong>markup</strong> language
can be generated using the filter gem. </p>

<h3>Source</h3>

<code class="javascript">
  foo = function(){
    bar()
  }
</code>

License:

(The MIT License)

Copyright © 2009 TJ Holowaychuk <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, an d/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.