Skip to content

Commit

Permalink
Merge pull request #40 from id774/master
Browse files Browse the repository at this point in the history
Add Filter::Sanitize
  • Loading branch information
id774 committed Jun 20, 2013
2 parents 6c25ced + 1b0cd92 commit b717b38
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -12,6 +12,7 @@ gem 'gcalapi'
gem 'xml-simple'
gem 'feedbag'
gem 'nokogiri'
gem 'sanitize'
gem 'twitter'
gem 'weather_hacker'
gem 'pocket-ruby'
Expand Down
16 changes: 16 additions & 0 deletions doc/PLUGINS
Expand Up @@ -281,6 +281,22 @@ FilterRand
- module: FilterRand


FilterSanitize
--------------
[Path]
/plugins/filter/sanitize.rb

[Abstract]
Strip the html tags.
Reference: https://github.com/rgrove/sanitize

[Syntax]
- module: FilterSanitize
config:
mode: basic or relaxed or restricted
(default: restricted)


FilterFullFeed
--------------
[Path]
Expand Down
16 changes: 16 additions & 0 deletions doc/PLUGINS.ja
Expand Up @@ -282,6 +282,22 @@ FilterRand
- module: FilterRand


FilterSanitize
--------------
[パス]
/plugins/filter/sanitize.rb

[概要]
HTML タグを除去する
参照 https://github.com/rgrove/sanitize

[レシピ記法]
- module: FilterSanitize
config:
mode: basic or relaxed or restricted
(デフォルト restricted)


FilterFullFeed
--------------
[パス]
Expand Down
41 changes: 41 additions & 0 deletions plugins/filter/sanitize.rb
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# Name:: Automatic::Plugin::Filter::Sanitize
# Author:: 774 <http://id774.net>
# Created:: Jun 20, 2013
# Updated:: Jun 20, 2013
# Copyright:: 774 Copyright (c) 2013
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.

module Automatic::Plugin
class FilterSanitize
require 'sanitize'

def initialize(config, pipeline=[])
@config = config
@pipeline = pipeline
case @config['mode']
when "basic"
@mode = Sanitize::Config::BASIC
when "relaxed"
@mode = Sanitize::Config::RELAXED
else
@mode = Sanitize::Config::RESTRICTED
end
end

def run
@return_feeds = []
@pipeline.each {|feeds|
return_feed_items = []
unless feeds.nil?
feeds.items.each {|feed|
feed.description = Sanitize.clean(feed.description, @mode) unless feed.description.nil?
}
@return_feeds << feeds
end
}
@return_feeds
end

end
end
128 changes: 128 additions & 0 deletions spec/plugins/filter/sanitize_spec.rb
@@ -0,0 +1,128 @@
# -*- coding: utf-8 -*-
# Name:: Automatic::Plugin::Filter::Sanitize
# Author:: 774 <http://id774.net>
# Created:: Jun 20, 2013
# Updated:: Jun 20, 2013
# Copyright:: 774 Copyright (c) 2013
# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.

require File.expand_path(File.dirname(__FILE__) + '../../../spec_helper')

require 'filter/sanitize'

describe Automatic::Plugin::FilterSanitize do
context "It should be sanitized" do
subject {
Automatic::Plugin::FilterSanitize.new(
{},
AutomaticSpec.generate_pipeline {
feed {
item "http://testsite.org", "hoge",
"<a>fuga</a>",
"Mon, 07 Mar 2011 15:54:11 +0900"
}
}
)
}

describe "#run" do
its(:run) { should have(1).feeds }

specify {
subject.run
subject.instance_variable_get(:@return_feeds)[0].items.
count.should == 1
subject.instance_variable_get(:@return_feeds)[0].items[0].description.
should == 'fuga'
}
end
end

context "It should not be sanitized in basic mode" do
subject {
Automatic::Plugin::FilterSanitize.new(
{
'mode' => "basic"
},
AutomaticSpec.generate_pipeline {
feed {
item "http://testsite.org", "hoge",
"<a>fuga</a>",
"Mon, 07 Mar 2011 15:54:11 +0900"
}
}
)
}

describe "#run" do
its(:run) { should have(1).feeds }

specify {
subject.run
subject.instance_variable_get(:@return_feeds)[0].items.
count.should == 1
subject.instance_variable_get(:@return_feeds)[0].items[0].description.
should == '<a rel="nofollow">fuga</a>'
}
end
end

context "It should not be sanitized in restricted mode" do
subject {
Automatic::Plugin::FilterSanitize.new(
{
'mode' => "restricted"
},
AutomaticSpec.generate_pipeline {
feed {
item "http://testsite.org", "hoge",
"<a>fuga</a>",
"Mon, 07 Mar 2011 15:54:11 +0900"
}
}
)
}

describe "#run" do
its(:run) { should have(1).feeds }

specify {
subject.run
subject.instance_variable_get(:@return_feeds)[0].items.
count.should == 1
subject.instance_variable_get(:@return_feeds)[0].items[0].description.
should == 'fuga'
}
end
end

context "It should not be sanitized in relaxed mode" do
subject {
Automatic::Plugin::FilterSanitize.new(
{
'mode' => "relaxed"
},
AutomaticSpec.generate_pipeline {
feed {
item "http://testsite.org", "hoge",
"<a>fuga</a>",
"Mon, 07 Mar 2011 15:54:11 +0900"
}
}
)
}

describe "#run" do
its(:run) { should have(1).feeds }

specify {
subject.run
subject.instance_variable_get(:@return_feeds)[0].items.
count.should == 1
subject.instance_variable_get(:@return_feeds)[0].items[0].description.
should == '<a>fuga</a>'
}
end
end

end
22 changes: 22 additions & 0 deletions test/integration/test_sanitize.yml
@@ -0,0 +1,22 @@
global:
timezone: Asia/Tokyo
cache:
base: /tmp
log:
level: info

plugins:
- module: SubscriptionFeed
config:
feeds:
- http://blog.id774.net/post/feed/

- module: FilterSanitize
config:
mode: restricted

- module: StoreFullText
config:
db: test_sanitize.db

# - module: PublishConsole

0 comments on commit b717b38

Please sign in to comment.