| Class | RailsTidy |
| In: |
lib/rails_tidy.rb
|
| Parent: | Object |
| IGNORED | = | [ /<a> escaping malformed URI reference/, /attribute "id" has invalid value "<%=.*?%>"/, /<link> escaping malformed URI reference/, /<div> anchor "<%=.*?%>" already defined/ |
| MASK | = | /\.(rhtml|html\.erb)$/ |
filter the body of a response
use this method in an after_filter. Example:
after_filter :tidy
def tidy
RailsTidy.filter(response)
end
# 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
path to tidy configuration file, set to config/tidy.rc by default
# File lib/rails_tidy.rb, line 52
52: def self.tidy_configuration=(tidy_configuration)
53: @@tidy_configuration = tidy_configuration
54: end
path to the tidy library
# File lib/rails_tidy.rb, line 42
42: def self.tidy_path=(tidy_path)
43: @@tidy_path = tidy_path
44: end
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
# 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
# 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
# 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