Change minifier calling

This commit is contained in:
JoseluCross
2017-06-29 19:14:07 +02:00
parent 15898d10e5
commit f39594aa63
2 changed files with 37 additions and 26 deletions

View File

@@ -1,3 +0,0 @@
{
"supported": ["js","css","htm","html","twig"]
}

View File

@@ -1,19 +1,20 @@
#!/usr/bin/python3
import os
import json
from sys import argv as _
from shutil import copyfile
from jsmin import jsmin
from rcssmin import cssmin
VERSION="0.1.0.0"
FORMATS={}
with open('../data/supported.json') as data:
FORMATS = json.load(data)
VERSION = "0.1.1.0"
verbose = False
FORMATS = {
"supported": ["js", "css", "htm", "html", "twig"]
}
def copyStructure(originalPath, finalPath):
"""Copy folder structure and start copy and minimize"""
if not os.path.isdir(originalPath):
raise Exception("The origin directory " + originalPath + " not exists")
@@ -27,57 +28,68 @@ def copyStructure(originalPath, finalPath):
def startCopy(origin, final):
"""Copy files which do not support minifer"""
originList = os.listdir(origin)
for i in originList:
part = i.split('.')
if os.path.isdir(origin+"/"+i):
if verbose:
print(final + "/" + i + " has been created")
os.makedirs(final+"/"+i)
startCopy(origin+"/"+i, final+"/"+i)
elif not part[len(part)-1] in FORMATS["supported"]:
copyfile(origin+"/"+i, final+"/"+i)
if verbose:
print(origin + "/" + i + " has been copied")
else:
minimizeFile(origin+"/"+i,final+"/"+i,part[len(part)-1])
def minimizeFile(source, production, format):
"""Minify files"""
input = open(source, "r")
output = open(production, "w")
if format == "js":
output.write(jsmin(input.read()))
if format == "css":
output.write(cssmin(input.read(), keep_bang_comments=False))
if format == "html" or format == "htm" or format == "twig":
output.write(htmlmin(input.read()))
output.write(globals()[format+"min"](input.read()))
input.close()
output.close()
if verbose:
print(source + " has been minified")
def htmlmin(source):
ret=""
codePre=False #Is inside a code or pre label
lines=source.split("\n")
"""Minify html"""
ret = ""
codePre = False #Is inside a code or pre label
lines = source.split("\n")
for i in lines:
if "<!DOCTYPE" in i:
ret=i+"\n"
ret = i+"\n"
else:
if "</pre>" in i or "</code>" in i:
codePre=False
codePre = False
elif "<pre>" in i or "<code>" in i:
codePre=True
codePre = True
if codePre:
ret+="\n"+i
ret += "\n"+i
else:
index=0
index = 0
for j in i:
if j != " " and j != "\t":
break
else:
index+=1
i=i[index:]
ret+=i
index += 1
i = i[index:]
ret += i
return ret
def htmmin(source):
"""Minify htm"""
return htmlmin(source)
def twigmin(source):
"""Minify twig"""
return htmlmin(source)
def main():
"""Main activity"""
if len(_) == 1 or _[1] == "-h":
help()
elif _[1] == "-v":
@@ -88,6 +100,7 @@ def main():
help()
def help():
"""Help"""
print("Usage of minithunder:")
print("---------------------")
print("-h: show help")
@@ -95,6 +108,7 @@ def help():
print("-m <Source path> <Production path>: Minimize project to production")
def version():
"""version"""
print("MiniThunder - Prepare your software to production")
print("Version: "+VERSION)