Last active
December 18, 2015 17:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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