rack-gsub 0.0.0 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,7 @@
1
+ 0.1.0 (July 18, 2010)
2
+ * Explained the API in more detail in the README.
3
+ * Gsub no longer replaces the text of form elements.
4
+ * Added automated tests.
5
+
1
6
  0.0.0 (December 3, 2009)
2
7
  * Initial release.
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License
2
2
 
3
- Copyright (c) 2009 Wyatt M. Greene
3
+ Copyright (c) 2009, 2010 Wyatt M. Greene
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -13,7 +13,32 @@ occurrences of "the":
13
13
 
14
14
  use Rack::Gsub, "five" => "three", "the" => ""
15
15
 
16
- Note that you can use regular expressions, too, just like with Ruby's gsub.
17
16
  This is the syntax for using within Rails' config/environment.rb:
18
17
 
19
18
  config.middleware.use Rack::Gsub, "five" => "three", "the" => ""
19
+
20
+ == Details
21
+
22
+ You can use regular expressions, too, just like with Ruby's gsub:
23
+
24
+ use Rack::Gsub, /five/ => "5"
25
+ use Rack::Gsub, /f\w+e/ => "5"
26
+
27
+ By default, the arguments are case-sensitive. Use the /i regex switch to
28
+ do a case-insensitive replace:
29
+
30
+ use Rack::Gsub, /five/i => "5"
31
+
32
+ Only text within the body of the document is replaced. The following will
33
+ only replace the word "five" in the body, not in the page's title:
34
+
35
+ use Rack::Gsub, "five" => "5"
36
+
37
+ Rack::Gsub does not replace the text inside form elements.
38
+
39
+ Rack::Gsub eventually passes the arguments to Ruby's String.gsub method,
40
+ so it behaves the same way.
41
+
42
+ use Rack::Gsub, /five/ => "5"
43
+ # gets turned into
44
+ string.gsub(/five/, "5")
data/Rakefile CHANGED
@@ -1,12 +1,23 @@
1
1
  require 'rake'
2
+ require 'rake/testtask'
2
3
  require 'rubygems'
3
4
 
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test Rack::Gsub'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
4
15
  begin
5
16
  require 'jeweler'
6
17
  Jeweler::Tasks.new do |s|
7
18
  s.name = "rack-gsub"
8
- s.version = "0.0.0"
9
- s.add_dependency 'rack-plastic', '>= 0.0.3'
19
+ s.version = "0.1.0"
20
+ s.add_dependency 'rack-plastic', '>= 0.1.1'
10
21
  s.author = "Wyatt Greene"
11
22
  s.email = "techiferous@gmail.com"
12
23
  s.summary = "Rack middleware wrapper for gsub."
@@ -29,5 +40,5 @@ begin
29
40
  end
30
41
  Jeweler::GemcutterTasks.new
31
42
  rescue LoadError
32
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
43
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
33
44
  end
@@ -5,7 +5,7 @@ module Rack
5
5
 
6
6
  def change_nokogiri_doc(doc)
7
7
  doc.at_css("body").traverse do |node|
8
- if node.text?
8
+ if node.text? && node.parent.name != 'textarea' && node.parent.name != 'option'
9
9
  options.each do |pattern, replacement|
10
10
  update_text(node, node.content.gsub(pattern, replacement))
11
11
  end
@@ -0,0 +1,197 @@
1
+ require 'test/test_helper'
2
+
3
+ class GsubTest < Test::Unit::TestCase
4
+
5
+ def test_no_arguments
6
+ before_html = %Q{
7
+ <!DOCTYPE html
8
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
9
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
10
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
11
+ <head>
12
+ <title>Testing Rack::Gsub</title>
13
+ </head>
14
+ <body>
15
+ Hi, Mom!
16
+ </body>
17
+ </html>
18
+ }
19
+ after_html = process_html(before_html, Rack::Gsub)
20
+ assert_html_equal before_html, after_html
21
+ end
22
+
23
+ def test_basic_replace
24
+ before_html = %Q{
25
+ <!DOCTYPE html
26
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
27
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
28
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
29
+ <head>
30
+ <title>Testing Rack::Gsub</title>
31
+ </head>
32
+ <body>
33
+ In Soviet Russia, there is censorship.
34
+ </body>
35
+ </html>
36
+ }
37
+ expected_html = %Q{
38
+ <!DOCTYPE html
39
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
40
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
41
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
42
+ <head>
43
+ <title>Testing Rack::Gsub</title>
44
+ </head>
45
+ <body>
46
+ In Soviet Russia, there is no censorship.
47
+ </body>
48
+ </html>
49
+ }
50
+ after_html = process_html(before_html, Rack::Gsub, "censorship" => "no censorship")
51
+ assert_html_equal expected_html, after_html
52
+ end
53
+
54
+ def test_multiple_replace
55
+ before_html = %Q{
56
+ <!DOCTYPE html
57
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
58
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
59
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
60
+ <head>
61
+ <title>Testing Rack::Gsub</title>
62
+ </head>
63
+ <body>
64
+ In Soviet Russia, there is censorship.
65
+ </body>
66
+ </html>
67
+ }
68
+ expected_html = %Q{
69
+ <!DOCTYPE html
70
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
71
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
72
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
73
+ <head>
74
+ <title>Testing Rack::Gsub</title>
75
+ </head>
76
+ <body>
77
+ In SovXXt RussXa, thXrX Xs cXnsorshXp.
78
+ </body>
79
+ </html>
80
+ }
81
+ after_html = process_html(before_html, Rack::Gsub, "i" => "X", "e" => "X")
82
+ assert_html_equal expected_html, after_html
83
+ end
84
+
85
+ def test_regex_replace
86
+ before_html = %Q{
87
+ <!DOCTYPE html
88
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
89
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
90
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
91
+ <head>
92
+ <title>Testing Rack::Gsub</title>
93
+ </head>
94
+ <body>
95
+ I'm getting better!
96
+ No, you're not. You'll be stone dead in a moment.
97
+ Oh, I can't take him like that; it's against regulations.
98
+ I don't want to go in the cart!
99
+ Oh, don't be such a baby.
100
+ I can't take him...
101
+ I feel fine!
102
+ </body>
103
+ </html>
104
+ }
105
+ expected_html = %Q{
106
+ <!DOCTYPE html
107
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
108
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
109
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
110
+ <head>
111
+ <title>Testing Rack::Gsub</title>
112
+ </head>
113
+ <body>
114
+ I'm getting better!
115
+ No, you're not. You'll be stone dead in a moment.
116
+ Oh, I can't take him like th**; it's against regul**ions.
117
+ I don't want to go in the c**t!
118
+ Oh, don't be such a baby.
119
+ I can't take him...
120
+ I feel fine!
121
+ </body>
122
+ </html>
123
+ }
124
+ after_html = process_html(before_html, Rack::Gsub, /a[t,r]/ => '**')
125
+ assert_html_equal expected_html, after_html
126
+ end
127
+
128
+ def test_case_insensitive_regex_replace
129
+ before_html = %Q{
130
+ <!DOCTYPE html
131
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
132
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
133
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
134
+ <head>
135
+ <title>Testing Rack::Gsub</title>
136
+ </head>
137
+ <body>
138
+ one ONE One
139
+ two TWO Two
140
+ three THREE Three
141
+ four FOUR Four
142
+ </body>
143
+ </html>
144
+ }
145
+ expected_html = %Q{
146
+ <!DOCTYPE html
147
+ PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
148
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
149
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
150
+ <head>
151
+ <title>Testing Rack::Gsub</title>
152
+ </head>
153
+ <body>
154
+ 1 ONE One
155
+ two TWO Two
156
+ 3 3 3
157
+ four FOUR Four
158
+ </body>
159
+ </html>
160
+ }
161
+ after_html = process_html(before_html, Rack::Gsub, /one/ => '1', /three/i => '3')
162
+ assert_html_equal expected_html, after_html
163
+ end
164
+
165
+ def test_form_elements_not_replaced
166
+ before_html = %Q{
167
+ <html>
168
+ <head><title>Testing Rack::Gsub</title></head>
169
+ <body>
170
+ <div id="container">
171
+ <p>
172
+ The contents of form elements should not be replaced by default.
173
+ <form action="/users/signup" class="signup_new_user" id="foo" method="post">
174
+ <input id="user_name" name="user[name]" size="80" tabindex="1" type="text"
175
+ value="This should not be replaced.">
176
+ <select id="subscription_id" name="subscription_id" tabindex="2">
177
+ <option value="1" selected>This should not be replaced.</option>
178
+ <option value="2">This also should not be replaced.</option>
179
+ </select>
180
+ <textarea cols="20" id="commitment_note" name="commitment[note]" rows="10" tabindex="18">
181
+ This should not be replaced.
182
+ </textarea>
183
+ <div class="actions">
184
+ <input id="user_submit" name="commit" tabindex="3" type="submit"
185
+ value="This should not be replaced.">
186
+ </div>
187
+ </form>
188
+ </p>
189
+ </div>
190
+ </body>
191
+ </html>
192
+ }
193
+ after_html = process_html(before_html, Rack::Gsub, 'This' => '!!!')
194
+ assert_html_equal before_html, after_html
195
+ end
196
+
197
+ end
@@ -0,0 +1,15 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'redgreen'
4
+ require 'rack-gsub'
5
+ require 'plastic_test_helper'
6
+
7
+ module Test
8
+ module Unit
9
+ class TestCase
10
+
11
+ include PlasticTestHelper
12
+
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-gsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Wyatt Greene
@@ -9,19 +15,25 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-12-03 00:00:00 -05:00
18
+ date: 2010-07-18 00:00:00 -04:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: rack-plastic
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
23
- version: 0.0.3
24
- version:
29
+ hash: 25
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 1
34
+ version: 0.1.1
35
+ type: :runtime
36
+ version_requirements: *id001
25
37
  description: "\n This is a Rack middleware wrapper for gsub. You can replace text on your\n web page without worrying about accidentally modifying the HTML tags\n themselves.\n "
26
38
  email: techiferous@gmail.com
27
39
  executables: []
@@ -37,8 +49,10 @@ files:
37
49
  - README.rdoc
38
50
  - Rakefile
39
51
  - lib/rack-gsub.rb
52
+ - test/gsub_test.rb
40
53
  - test/rackapp/app.rb
41
54
  - test/rackapp/config.ru
55
+ - test/test_helper.rb
42
56
  has_rdoc: true
43
57
  homepage: http://github.com/techiferous/rack-gsub
44
58
  licenses: []
@@ -49,23 +63,31 @@ rdoc_options:
49
63
  require_paths:
50
64
  - lib
51
65
  required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
52
67
  requirements:
53
68
  - - ">="
54
69
  - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
55
73
  version: "0"
56
- version:
57
74
  required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
58
76
  requirements:
59
77
  - - ">="
60
78
  - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
61
82
  version: "0"
62
- version:
63
83
  requirements:
64
84
  - none
65
85
  rubyforge_project:
66
- rubygems_version: 1.3.5
86
+ rubygems_version: 1.3.7
67
87
  signing_key:
68
88
  specification_version: 3
69
89
  summary: Rack middleware wrapper for gsub.
70
90
  test_files:
91
+ - test/gsub_test.rb
71
92
  - test/rackapp/app.rb
93
+ - test/test_helper.rb