Created
September 30, 2009 06:59
-
-
Save ryanmcgrath/197878 to your computer and use it in GitHub Desktop.
This file contains 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/ruby | |
require "optparse" | |
# Impact v0.1 | |
# @Author: Ryan McGrath | |
# @Email: ryan [at] venodesigns.net | |
# Impact is a Ruby script to strip out spaces and useless junk from CSS and Javascript files. It doesn't | |
# obfuscate the code in any manner, it simply kills all whitespace and newlines. Very simple... | |
# A String that gets built on, and is eventually tossed into our new file. | |
finalCode = "" | |
# Crash if we don't get the arguments we want | |
unless ARGV.length >= 1 | |
puts "You realize that you called this without passing any files, right?" | |
puts "Usage: ruby impact.rb <filenames>" | |
exit | |
end | |
# Loop through the files passed in from the command line | |
ARGV.each do |filename| | |
text = File.new(filename, "r") | |
text.readlines.each do |textline| | |
if textline.index('#') != 0 | |
temporaryCodeHolder = Array.new | |
# This could be horribly intensive - benchmark this at some point | |
textline.each_byte do |c| | |
# Manually compare and check for tabs (This might grow, .chomp doesn't catch certain things) | |
temporaryCodeHolder << c.chr if c != 9 || c == 10 || c == 13 | |
end | |
finalCode += temporaryCodeHolder.join.chomp | |
end | |
end | |
end | |
# Strip out spaces (This could become more intensive once we start parsing JS) | |
# We use a regular expression, as it seems to be the fastest way to remove spaces | |
# across various Ruby distributions. String.squeeze and such tend to get slower | |
# depending on the environment you're running in, oddly enough. | |
finalCode.gsub!(/ +/, '') | |
# Create our new file, which will hold the minified and concatenated code, and toss it in | |
impactedFile = File.open("impact_minified_javascript.js", "w") | |
impactedFile.puts finalCode |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment