phorsfall-tidy_rack 0.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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Paul Horsfall
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.textile ADDED
@@ -0,0 +1,36 @@
1
+ h1. Tidy Rack
2
+
3
+ Tidy Rack is a "Rack":http://rack.rubyforge.org/ middleware that runs the body of HTML responses through "Tidy":http://tidy.sourceforge.net/.
4
+
5
+ It's almost certainly a terrible idea to use this in production, but in development it does two useful things:
6
+
7
+ * Includes an HTML comment at the end of the response body that includes info/errors/warnings generated by Tidy.
8
+ * Prettifies and indents HTML, making View Source easier on the eye.
9
+
10
+ h2. Installation
11
+
12
+ Install the Gem:
13
+
14
+ <pre>
15
+ gem install phorsfall-tidy_rack --source http://gems.github.com
16
+ </pre>
17
+
18
+ In Rails, you can add Tidy Rack to your middleware stack by adding the following to @config/development.rb@:
19
+
20
+ <pre>
21
+ config.middleware.use 'TidyRack'
22
+ </pre>
23
+
24
+ In Sinatra or a RackUp script, this should work:
25
+
26
+ <pre>
27
+ use TidyRack
28
+ </pre>
29
+
30
+ h2. Dependencies
31
+
32
+ You'll need the "Tidy":http://tidy.sourceforge.net/ binaries available to use Tidy Rack. If you're on OS X or Linux you've likely got them already, if you're on Windows you probably have not.
33
+
34
+ h2. Copyright
35
+
36
+ Copyright (c) 2009 Paul Horsfall. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "tidy_rack"
8
+ gem.summary = %Q{A Rack middleware that runs the response body through HTML Tidy.}
9
+ gem.email = "horsfallp@gmail.com"
10
+ gem.homepage = "http://github.com/phorsfall/tidy_rack"
11
+ gem.authors = ["Paul Horsfall"]
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 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "tidy_rack #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 0
3
+ :patch: 0
4
+ :major: 0
data/lib/tidy_rack.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'Open3'
2
+
3
+ class TidyRack
4
+ def initialize(app)
5
+ @app = app
6
+ end
7
+
8
+ def call(env)
9
+ status, headers, response = @app.call(env)
10
+ if headers['Content-Type'].include?('text/html')
11
+ response = tidy(response)
12
+ headers['Content-Length'] = response.size.to_s
13
+ end
14
+ [status, headers, response]
15
+ end
16
+
17
+ def tidy(response)
18
+ html = ''
19
+ Open3.popen3("tidy -i") do |stdin, stdout, stderr|
20
+ response.each { |s| stdin.write s }
21
+ stdin.close
22
+ html << stdout.read
23
+ html << "<!--\n#{stderr.read}\n-->\n"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'hpricot'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'tidy_rack'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class TidyRackTest < Test::Unit::TestCase
4
+ def html
5
+ <<-eof
6
+ <html>
7
+ <head>
8
+ <title>Tidy Rack<title>
9
+ </head>
10
+ <body>Hello, world!</body>
11
+ </html>
12
+ eof
13
+ end
14
+
15
+ should "include the HTML Tidy generated meta tag" do
16
+ app = lambda { |env| [200, { 'Content-Type' => 'text/html' }, html] }
17
+ status, headers, response = TidyRack.new(app).call({})
18
+ doc = Hpricot(response)
19
+ assert doc.at('meta[@content*="HTML Tidy"]')
20
+ end
21
+
22
+ should "only tidy html responses" do
23
+ app = lambda { |env| [200, { 'Content-Type' => 'text/plain' }, html] }
24
+ status, headers, response = TidyRack.new(app).call({})
25
+ doc = Hpricot(response)
26
+ assert_nil doc.at('meta[@content*="HTML Tidy"]')
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: phorsfall-tidy_rack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Horsfall
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-05 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: horsfallp@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.textile
25
+ files:
26
+ - LICENSE
27
+ - README.textile
28
+ - Rakefile
29
+ - VERSION.yml
30
+ - lib/tidy_rack.rb
31
+ - test/test_helper.rb
32
+ - test/tidy_rack_test.rb
33
+ has_rdoc: true
34
+ homepage: http://github.com/phorsfall/tidy_rack
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: A Rack middleware that runs the response body through HTML Tidy.
59
+ test_files:
60
+ - test/test_helper.rb
61
+ - test/tidy_rack_test.rb