Created
January 13, 2014 13:34
-
-
Save kt0/8400321 to your computer and use it in GitHub Desktop.
Remove extra whitespace and comments from html templates and create a single JS and RB file
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
#! /usr/bin/env ruby | |
Js = 'public/static/js/_tpl.js' | |
Rb = 'config/initializers/templates.rb' | |
files = Dir.glob('templates/**/*.html') | |
templates = {} | |
total_size = 0 | |
count = 0 | |
files.each do |file| | |
template = (File.read file).gsub(/>\s+</,'><').gsub("'","\\'").gsub("\n",'').gsub(/<!--(.|\s)*?-->/, '') | |
name = file.gsub('.html','').gsub('templates/','') | |
templates[name] = template | |
# puts "#{name} : #{File.size(file) - template.length}" | |
total_size += File.size file | |
count += 1 | |
end | |
File.delete(Js) if File.exists? Js | |
File.delete(Rb) if File.exists? Rb | |
File.open Js, 'w' do |js| | |
js.write "window._tpl={" | |
i = 0 | |
templates.each do |name,tpl| | |
i != 0 ? js.write(',') : i=1 | |
js.write "'#{name}':'#{tpl}'" | |
end | |
js.write "};" | |
end | |
puts "Minified (#{count}) html files with total size of (#{total_size}) to (1) js file with size of (#{File.size(Js)})" | |
File.open Rb, 'w' do |rb| | |
rb.write "Templates = {" | |
i = 0 | |
templates.each do |name,tpl| | |
i != 0 ? rb.write(',') : i=1 | |
rb.write "\n " | |
rb.write "'#{name}' => '#{tpl}'" | |
end | |
rb.write "\n}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment