Created
July 26, 2012 13:13
-
-
Save mindreframer/3181974 to your computer and use it in GitHub Desktop.
html/erb prettifying stuff
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'timeout' | |
require 'htmlbeautifier' | |
def log(msg) | |
puts msg | |
end | |
view_files = Dir["app/**/**/**.erb"] | |
def beautify(file_name) | |
log "doing #{file_name}" | |
output = "" | |
input = File.read(file_name) | |
begin | |
HtmlBeautifier::Beautifier.new(output).scan(input) | |
rescue | |
puts "ERROR on #{file_name}" | |
end | |
return output | |
end | |
view_files[0..5].each do |f| | |
puts beautify(f) | |
end; nil | |
####### | |
ps -ef|grep html|awk '{print $2}'|xargs kill -9 | |
########### | |
https://github.com/victorporof/Sublime-HTMLPrettify/blob/master/scripts/beautify-html.js | |
### with gsubbing | |
http://www.ruby-forum.com/topic/115982 | |
### | |
http://railscasts.com/episodes/286-draper?view=asciicast (view cleanup) | |
### Sounds very promising... (convert haml --> erb) + pretty-print erb | |
https://github.com/railsdog/deface | |
https://github.com/railsdog/deface/blob/master/lib/deface/applicator.rb | |
class PrettyHtml | |
require 'tidy' | |
require 'deface' | |
begin | |
Tidy.path = '/usr/lib/libtidy-0.99.so.0' | |
rescue LoadError | |
Tidy.path = '/usr/lib/libtidy.A.dylib' | |
end | |
def self.tidy_html(text) | |
nice_html = "" | |
Tidy.open(:show_warnings=>true) do |tidy| | |
tidy.options.show_body_only = true | |
tidy.options.doctype = "omit" | |
tidy.options.add_xml_decl = false | |
tidy.options.output_html = true | |
tidy.options.wrap = 0 | |
tidy.options.indent = 'auto' | |
tidy.options.indent_attributes = false | |
tidy.options.indent_spaces = 2 | |
tidy.options.vertical_space = false | |
tidy.options.char_encoding = 'utf8' | |
nice_html = tidy.clean(text) | |
end | |
nice_html = nice_html.strip.gsub(/\n+/, "\n") | |
end | |
def self.replace_erb_with_tags(input) | |
Deface::Parser.convert(input).to_s | |
end | |
def self.replace_tags_with_erb(input) | |
Deface::Parser.undo_erb_markup!(input) | |
end | |
def self.convert_file(file_path) | |
orig = File.read(file_path) | |
erb_as_tags = PrettyHtml.replace_erb_with_tags(orig) | |
clean_html = PrettyHtml.tidy_html(erb_as_tags) | |
clean_erb = PrettyHtml.replace_tags_with_erb(clean_html) | |
end | |
end | |
view_files = Dir["app/**/**/**.erb"]; nil | |
file = view_files[2] | |
puts PrettyHtml.convert_file(file) | |
file_path = file | |
orig = File.read(file_path) | |
erb_as_tags = PrettyHtml.replace_erb_with_tags(orig) | |
clean_html = PrettyHtml.tidy_html(erb_as_tags) | |
clean_erb = PrettyHtml.replace_tags_with_erb(clean_html) | |
puts CGI.unescapeHTML(clean_erb) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment