Class RailsTidy
In: lib/rails_tidy.rb
Parent: Object

RailsTidy

class use to validate html in templates and in http response

Methods

Constants

IGNORED = [ /<a> escaping malformed URI reference/, /attribute "id" has invalid value "<%=.*?%>"/, /<link> escaping malformed URI reference/, /<div> anchor "<%=.*?%>" already defined/
MASK = /\.(rhtml|html\.erb)$/

Public Class methods

filter the body of a response

use this method in an after_filter. Example:

  after_filter :tidy
  def tidy
    RailsTidy.filter(response)
  end

[Source]

     # File lib/rails_tidy.rb, line 164
164:   def self.filter(response)
165:     tidy = tidy_factory
166:     response.body = tidy.clean(response.body)
167:     tidy.errors.each { |error| logger.debug("Tidy: #{error}") }
168:     tidy.release
169:   end

[Source]

    # File lib/rails_tidy.rb, line 62
62:   def initialize
63:     @tidy = RailsTidy.tidy_factory
64:   end

path of the file or directory to validate

if path is a file, it will be validated using tidy, if it is a directory, it will search it for .rhtml file and validate all those files.

[Source]

    # File lib/rails_tidy.rb, line 37
37:   def self.path=(path)
38:     @@path = path
39:   end

validate templates in path

[Source]

    # File lib/rails_tidy.rb, line 57
57:   def self.run
58:     td = self.new
59:     td.validate
60:   end

path to tidy configuration file, set to config/tidy.rc by default

[Source]

    # File lib/rails_tidy.rb, line 52
52:   def self.tidy_configuration=(tidy_configuration)
53:     @@tidy_configuration = tidy_configuration
54:   end

return a new tidy instance

[Source]

     # File lib/rails_tidy.rb, line 172
172:   def self.tidy_factory
173:     Tidy.path = @@tidy_path
174:     tidy = Tidy.new
175:     tidy.load_config(@@tidy_configuration) if File.exists?(@@tidy_configuration)
176:     tidy
177:   end

[Source]

    # File lib/rails_tidy.rb, line 46
46:   def self.tidy_path
47:     @@tidy_path
48:   end

path to the tidy library

[Source]

    # File lib/rails_tidy.rb, line 42
42:   def self.tidy_path=(tidy_path)
43:     @@tidy_path = tidy_path
44:   end

Public Instance methods

format the response

this method add doctype and missing tag so that when a a templates miss doctypes or html tag, tidy does not complain

[Source]

     # File lib/rails_tidy.rb, line 133
133:   def format_content(content)
134:     # add doctype if not found
135:     validable = ""
136:     unless content.match(/<!DOCTYPE/)
137:       validable << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" " +
138:         " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
139:     end
140: 
141:     # add html, head, title and body 
142:     unless content.match(/<html/)
143:       validable << "<html><head><title>rake test_templates</title></head><body>#{content}</body></html>"
144:     else
145:       validable << content
146:     end
147: 
148:     # replace start_form_tag and end_form_tag with <form> and </form>
149:     validable.gsub!(/<%=\s*start_form_tag.*?%>/, "<form action=\"test\">")
150:     validable.gsub!(/<%=\s*form_remote_tag.*?%>/, "<form action=\"test\">")
151:     validable.gsub!(/<%=\s*end_form_tag.*?%>/, "</form>")
152:     return validable
153:   end

log tidy output

[Source]

     # File lib/rails_tidy.rb, line 109
109:   def log_errors
110:     # remove errors that are to be ignored
111:     @tidy.errors.delete_if do |error|
112:       IGNORED.inject(false) { |b,r| b ||= error.message.match(r) }
113:     end
114: 
115:     # display errors
116:     unless @tidy.errors.empty?
117:       puts "ERRORS"
118:       File.open(log_file, "w") do |log|
119:         log.puts @tidy.errors
120:         log.puts
121:         log.puts @tidy.diagnostics
122:       end
123:     else
124:       File.delete(log_file) if File.exists?(log_file)
125:       puts "OK"
126:     end
127:   end

validate all templates in path

[Source]

    # File lib/rails_tidy.rb, line 67
67:   def validate
68:     if File.directory?(@@path)
69:       Find.find(@@path) do |file|
70:         if file.match(MASK) 
71:           @file = file
72:           validate_file
73:         end
74:       end
75:     else
76:       if File.file?(@@path)
77:         @file = @@path
78:       elsif File.file?("#{@@path}.rhtml")
79:         @file = "#{@@path}.rhtml"
80:       elsif File.file?("#{@@path}.html.erb")
81:         @file = "#{@@path}.html.erb"
82:       elsif File.file?(File.join(RAILS_ROOT, "app", "views", @@path))
83:         @file = File.join(RAILS_ROOT, "app", "views", @@path)
84:       elsif File.file?(File.join(RAILS_ROOT, "app", "views", @@path) + ".rhtml")
85:         @file = File.join(RAILS_ROOT, "app", "views", @@path) + ".rhtml"
86:       elsif File.file?(File.join(RAILS_ROOT, "app", "views", @@path) + ".html.erb")
87:         @file = File.join(RAILS_ROOT, "app", "views", @@path) + ".html.erb"
88:       end
89:       validate_file
90:       puts @tidy.errors
91:       puts @tidy.diagnostics
92:     end
93:   end

validate a single file

[Source]

     # File lib/rails_tidy.rb, line 96
 96:   def validate_file
 97:     printf "%-70s", @file
 98:     
 99:     content = format_content(IO.read(@file))
100: 
101:     # validate
102:     @tidy.clean(content)
103: 
104:     # and log erros
105:     log_errors 
106:   end

[Validate]