Skip to content

Instantly share code, notes, and snippets.

@ralsina
Last active December 18, 2015 17:09
Show Gist options
  • Save ralsina/5816594 to your computer and use it in GitHub Desktop.
Save ralsina/5816594 to your computer and use it in GitHub Desktop.
Process a Nikola output folder and replace all CSS files with versions where mincss has removed all the unused crud, making them much smaller.
import os
import sys
output_folder = os.path.abspath(sys.argv[1])
from mincss.processor import Processor
p = Processor()
urls = []
css_files = {}
for root, dirs, files in os.walk(output_folder):
for f in files:
url = os.path.join(output_folder, root, f)
if url.endswith('.css'):
fname = os.path.basename(url)
if fname in css_files:
print "You have two CSS files with the same name and that confuses me."
sys.exit(1)
css_files[fname] = url
if not f.endswith('.html'):
continue
urls.append(url)
p.process(*urls)
for inline in p.links:
fname = os.path.basename(inline.href)
print css_files[fname]
print "===>", inline.href, len(inline.before), len(inline.after)
with open(css_files[fname], 'wb+') as outf:
outf.write(inline.after)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment