From f39594aa63376541f68920cc497f5c6446372798 Mon Sep 17 00:00:00 2001 From: JoseluCross Date: Thu, 29 Jun 2017 19:14:07 +0200 Subject: [PATCH] Change minifier calling --- data/supported.json | 3 --- src/minithunder.py | 60 ++++++++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 26 deletions(-) delete mode 100644 data/supported.json diff --git a/data/supported.json b/data/supported.json deleted file mode 100644 index 770b077..0000000 --- a/data/supported.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "supported": ["js","css","htm","html","twig"] -} \ No newline at end of file diff --git a/src/minithunder.py b/src/minithunder.py index bc041fa..60f25b0 100644 --- a/src/minithunder.py +++ b/src/minithunder.py @@ -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 "" in i or "" in i: - codePre=False + codePre = False elif "
" in i or "" 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  :          Minimize project to production")
 
 def version():
+    """version"""
     print("MiniThunder - Prepare your software to production")
     print("Version: "+VERSION)