Skip to content

Instantly share code, notes, and snippets.

@mindreframer
Created July 26, 2012 01:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindreframer/3179698 to your computer and use it in GitHub Desktop.
Save mindreframer/3179698 to your computer and use it in GitHub Desktop.
Cleanup views with tidy (erb)
require 'tidy'
#Tidy.path = '/opt/local/lib/libtidy.dylib' # or where ever your tidylib resides
Tidy.path = '/usr/lib/libtidy.A.dylib'
def generate(files)
tidy = Tidy.open(:show_warnings=>true) do |tidy|
tidy.options.indent = 'auto'
tidy.options.show_body_only = true
tidy.options.output_xhtml = true
tidy.options.wrap = 0
tidy.options.indent_spaces = 2
tidy.options.indent_attributes = false
tidy.options.vertical_space = false
tidy.options.char_encoding = 'utf8'
files.each do |f|
template = File.read(f)
xml = tidy.clean(template)
xml = xml.strip.gsub(/\n+/, "\n")
puts xml
File.open(f, 'w') do |out|
out.puts(xml)
end
end; nil
end
end
view_files = Dir["app/**/**/**.erb"]
generate(view_files)
### find app/ -type f -name "*.erb" -exec tidy -m -utf8 -i -ashtml {} \;
#!/usr/bin/env ruby
# http://blog.neontology.com/articles/2006/05/10/beautiful-ruby-in-textmate
# http://www.arachnoid.com/ruby/rubyBeautifier.html
#
# Ruby beautifier, version 1.3, 04/03/2006
# Copyright (c) 2006, P. Lutus
# TextMate modifications by T. Burks
# Vim modifications by Luis Mondesi
# Released under the GPL
$tabSize = 2
$tabStr = " "
# indent regexp tests
$indentExp = [
/^\<[^\/%]*\>$/,
/^\<%[^=]* do .*%\>$/,
/^\<%[^=]* if .*%\>$/,
/^\<%[^=]* unless .*%\>$/,
/^\<%[^=]*\{[^\}]*%\>$/
]
# outdent regexp tests
$outdentExp = [
/^\<\/.*\>$/,
/^\<%.* end .*%\>$/,
/^\<%.*\}.*%\>$/
]
def makeTab(tab)
return (tab < 0)? "":$tabStr * $tabSize * tab
end
def addLine(line,tab)
line.strip!
line = makeTab(tab)+line if line.length > 0
return line + "\n"
end
def beautifyERB(source)
commentBlock = false
multiLineArray = Array.new
multiLineStr = ""
tab = 0
dest = ""
source.split("\n").each do |line|
# combine continuing lines
if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
multiLineArray.push line
multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
next
end
# add final line
if(multiLineStr.length > 0)
multiLineArray.push line
multiLineStr += line.sub(/^(.*)\\\s*$/,"\\1")
end
tline = ((multiLineStr.length > 0)?multiLineStr:line).strip
if(tline =~ /^=begin/)
commentBlock = true
end
if(commentBlock)
# add the line unchanged
dest += line + "\n"
else
commentLine = (tline =~ /^#/)
if(!commentLine)
# throw out sequences that will
# only sow confusion
$outdentExp.each do |re|
if(tline =~ re)
tab -= 1
break
end
end
end
if (multiLineArray.length > 0)
multiLineArray.each do |ml|
dest += addLine(ml,tab)
end
multiLineArray.clear
multiLineStr = ""
else
dest += addLine(line,tab)
end
if(!commentLine)
$indentExp.each do |re|
if(tline =~ re && !(tline =~ /\s+end\s*$/))
tab += 1
break
end
end
end
end
if(tline =~ /^=end/)
commentBlock = false
end
end
dest
end
def generate(files)
files.each do |f|
template = File.read(f)
ready_html = beautifyERB(template)
File.open(f, 'w') do |out|
out.puts(ready_html)
end
end; nil
end
view_files = Dir["app/**/**/**.erb"]
generate(view_files)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment