Last active
August 29, 2015 14:24
-
-
Save eric-cc-su/ebf75ba3be97896b9dab to your computer and use it in GitHub Desktop.
Cumin (Cu)stom (min)ifier for GitHub Pages site constructed by Jekyll. This is a simple minifier for javascript. Still a work in progress
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
=begin | |
author: eric-cc-su | |
Cumin (Cu)stom (min)ifier | |
This Ruby script/plugin is meant to minify files that should be minified prior to website deployment. | |
This plugin will ultimately support simple minification of javascript and css files. | |
This code is licensed under the MIT License (end of file) and under copyright by Eric Su | |
=end | |
require 'fileutils' | |
module Cumin | |
$base = Dir.pwd | |
@list = [] # Compilation of all scripts | |
comment_open = false | |
leadstrip = true | |
no_min = false | |
trailstrip = true | |
dpath = File.join($base, "javascripts") | |
if not File.directory?(File.join($base, "js")) | |
FileUtils.mkdir_p(File.join($base, "js")) | |
end | |
Dir.foreach(dpath) { | |
|file| | |
if file.end_with?(".js") and not file.end_with?(".min.js") | |
newfile = file.dup.sub! ".js", ".min.js" | |
File.open(File.join($base, "js", newfile), "w+") do |jsminfile| | |
File.foreach(File.join($base, "javascripts", file)) do |line| | |
line = line.strip | |
#Indicates that following code should not be minified | |
if line == "/*NOMIN*/" | |
no_min = true; | |
next | |
end | |
#Toggle whether spaces in front of special characters are stripped | |
if line == "/*LEADSTRIP*/" | |
leadstrip ^= 1 | |
end | |
#Toggle whether spaces behind special characters are stripped | |
if line == "/*TRAILSTRIP*/" | |
trailstrip ^= 1 | |
end | |
#Indicates that following code should be continued to be minified | |
if no_min and line != "/*YESMIN*/" | |
jsminfile.write(line) | |
next | |
elsif line == "/*YESMIN*/" | |
no_min = false | |
next | |
end | |
if line[0..1] == "//" # Remove comments | |
next | |
elsif line.include?("/*") | |
comment_open = true | |
open_index = line.index("/*") | |
if line.include?("*/") | |
close_index = line.index("*/")+1 | |
comment_open = false | |
else | |
close_index = -1 | |
line = line.sub! line[open_index..close_index], "" | |
end | |
elsif line.include?("*/") and comment_open | |
comment_open = false | |
line = line.sub! line[0..line.index("*/")+1], "" | |
end | |
# Strip spaces in front of special characters | |
if leadstrip | |
leading_space = /\s[^A-Za-z0-9"'\s]/.match(line) | |
while leading_space != nil | |
line = line.sub! leading_space.to_s, leading_space.to_s.strip | |
leading_space = /\s[^A-Za-z0-9"'\s]/.match(line) | |
end | |
end | |
# Strip spaces following special characters | |
if trailstrip | |
trailing_space = /[^A-Za-z0-9"'\s]\s/.match(line) | |
while trailing_space != nil | |
line = line.sub! trailing_space.to_s, trailing_space.to_s.strip | |
trailing_space = /[^A-Za-z0-9"'\s]\s/.match(line) | |
end | |
end | |
if comment_open | |
next | |
end | |
jsminfile.write(line) | |
end | |
end | |
end | |
} | |
end | |
=begin | |
The MIT License (MIT) | |
Copyright (c) 2015 Eric Su | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment