hiroeorz-simple-html-helper 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +48 -0
- data/VERSION.yml +4 -0
- data/lib/simple_html_helper.rb +95 -0
- data/spec/simple_html_helper_spec.rb +57 -0
- data/spec/spec_helper.rb +9 -0
- metadata +61 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 hiroeorz
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "simple-html-helper"
|
8
|
+
gem.summary = %Q{TODO}
|
9
|
+
gem.email = "hiroe.orz@gmail.com"
|
10
|
+
gem.homepage = "http://github.com/hiroeorz/simple-html-helper"
|
11
|
+
gem.authors = ["hiroeorz"]
|
12
|
+
|
13
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
14
|
+
end
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'spec/rake/spectask'
|
20
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
21
|
+
spec.libs << 'lib' << 'spec'
|
22
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
23
|
+
end
|
24
|
+
|
25
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
28
|
+
spec.rcov = true
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
task :default => :spec
|
33
|
+
|
34
|
+
require 'rake/rdoctask'
|
35
|
+
Rake::RDocTask.new do |rdoc|
|
36
|
+
if File.exist?('VERSION.yml')
|
37
|
+
config = YAML.load(File.read('VERSION.yml'))
|
38
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
39
|
+
else
|
40
|
+
version = ""
|
41
|
+
end
|
42
|
+
|
43
|
+
rdoc.rdoc_dir = 'rdoc'
|
44
|
+
rdoc.title = "simple-html-helper #{version}"
|
45
|
+
rdoc.rdoc_files.include('README*')
|
46
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
47
|
+
end
|
48
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
begin
|
2
|
+
require "rubygems"
|
3
|
+
rescue LoadError
|
4
|
+
end
|
5
|
+
|
6
|
+
|
7
|
+
require "sanitize"
|
8
|
+
|
9
|
+
class String
|
10
|
+
@@html_replace_target = {
|
11
|
+
"<" => "<",
|
12
|
+
">" => ">",
|
13
|
+
"\s" => " ",
|
14
|
+
"\"" => """,
|
15
|
+
}
|
16
|
+
|
17
|
+
@@sanitize_config = Sanitize::Config::RELAXED.
|
18
|
+
merge({:add_attributes => {"a" => {"target" => "_blank"}}})
|
19
|
+
|
20
|
+
@@use_sanitize = true
|
21
|
+
|
22
|
+
|
23
|
+
def String.use_sanitize=(value)
|
24
|
+
if !value.instance_of?(TrueClass) and !value.instance_of?(FalseClass)
|
25
|
+
raise ArgumentError.new("invalid argument. set true or false")
|
26
|
+
end
|
27
|
+
|
28
|
+
@@use_sanitize = value
|
29
|
+
end
|
30
|
+
|
31
|
+
def String.use_sanitize
|
32
|
+
@@use_sanitize
|
33
|
+
end
|
34
|
+
|
35
|
+
def String.sanitize_config=(value)
|
36
|
+
@@sanitize_config = value
|
37
|
+
end
|
38
|
+
|
39
|
+
def String.sanitize_config
|
40
|
+
@@sanitize_config
|
41
|
+
end
|
42
|
+
|
43
|
+
def String.html_replace_target=(value)
|
44
|
+
@@html_replace_target = value
|
45
|
+
end
|
46
|
+
|
47
|
+
def String.html_replace_target
|
48
|
+
@@html_replace_target
|
49
|
+
end
|
50
|
+
|
51
|
+
def sanitize(sanitize_config = @@sanitize_config)
|
52
|
+
unless @@use_sanitize
|
53
|
+
return self.dup
|
54
|
+
end
|
55
|
+
|
56
|
+
Sanitize.clean(self, sanitize_config)
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_html
|
60
|
+
self.
|
61
|
+
to_html_amp.
|
62
|
+
to_html_special_char.
|
63
|
+
to_html_br.
|
64
|
+
to_html_link.
|
65
|
+
sanitize
|
66
|
+
end
|
67
|
+
|
68
|
+
def to_html_link(target = "_blank")
|
69
|
+
self.gsub(/(https?\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/) {
|
70
|
+
"<a href=\"#{$1}\" target=\"#{target}\">#{$1}</a>"
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_html_special_char
|
75
|
+
text = self.dup
|
76
|
+
|
77
|
+
@@html_replace_target.each do |key, value|
|
78
|
+
text.gsub!(/#{key}/, value)
|
79
|
+
end
|
80
|
+
|
81
|
+
text
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_html_amp
|
85
|
+
self.gsub(/\&/, "&")
|
86
|
+
end
|
87
|
+
|
88
|
+
def to_html_br
|
89
|
+
self.
|
90
|
+
gsub(/\r\n?/, "\n").
|
91
|
+
gsub(/\r/, "\n").
|
92
|
+
gsub(/\n/, "<br />")
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
+
|
4
|
+
describe "SimpleHtmlHelper" do
|
5
|
+
it "should replace html tags" do
|
6
|
+
text =<<EOF
|
7
|
+
hello this is my blog
|
8
|
+
|
9
|
+
my home pege is http://my.home.page/blogs/20090515
|
10
|
+
please check this url
|
11
|
+
|
12
|
+
EOF
|
13
|
+
|
14
|
+
http_text = "hello this is my blog<br /><br />my home pege is <a href=\"http://my.home.page/blogs/20090515\" target=\"_blank\">http://my.home.page/blogs/20090515</a><br />please check this url<br /><br />"
|
15
|
+
|
16
|
+
text.to_html.should == http_text
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should replace \n,\r\n,\r -> <br />" do
|
20
|
+
"hello\nhello".to_html_br.should == "hello<br />hello"
|
21
|
+
"hello\r\nhello".to_html_br.should == "hello<br />hello"
|
22
|
+
"hello\rhello".to_html_br.should == "hello<br />hello"
|
23
|
+
|
24
|
+
"hello\n\n\nhello".to_html_br.should == "hello<br /><br /><br />hello"
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should replace & -> &" do
|
29
|
+
"hello&world".to_html_amp.should == "hello&world"
|
30
|
+
"hello&&world".to_html_amp.should == "hello&&world"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should replace special character" do
|
34
|
+
"hello<good>world".to_html_special_char.should == "hello<good>world"
|
35
|
+
"hello world".to_html_special_char.should == "hello world"
|
36
|
+
'hello"world'.to_html_special_char.should == "hello"world"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should replace url -> url link" do
|
40
|
+
"helo http://google.co.jp world".to_html_link.should ==
|
41
|
+
"helo <a href=\"http://google.co.jp\" target=\"_blank\">http://google.co.jp</a> world"
|
42
|
+
|
43
|
+
"helohttp://google.co.jp world".to_html_link.should ==
|
44
|
+
"helo<a href=\"http://google.co.jp\" target=\"_blank\">http://google.co.jp</a> world"
|
45
|
+
|
46
|
+
"helo\nhttp://google.co.jp\nworld".to_html_link.should ==
|
47
|
+
"helo\n<a href=\"http://google.co.jp\" target=\"_blank\">http://google.co.jp</a>\nworld"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should sanitize not allowed tag" do
|
51
|
+
text = "hello<script type='text/javascript'>alert('script')</script>wolrd"
|
52
|
+
text.sanitize.should == "helloalert('script')wolrd"
|
53
|
+
|
54
|
+
text = "<embed src='../images/htmq.swf' />"
|
55
|
+
text.sanitize.should == ""
|
56
|
+
end
|
57
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hiroeorz-simple-html-helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- hiroeorz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-15 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: hiroe.orz@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- LICENSE
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- Rakefile
|
29
|
+
- VERSION.yml
|
30
|
+
- lib/simple_html_helper.rb
|
31
|
+
- spec/simple_html_helper_spec.rb
|
32
|
+
- spec/spec_helper.rb
|
33
|
+
has_rdoc: true
|
34
|
+
homepage: http://github.com/hiroeorz/simple-html-helper
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options:
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 2
|
58
|
+
summary: TODO
|
59
|
+
test_files:
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
- spec/simple_html_helper_spec.rb
|