From 4fbe411b13ee996dacc03abfb4284134b18572e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 16 Jun 2018 03:16:42 +0200 Subject: [PATCH] update css_compress --- data/css_compress/compress.py | 85 +++++++++++++++++++++++++++++++---- data/css_compress/scss | 2 +- data/css_compress/scss.bat | 6 --- 3 files changed, 77 insertions(+), 16 deletions(-) delete mode 100644 data/css_compress/scss.bat diff --git a/data/css_compress/compress.py b/data/css_compress/compress.py index 2cc7a91..cdcbaeb 100644 --- a/data/css_compress/compress.py +++ b/data/css_compress/compress.py @@ -24,10 +24,24 @@ def countnl(str, start, end): if (str[i] == '\n'): cnt = cnt+1; return cnt; +def comment_remover(text): + def replacer(match): + s = match.group(0) + if s.startswith('/'): + return " " # note: a space and not an empty string + else: + return s + pattern = re.compile( + r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"', + re.DOTALL | re.MULTILINE + ) + return re.sub(pattern, replacer, text) + fsource = str.replace(sys.argv[1], '\\', '/') # scss finput = str.replace(sys.argv[2], '\\', '/') # css foutput = str.replace(sys.argv[3], '\\', '/') # min.css -ftemp = '__temp_compresss_py_yui.tmp.css'; +ftemp1 = '__temp_compresss_py_1.tmp.css'; +ftemp2 = '__temp_compresss_py_2.tmp.css'; print('======== INPUT ========'); print(); @@ -37,9 +51,30 @@ print(foutput); print(); print(); +print('======== DELETE OLD DATA ========'); +if os.path.isfile(finput): + try: + os.remove(finput); + print(finput + ' deleted') + except e: + print(e) +else: + print(finput + ' does not exist') +if os.path.isfile(foutput): + try: + os.remove(foutput); + print(foutput + ' deleted') + except e: + print(e) +else: + print(foutput + ' does not exist') +print(); +print(); + + print('======== CALL SCSS ========'); -out = subprocess.run(['scss.bat', '--no-cache', '--update', fsource + ':' + finput], stdout=subprocess.PIPE, stderr=subprocess.PIPE) -print('> scss.bat --no-cache --update ' + fsource + ':' + finput) +out = subprocess.run(['ruby', 'scss', '--style=expanded', '--no-cache', '--update', fsource + ':' + finput], stdout=subprocess.PIPE, stderr=subprocess.PIPE) +print('> scss.bat --style=expanded --no-cache --update ' + fsource + ':' + finput) print('STDOUT:') print(out.stdout.decode('utf-8')) print('STDERR:') @@ -48,10 +83,25 @@ print(out.stderr.decode('utf-8')) print('') print('') +print('======== CLEANUP COMMENTS ========'); +with open(finput, 'r') as tf: + data1 = tf.read() + print(str(len(data1)) + ' characters read from ' + os.path.basename(finput)) + +data1 = comment_remover(data1) +print('Comments in css removed'); + +with open(ftemp1, "w") as tf: + tf.write(data1) + print(str(len(data1)) + ' characters written to ' + ftemp1) + +print('') +print('') + print('======== CALL YUI ========'); -out = subprocess.run(['java', '-jar', 'yuicompressor.jar', '--verbose', finput, '-o', ftemp], stdout=subprocess.PIPE, stderr=subprocess.PIPE) -print('> java -jar yuicompressor.jar --verbose "'+finput+'" -o "'+ftemp+'"') +out = subprocess.run(['java', '-jar', 'yuicompressor.jar', '--verbose', ftemp1, '-o', ftemp2], stdout=subprocess.PIPE, stderr=subprocess.PIPE) +print('> java -jar yuicompressor.jar --verbose "'+finput+'" -o "'+ftemp2+'"') print('STDOUT:'); print(out.stdout.decode('utf-8')) print('STDERR:'); @@ -61,17 +111,19 @@ print('') print('') print('======== READ ========'); -with open(ftemp, 'r') as tf: +with open(ftemp2, 'r') as tf: data = tf.read() - print(str(len(data)) + ' characters read from ' + ftemp) + print(str(len(data)) + ' characters read from ' + ftemp2) print('') print('') print('======== REM ========'); try: - os.remove(ftemp); - print(ftemp + ' deleted') + os.remove(ftemp1); + print(ftemp1 + ' deleted') + os.remove(ftemp2); + print(ftemp2 + ' deleted') except e: print(e) @@ -118,4 +170,19 @@ with open(foutput, "w") as tf: print('') print('') + +print('======== REMOVE MAP ========'); +if os.path.isfile(finput + '.map'): + try: + os.remove(finput + '.map'); + print(finput + '.map' + ' deleted') + except e: + print(e) +else: + print(finput + '.map' + ' does not exist') + +print('') +print('') + + print('Finished.') diff --git a/data/css_compress/scss b/data/css_compress/scss index 919dccd..216df15 100644 --- a/data/css_compress/scss +++ b/data/css_compress/scss @@ -8,7 +8,7 @@ require 'rubygems' -version = ">= 0.a" +version = ">= 3.a" if ARGV.first str = ARGV.first diff --git a/data/css_compress/scss.bat b/data/css_compress/scss.bat deleted file mode 100644 index 30fa211..0000000 --- a/data/css_compress/scss.bat +++ /dev/null @@ -1,6 +0,0 @@ -@ECHO OFF -IF NOT "%~f0" == "~f0" GOTO :WinNT -@"C:\TOOLS\Ruby\bin\ruby.exe" "C:/TOOLS/Ruby/bin/scss" %1 %2 %3 %4 %5 %6 %7 %8 %9 -GOTO :EOF -:WinNT -@"C:\TOOLS\Ruby\bin\ruby.exe" "%~dpn0" %*