webficient (owner)

Revisions

gist: 209459 Download_button fork
public
Description:
Rack::Tidy code extracted from gem
Public Clone URL: git://gist.github.com/209459.git
Embed All Files: show embed
cleaner.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# gem available at http://github.com/webficient/rack-tidy
# source code extracted from gem:
 
require 'tidy'
 
module Rack::Tidy
  # This class is the interface between Rack and the Tidy gem
  class Cleaner
    
    # Defaults for the Tidy gem config
    DEFAULT_TIDY_OPTS = {
      'char-encoding' => 'utf8',
      'indent' => true,
      'indent-spaces' => 2,
      'tidy-mark' => false,
      'wrap' => 0 }
          
    # Tidy gem options, see http://tidy.sourceforge.net/docs/quickref.html
    attr_accessor :tidy_options
    
    # Paths to be ignored during Rack::Tidy processing
    attr_accessor :ignore_paths
                
    def initialize(app, options = {})
      ::Tidy.path = TIDY_LIB
      @app = app
      self.ignore_paths = options[:ignore_paths] || []
      self.tidy_options = DEFAULT_TIDY_OPTS.merge(options)
    end
    
    # method required by Rack interface
    def call(env)
      call! env
    end
 
    # thread safe version using shallow copy of env
    def call!(env)
      @env = env.dup
      status, @headers, response = @app.call(@env)
      if should_clean?
        @headers.delete('Content-Length')
        response = Rack::Response.new(
          tidy_markup(response.respond_to?(:body) ? response.body : response),
          status,
          @headers
        )
        response.finish
        response.to_a
      else
        [status, @headers, response]
      end
    end
    
    private
              
      def should_clean? #:nodoc:
        @headers["Content-Type"] &&
        @headers["Content-Type"].include?("text/html") &&
        self.ignore_paths.none? { |p| @env["PATH_INFO"].start_with?(p) }
      end
      
      def tidy_markup(content) #:nodoc:
        ::Tidy.open(self.tidy_options) { |tidy| tidy.clean(content) }
      end
  end
end
tidy.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# gem available at http://github.com/webficient/rack-tidy
# source code extracted from gem:
 
require 'rack'
 
# = Tidy Markup Cleaner for Rack
#
# Rack::Tidy cleans text/html markup by automatically
# indenting and reformatting content. Best results are achieved
# with valid markup, when you simply want to use this component
# to produce clean (X)HTML rendered by templating systems such as ERb.
#
# Rack::Tidy relies on the power of the Tidy gem and defaults to settings
# based on convention. However, you can override these through configuration.
#
# === Usage
#
# Within a rackup file (or with Rack::Builder):
# require 'rack/tidy'
# use Rack::Tidy,
# :ignore_paths => ['/admin', '/cms'],
# 'indent-spaces' => 4
# run app
#
# Rails example:
# # above Rails::Initializer block
# require 'rack/tidy'
#
# # inside Rails::Initializer block
# config.middleware.use Rack::Tidy,
# :ignore_paths => ['/admin', '/cms'],
# 'indent-spaces' => 4
module Rack::Tidy
  autoload :Cleaner, 'rack/tidy/cleaner'
  
  # Specify path of Tidy library, e.g.
  # "/usr/lib/libtidy.A.dylib" (Mac; also the default if not set)
  # "/usr/lib/tidylib.so" (Ubuntu)
  # "/usr/lib/libtidy-0.99.so.0" (Fedora/CentOS)
  TIDY_LIB = defined?(::TIDY_LIB) ? ::TIDY_LIB : '/usr/lib/libtidy.A.dylib'
  
  # Create a new Rack::Tidy middleware component that cleans text/html markup
  # using the Tidy gem. The +options+ Hash can be used to specify which paths
  # should be ignored during processing as well as Tidy gem configuration values.
  # See Cleaner for defaults and http://tidy.sourceforge.net/docs/quickref.html
  # for more options
  def self.new(backend, options = {})
    Cleaner.new(backend, options)
  end
end