Skip to content

Instantly share code, notes, and snippets.

@jpustula
Created April 25, 2014 08:47
Show Gist options
  • Save jpustula/11282495 to your computer and use it in GitHub Desktop.
Save jpustula/11282495 to your computer and use it in GitHub Desktop.
CSS compressor for phpBB.
#!/usr/bin/env python
"""
Compress all CSS files imported in a single file (eg. stylesheet.css) and write
it's minified content to new file called stylesheet.min.css.
Requirements:
- cssmin library (https://github.com/zacharyvoase/cssmin)
Usage:
- compress.py -f path_to_stylesheet
"""
import argparse
import cssmin
import os
import re
# Parse arguments from command line
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--file', default = 'stylesheet.css', help = 'path to stylesheet.css')
args = parser.parse_args()
# Get base path of given file
base_path = os.path.dirname(args.file)
# Extract imported *.css files list
files_list = re.findall('"(.*\.css)"', open(args.file).read())
# Create new file
minified_file = open(os.path.join(base_path, 'stylesheet.min.css'), 'w')
for f in files_list:
# Append minified content to new file
minified_file.write(cssmin.cssmin(open(os.path.join(base_path, f)).read()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment