yuicompressor.py 1004 B

1234567891011121314151617181920212223242526272829303132333435
  1. # -*- coding: utf-8 -*-
  2. from pelican import signals
  3. from subprocess import call
  4. import logging
  5. import os
  6. logger = logging.getLogger(__name__)
  7. # Display command output on DEBUG and TRACE
  8. SHOW_OUTPUT = logger.getEffectiveLevel() <= logging.DEBUG
  9. """
  10. Minify CSS and JS files in output path
  11. with Yuicompressor from Yahoo
  12. Required : pip install yuicompressor
  13. """
  14. def minify(pelican):
  15. """
  16. Minify CSS and JS with YUI Compressor
  17. :param pelican: The Pelican instance
  18. """
  19. for dirpath, _, filenames in os.walk(pelican.settings['OUTPUT_PATH']):
  20. for name in filenames:
  21. if os.path.splitext(name)[1] in ('.css','.js'):
  22. filepath = os.path.join(dirpath, name)
  23. logger.info('minifiy %s', filepath)
  24. verbose = '-v' if SHOW_OUTPUT else ''
  25. call("yuicompressor {} --charset utf-8 {} -o {}".format(
  26. verbose, filepath, filepath), shell=True)
  27. def register():
  28. signals.finalized.connect(minify)