1
0
www.mikescher.com/data/css_compress/compress.py

122 lines
2.8 KiB
Python
Raw Permalink Normal View History

2018-01-01 21:07:48 +01:00
#!/usr/bin/env python3
import sys
import os
from subprocess import call
import re
import subprocess
def findnext(str, start, chr):
depth = 0
for i in range(start, len(str)):
if (str[i] == chr): return i;
def findclose(str, start):
depth = 0
for i in range(start, len(str)):
if (str[i] == '{'): depth = depth+1;
if (str[i] == '}'): depth = depth-1;
if (depth == 0): return i;
def countnl(str, start, end):
cnt = 0
for i in range(start, end+1):
if (str[i] == '\n'): cnt = cnt+1;
return cnt;
2018-01-19 22:49:46 +01:00
fsource = str.replace(sys.argv[1], '\\', '/') # scss
finput = str.replace(sys.argv[2], '\\', '/') # css
foutput = str.replace(sys.argv[3], '\\', '/') # min.css
2018-01-01 21:07:48 +01:00
ftemp = '__temp_compresss_py_yui.tmp.css';
print('======== INPUT ========');
print();
2018-01-19 22:49:46 +01:00
print(fsource);
2018-01-01 21:07:48 +01:00
print(finput);
print(foutput);
print();
print();
2018-01-19 22:49:46 +01:00
print('======== CALL SCSS ========');
2018-01-20 03:05:15 +01:00
out = subprocess.run(['scss.bat', '--no-cache', '--update', fsource + ':' + finput], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print('> scss.bat --no-cache --update ' + fsource + ':' + finput)
2018-01-19 22:49:46 +01:00
print('STDOUT:')
print(out.stdout.decode('utf-8'))
print('STDERR:')
print(out.stderr.decode('utf-8'))
print('')
print('')
2018-01-01 21:07:48 +01:00
2018-01-19 22:49:46 +01:00
print('======== CALL YUI ========');
2018-01-20 03:05:15 +01:00
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+'"')
2018-01-01 21:07:48 +01:00
print('STDOUT:');
print(out.stdout.decode('utf-8'))
print('STDERR:');
print(out.stderr.decode('utf-8'))
2018-01-19 22:49:46 +01:00
print('')
print('')
2018-01-01 21:07:48 +01:00
print('======== READ ========');
with open(ftemp, 'r') as tf:
data = tf.read()
2018-01-20 03:05:15 +01:00
print(str(len(data)) + ' characters read from ' + ftemp)
2018-01-01 21:07:48 +01:00
2018-01-19 22:49:46 +01:00
print('')
print('')
2018-01-01 21:07:48 +01:00
print('======== REM ========');
try:
os.remove(ftemp);
2018-01-20 03:05:15 +01:00
print(ftemp + ' deleted')
2018-01-01 21:07:48 +01:00
except e:
print(e)
2018-01-19 22:49:46 +01:00
print('')
print('')
2018-01-01 21:07:48 +01:00
print('======== REGEX ========');
data = re.sub(r'(\}*\})', '\g<1>\n', data);
2018-01-20 03:05:15 +01:00
print('css data modified (1)')
2018-01-19 22:49:46 +01:00
print('')
print('')
2018-01-01 21:07:48 +01:00
print('======== MEDIA ========');
ins = []
for i in range(len(data)):
if data[i:].startswith('@media'):
copen = findnext(data, i, '{')
cclose = findclose(data, copen)
if (countnl(data, copen, cclose) == 0): continue;
ins.append( (copen+1, '\n\t') )
for i in range(copen+1, cclose):
if data[i] == '\n':
tp =(i+1, '\t')
ins.append( tp )
ins.append((cclose, '\n'))
2018-01-20 03:05:15 +01:00
print('media query at idx:' + str(i) + ' formatted')
2018-01-01 21:07:48 +01:00
for (l, c) in reversed(ins):
data = data[:l] + c + data[l:]
2018-01-19 22:49:46 +01:00
print('')
print('')
2018-01-01 21:07:48 +01:00
print('======== WRITE ========');
with open(foutput, "w") as tf:
2018-01-19 22:49:46 +01:00
tf.write(data)
2018-01-20 03:05:15 +01:00
print(str(len(data)) + ' characters written to ' + foutput)
2018-01-19 22:49:46 +01:00
print('')
print('')
2018-01-20 03:05:15 +01:00
print('Finished.')