html5-rack-tidy 0.2.3

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.
Files changed (4) hide show
  1. data/README.md +109 -0
  2. data/lib/rack/tidy.rb +66 -0
  3. data/lib/rack/tidy/cleaner.rb +71 -0
  4. metadata +198 -0
@@ -0,0 +1,109 @@
1
+ # Tidy Markup Cleaner for Rack
2
+
3
+ *NOTE:* This is a fork of the rack-tidy gem.
4
+ This fork only provides basic HTML5 Doctype support.
5
+
6
+ Rack::Tidy cleans text/html markup by automatically
7
+ indenting and reformatting content. Best results are achieved
8
+ with valid markup, when you simply want to use this component
9
+ to produce clean (X)HTML rendered by templating systems such as ERb.
10
+
11
+ Rack::Tidy relies on the power of the Tidy gem and defaults to settings
12
+ based on convention. However, you can override these through configuration.
13
+
14
+ For more information about the Rack specification, check out http://rack.rubyforge.org
15
+
16
+ ## Installation
17
+
18
+ ### Bundler:
19
+
20
+ Add this to your Gemfile:
21
+
22
+ gem "html5-rack-tidy", "~> 0.2.3"
23
+
24
+ With a local copy:
25
+
26
+ $ git clone git://github.com/customink/html5-rack-tidy.git
27
+
28
+ And in your Gemfile:
29
+
30
+ gem "html5-rack-tidy", :path => "path/to/your/local/copy"
31
+
32
+ ### Command Line
33
+
34
+ Prerequisites:
35
+
36
+ * Rack gem (sudo gem install rack)
37
+ * Tidy gem (sudo gem install tidy)
38
+
39
+ Enter this at the command-line:
40
+
41
+ $ sudo gem install html5-rack-tidy
42
+
43
+ With a local working copy:
44
+
45
+ $ git clone git://github.com/customink/html5-rack-tidy.git
46
+ $ rake build && sudo rake install
47
+
48
+ ## Usage
49
+
50
+ ### Basic Usage
51
+
52
+ Rack::Tidy is implemented as a piece of Rack middleware and can be used with
53
+ any Rack-based application. If your application includes a rackup (`.ru`) file
54
+ or uses Rack::Builder to construct the application pipeline, simply require
55
+ and use as follows:
56
+
57
+ require 'rack/tidy'
58
+
59
+ use Rack::Tidy
60
+ run app
61
+
62
+ ### Using with Rails
63
+
64
+ Add this to your `config/environment.rb`:
65
+
66
+ # above Rails::Initializer block
67
+ require 'rack/tidy'
68
+
69
+ # inside Rails::Initializer block
70
+ config.middleware.use Rack::Tidy
71
+
72
+ You should now see `Rack::Tidy` listed in the middleware pipeline:
73
+
74
+ rake middleware
75
+
76
+ ### Configuration Options
77
+
78
+ The Tidy gem requires setting an internal path variable that points to the Tidy library,
79
+ which will differ per platform. Rack::Tidy defaults to '/usr/lib/libtidy.A.dylib'
80
+ which is the default location for the Tidy gem on Mac Leopard. To override the constant,
81
+ simply define it in your application's configuration file:
82
+
83
+ TIDY_LIB = '/usr/lib/tidylib.so'
84
+
85
+ Need Rack::Tidy to ignore certain paths? In your config, pass in an optional array of paths:
86
+
87
+ # Rails example
88
+ config.middleware.use Rack::Tidy,
89
+ :ignore_paths => ['/admin', '/cms']
90
+
91
+ Rack::Tidy relies on convention with regard to Tidy's configuration (see Rack::Tidy::Cleaner for details). If you want to override/set attributes supported by the Tidy gem, declare them in your configuration as an optional hash:
92
+
93
+ # Rails example
94
+ config.middleware.use Rack::Tidy,
95
+ 'indent-spaces' => 4
96
+
97
+ To specify the HTML5 Doctype:
98
+
99
+ config.middleware.use Rack::Tidy,
100
+ 'doctype' => 'html5'
101
+
102
+ See http://tidy.sourceforge.net/docs/quickref.html for Tidy gem config options
103
+
104
+ ## Copyright
105
+
106
+ Copyright (c) 2009 Phil Misiowiec, Webficient LLC. See MIT-LICENSE for details.
107
+
108
+ Modifications made by CustomInk, LLC (2011) without consent from Phil Misiowiec of Webficient, LLC as allowed by the licensing terms.
109
+
@@ -0,0 +1,66 @@
1
+ require 'rack'
2
+
3
+ # = Tidy Markup Cleaner for Rack
4
+ #
5
+ # Rack::Tidy cleans text/html markup by automatically
6
+ # indenting and reformatting content. Best results are achieved
7
+ # with valid markup, when you simply want to use this component
8
+ # to produce clean (X)HTML rendered by templating systems such as ERb.
9
+ #
10
+ # Rack::Tidy relies on the power of the Tidy gem and defaults to settings
11
+ # based on convention. However, you can override these through configuration.
12
+ #
13
+ # === Usage
14
+ #
15
+ # Within a rackup file (or with Rack::Builder):
16
+ # require 'rack/tidy'
17
+ # use Rack::Tidy,
18
+ # :ignore_paths => ['/admin', '/cms'],
19
+ # 'indent-spaces' => 4
20
+ # run app
21
+ #
22
+ # Rails example:
23
+ # # above Rails::Initializer block
24
+ # require 'rack/tidy'
25
+ #
26
+ # # inside Rails::Initializer block
27
+ # config.middleware.use Rack::Tidy,
28
+ # :ignore_paths => ['/admin', '/cms'],
29
+ # 'indent-spaces' => 4
30
+ module Rack::Tidy
31
+ autoload :Cleaner, 'rack/tidy/cleaner'
32
+
33
+
34
+ # look for libtidy lib in different OSes
35
+ def self.find_libtidy_path
36
+ [ "/usr/lib64/libtidy.so", # CentOs 64bit
37
+ "/usr/lib/libtidy.so", # CentOs/Fedora
38
+ "/usr/lib/tidylib.so", # (Ubuntu)
39
+ "/usr/lib/libtidy.A.dylib", # MacOS / default
40
+ ].each{|p|
41
+ if File.exist?(p)
42
+ puts "Found libtidy in: #{p}"
43
+ return p
44
+ end
45
+ }
46
+ puts "html5-rack-tidy couldn't find the libtidy library in your system"
47
+ puts "Run: yum install libtidy-devel on Fedora/CentOS"
48
+ raise "libtidy NOT FOUND!"
49
+ end
50
+
51
+
52
+ # Specify path of Tidy library, e.g.
53
+ # "/usr/lib/libtidy.A.dylib" (Mac; also the default if not set)
54
+ # "/usr/lib/tidylib.so" (Ubuntu)
55
+ # "/usr/lib/libtidy-0.99.so.0" (Fedora/CentOS)
56
+ TIDY_LIB = defined?(::TIDY_LIB) ? ::TIDY_LIB : find_libtidy_path
57
+
58
+ # Create a new Rack::Tidy middleware component that cleans text/html markup
59
+ # using the Tidy gem. The +options+ Hash can be used to specify which paths
60
+ # should be ignored during processing as well as Tidy gem configuration values.
61
+ # See Cleaner for defaults and http://tidy.sourceforge.net/docs/quickref.html
62
+ # for more options
63
+ def self.new(backend, options = {})
64
+ Cleaner.new(backend, options)
65
+ end
66
+ end
@@ -0,0 +1,71 @@
1
+ require 'tidy'
2
+
3
+ module Rack::Tidy
4
+ # This class is the interface between Rack and the Tidy gem
5
+ class Cleaner
6
+
7
+ # Defaults for the Tidy gem config
8
+ DEFAULT_TIDY_OPTS = {
9
+ 'char-encoding' => 'utf8',
10
+ 'indent' => true,
11
+ 'indent-spaces' => 2,
12
+ 'tidy-mark' => false,
13
+ 'wrap' => 0 }
14
+
15
+ # Tidy gem options, see http://tidy.sourceforge.net/docs/quickref.html
16
+ attr_accessor :tidy_options
17
+
18
+ # Paths to be ignored during Rack::Tidy processing
19
+ attr_accessor :ignore_paths
20
+
21
+ def initialize(app, options = {})
22
+ ::Tidy.path = TIDY_LIB
23
+ @app = app
24
+ self.ignore_paths = options[:ignore_paths] || []
25
+ self.tidy_options = DEFAULT_TIDY_OPTS.merge(options)
26
+ end
27
+
28
+ # method required by Rack interface
29
+ def call(env)
30
+ call! env
31
+ end
32
+
33
+ # thread safe version using shallow copy of env
34
+ def call!(env)
35
+ @env = env.dup
36
+ status, @headers, response = @app.call(@env)
37
+ if should_clean?
38
+ @headers.delete('Content-Length')
39
+ response = Rack::Response.new(
40
+ tidy_markup(response.respond_to?(:body) ? response.body : response),
41
+ status,
42
+ @headers
43
+ )
44
+ response.finish
45
+ response.to_a
46
+ else
47
+ [status, @headers, response]
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def should_clean? #:nodoc:
54
+ @headers["Content-Type"] &&
55
+ @headers["Content-Type"].include?("text/html") &&
56
+ self.ignore_paths.none? { |p| @env["PATH_INFO"].start_with?(p) }
57
+ end
58
+
59
+ def tidy_markup(content) #:nodoc:
60
+ # Brow beat in HTML5 Doctype support
61
+ cfg = self.tidy_options
62
+ if cfg['doctype'] == 'html5'
63
+ doctype = "<!DOCTYPE html>\n\n"
64
+ cfg['doctype'] = 'omit'
65
+ end
66
+ ##
67
+
68
+ ::Tidy.open(self.tidy_options) { |tidy| (doctype||'') + tidy.clean(content) }
69
+ end
70
+ end
71
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html5-rack-tidy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 17
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 3
10
+ version: 0.2.3
11
+ platform: ruby
12
+ authors:
13
+ - Phil Misiowiec
14
+ - Robert Bialek
15
+ - CustomInk, LLC
16
+ autorequire:
17
+ bindir: bin
18
+ cert_chain: []
19
+
20
+ date: 2011-06-01 00:00:00 -04:00
21
+ default_executable:
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ requirement: *id001
36
+ prerelease: false
37
+ name: bundler
38
+ type: :development
39
+ - !ruby/object:Gem::Dependency
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 1
48
+ - 6
49
+ - 0
50
+ version: 1.6.0
51
+ requirement: *id002
52
+ prerelease: false
53
+ name: jeweler
54
+ type: :development
55
+ - !ruby/object:Gem::Dependency
56
+ version_requirements: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ hash: 41
62
+ segments:
63
+ - 0
64
+ - 9
65
+ - 9
66
+ version: 0.9.9
67
+ requirement: *id003
68
+ prerelease: false
69
+ name: rcov
70
+ type: :development
71
+ - !ruby/object:Gem::Dependency
72
+ version_requirements: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ hash: 27
78
+ segments:
79
+ - 1
80
+ - 3
81
+ - 0
82
+ version: 1.3.0
83
+ requirement: *id004
84
+ prerelease: false
85
+ name: rack
86
+ type: :runtime
87
+ - !ruby/object:Gem::Dependency
88
+ version_requirements: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 23
94
+ segments:
95
+ - 1
96
+ - 1
97
+ - 2
98
+ version: 1.1.2
99
+ requirement: *id005
100
+ prerelease: false
101
+ name: tidy
102
+ type: :runtime
103
+ - !ruby/object:Gem::Dependency
104
+ version_requirements: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ hash: 15
110
+ segments:
111
+ - 1
112
+ - 6
113
+ - 0
114
+ version: 1.6.0
115
+ requirement: *id006
116
+ prerelease: false
117
+ name: jeweler
118
+ type: :development
119
+ - !ruby/object:Gem::Dependency
120
+ version_requirements: &id007 !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ hash: 27
126
+ segments:
127
+ - 1
128
+ - 2
129
+ - 2
130
+ version: 1.2.2
131
+ requirement: *id007
132
+ prerelease: false
133
+ name: redgreen
134
+ type: :development
135
+ - !ruby/object:Gem::Dependency
136
+ version_requirements: &id008 !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ~>
140
+ - !ruby/object:Gem::Version
141
+ hash: 37
142
+ segments:
143
+ - 2
144
+ - 11
145
+ - 3
146
+ version: 2.11.3
147
+ requirement: *id008
148
+ prerelease: false
149
+ name: shoulda
150
+ type: :development
151
+ description: Rack middleware for automatically cleaning markup using Tidy. Now with HTML5 support!
152
+ email: rubygems@customink.com
153
+ executables: []
154
+
155
+ extensions: []
156
+
157
+ extra_rdoc_files:
158
+ - README.md
159
+ files:
160
+ - lib/rack/tidy.rb
161
+ - lib/rack/tidy/cleaner.rb
162
+ - README.md
163
+ has_rdoc: true
164
+ homepage: http://github.com/customink/html5-rack-tidy
165
+ licenses: []
166
+
167
+ post_install_message:
168
+ rdoc_options: []
169
+
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ none: false
174
+ requirements:
175
+ - - ">="
176
+ - !ruby/object:Gem::Version
177
+ hash: 3
178
+ segments:
179
+ - 0
180
+ version: "0"
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ none: false
183
+ requirements:
184
+ - - ">="
185
+ - !ruby/object:Gem::Version
186
+ hash: 3
187
+ segments:
188
+ - 0
189
+ version: "0"
190
+ requirements: []
191
+
192
+ rubyforge_project:
193
+ rubygems_version: 1.4.0
194
+ signing_key:
195
+ specification_version: 3
196
+ summary: Rack middleware for automatically cleaning markup using Tidy
197
+ test_files: []
198
+