Skip to content

Instantly share code, notes, and snippets.

@mapix
Last active June 18, 2019 15:03
Show Gist options
  • Save mapix/29094675e22c4030f52f8159a09e0dd8 to your computer and use it in GitHub Desktop.
Save mapix/29094675e22c4030f52f8159a09e0dd8 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import re
import os.path
import glob
import argparse
parser = argparse.ArgumentParser(description='split css')
parser.add_argument('css', help='css file')
parser.add_argument('index', help='index file')
args = parser.parse_args()
css_file = glob.glob(args.css)[0]
base, ext = os.path.splitext(css_file)
with open(css_file) as f:
rules = re.findall(r'[^\{]+\{[^\}]*\}', f.read(), re.MULTILINE)
print 'Number of rules: ', len(rules)
def chunker(seq, size):
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
i = 0
for sub_rules in chunker(rules, 4096):
with open(base + '-' + str(i) + ".css", 'w') as f:
f.write('\n'.join(sub_rules))
i += 1
with open(args.index) as f:
rewrite = f.read()
with open(args.index, 'w') as f:
for l in rewrite.split('\n'):
if '<link' in l:
a, b = l.split('.css')
for j in range(i):
f.write(a + '-' + str(j) + '.css' + b + '\n')
else:
f.write(l + '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment