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

190 lines
4.4 KiB
Python
Raw Normal View History

2018-01-01 21:07:48 +01:00
#!/usr/bin/env python3
import sys
import os
import re
import subprocess
2020-08-10 22:25:43 +02:00
import shutil
2018-01-01 21:07:48 +01:00
2020-01-16 10:07:14 +01:00
def findnext(strdata, start, searchchr):
2018-01-01 21:07:48 +01:00
depth = 0
2020-01-16 10:07:14 +01:00
for i in range(start, len(strdata)):
if strdata[i] == searchchr: return i;
2018-01-01 21:07:48 +01:00
2020-01-16 10:07:14 +01:00
def findclose(strdata, start):
2018-01-01 21:07:48 +01:00
depth = 0
2020-01-16 10:07:14 +01:00
for i in range(start, len(strdata)):
if strdata[i] == '{': depth = depth + 1;
if strdata[i] == '}': depth = depth - 1;
if depth == 0: return i;
2018-01-01 21:07:48 +01:00
2020-01-16 10:07:14 +01:00
def countnl(strdata, start, end):
2018-01-01 21:07:48 +01:00
cnt = 0
2020-01-16 10:07:14 +01:00
for i in range(start, end + 1):
if strdata[i] == '\n': cnt = cnt + 1;
return cnt
2018-01-01 21:07:48 +01:00
2018-06-16 03:16:42 +02:00
def comment_remover(text):
def replacer(match):
s = match.group(0)
if s.startswith('/'):
2020-01-16 10:07:14 +01:00
return " " # note: a space and not an empty string
2018-06-16 03:16:42 +02:00
else:
return s
2020-01-16 10:07:14 +01:00
2018-06-16 03:16:42 +02:00
pattern = re.compile(
r'//.*?$|/\*.*?\*/|\'(?:\\.|[^\\\'])*\'|"(?:\\.|[^\\"])*"',
re.DOTALL | re.MULTILINE
)
return re.sub(pattern, replacer, text)
2018-01-19 22:49:46 +01:00
fsource = str.replace(sys.argv[1], '\\', '/') # scss
2020-08-10 22:25:43 +02:00
finput = str.replace(sys.argv[2], '\\', '/') # css
2018-01-19 22:49:46 +01:00
foutput = str.replace(sys.argv[3], '\\', '/') # min.css
2020-08-10 22:25:43 +02:00
ftemp1 = '__temp_compresss_py_1.tmp.css'
ftemp2 = '__temp_compresss_py_2.tmp.css'
2020-01-16 10:07:14 +01:00
print('======== INPUT ========')
print()
print(fsource)
print(finput)
print(foutput)
print()
print()
print('======== DELETE OLD DATA ========')
2018-06-16 03:16:42 +02:00
if os.path.isfile(finput):
2020-01-16 10:07:14 +01:00
try:
os.remove(finput)
print(finput + ' deleted')
except:
print(sys.exc_info()[0])
2018-06-16 03:16:42 +02:00
else:
2020-01-16 10:07:14 +01:00
print(finput + ' does not exist')
2018-06-16 03:16:42 +02:00
if os.path.isfile(foutput):
2020-01-16 10:07:14 +01:00
try:
os.remove(foutput)
print(foutput + ' deleted')
except:
print(sys.exc_info()[0])
2018-06-16 03:16:42 +02:00
else:
2020-01-16 10:07:14 +01:00
print(foutput + ' does not exist')
print()
print()
2018-06-16 03:16:42 +02:00
2020-01-16 10:07:14 +01:00
print('======== CALL SCSS ========')
2020-08-10 22:25:43 +02:00
print('> scss --style=expanded --no-cache --update "' + fsource + ':' + finput + '"')
out = subprocess.run([shutil.which('scss'), '--style=expanded', '--no-cache', '--update', fsource + ':' + finput], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
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
2020-01-16 10:07:14 +01:00
print('======== CLEANUP COMMENTS ========')
2018-06-16 03:16:42 +02:00
with open(finput, 'r') as tf:
data1 = tf.read()
print(str(len(data1)) + ' characters read from ' + os.path.basename(finput))
data1 = comment_remover(data1)
2020-01-16 10:07:14 +01:00
print('Comments in css removed')
2018-06-16 03:16:42 +02:00
with open(ftemp1, "w") as tf:
tf.write(data1)
print(str(len(data1)) + ' characters written to ' + ftemp1)
print('')
print('')
2020-01-16 10:07:14 +01:00
print('======== CALL YUI ========')
print('> java -jar yuicompressor.jar --verbose "' + finput + '" -o "' + ftemp2 + '"')
2020-08-10 22:25:43 +02:00
out = subprocess.run(['java', '-jar', 'yuicompressor.jar', '--verbose', ftemp1, '-o', ftemp2], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2020-01-16 10:07:14 +01:00
print('STDOUT:')
2018-01-01 21:07:48 +01:00
print(out.stdout.decode('utf-8'))
2020-01-16 10:07:14 +01:00
print('STDERR:')
2018-01-01 21:07:48 +01:00
print(out.stderr.decode('utf-8'))
2018-01-19 22:49:46 +01:00
print('')
print('')
2020-01-16 10:07:14 +01:00
print('======== READ ========')
2018-06-16 03:16:42 +02:00
with open(ftemp2, 'r') as tf:
2018-01-01 21:07:48 +01:00
data = tf.read()
2018-06-16 03:16:42 +02:00
print(str(len(data)) + ' characters read from ' + ftemp2)
2018-01-01 21:07:48 +01:00
2018-01-19 22:49:46 +01:00
print('')
print('')
2020-01-16 10:07:14 +01:00
print('======== REM ========')
2018-01-01 21:07:48 +01:00
try:
2020-01-16 10:07:14 +01:00
os.remove(ftemp1)
2018-06-16 03:16:42 +02:00
print(ftemp1 + ' deleted')
2020-01-16 10:07:14 +01:00
os.remove(ftemp2)
2018-06-16 03:16:42 +02:00
print(ftemp2 + ' deleted')
2019-11-08 15:15:58 +01:00
except:
print(sys.exc_info()[0])
2018-01-01 21:07:48 +01:00
2018-01-19 22:49:46 +01:00
print('')
print('')
2020-01-16 10:07:14 +01:00
print('======== REGEX ========')
data = re.sub(r'(\}*\})', '\g<1>\n', data)
2018-01-01 21:07:48 +01:00
2018-01-20 03:05:15 +01:00
print('css data modified (1)')
2018-01-19 22:49:46 +01:00
print('')
print('')
2020-01-16 10:07:14 +01:00
print('======== MEDIA ========')
2018-01-01 21:07:48 +01:00
ins = []
for i in range(len(data)):
if data[i:].startswith('@media'):
copen = findnext(data, i, '{')
cclose = findclose(data, copen)
2020-01-16 10:07:14 +01:00
if countnl(data, copen, cclose) == 0: continue;
2018-01-01 21:07:48 +01:00
2020-01-16 10:07:14 +01:00
ins.append((copen + 1, '\n\t'))
for i2 in range(copen + 1, cclose):
if data[i2] == '\n':
tp = (i2 + 1, '\t')
ins.append(tp)
2018-01-01 21:07:48 +01:00
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('')
2020-01-16 10:07:14 +01:00
print('======== WRITE ========')
2018-01-01 21:07:48 +01:00
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-06-16 03:16:42 +02:00
2020-01-16 10:07:14 +01:00
print('======== REMOVE MAP ========')
2018-06-16 03:16:42 +02:00
if os.path.isfile(finput + '.map'):
2020-01-16 10:07:14 +01:00
try:
os.remove(finput + '.map')
print(finput + '.map' + ' deleted')
except Exception as e:
print(e)
2018-06-16 03:16:42 +02:00
else:
2020-01-16 10:07:14 +01:00
print(finput + '.map' + ' does not exist')
2018-06-16 03:16:42 +02:00
print('')
print('')
2018-01-20 03:05:15 +01:00
print('Finished.')