From cd9b1891c09e858e7e4db0c87267014b81be7adf Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 23 Mar 2012 15:25:35 -0400 Subject: [PATCH] Add a CleverCSS plugin. CleverCSS [1] is a Python-based preprocessor for CSS. It is not quite as powerful as LessCSS and doesn't seem to get much updates, but the advantage is that it only requires Python (which you'd already have for Hyde) and you don't need node.js or some client-side JS. [1] http://sandbox.pocoo.org/clevercss/ --- hyde/ext/plugins/css.py | 81 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/hyde/ext/plugins/css.py b/hyde/ext/plugins/css.py index 5c0fecc..ae06e8e 100644 --- a/hyde/ext/plugins/css.py +++ b/hyde/ext/plugins/css.py @@ -3,7 +3,9 @@ CSS plugins """ -from hyde.plugin import CLTransformer + +from hyde.plugin import CLTransformer, Plugin +from hyde.exceptions import HydeException import re import subprocess @@ -227,3 +229,80 @@ class StylusPlugin(CLTransformer): "processing [%s]" % (stylus.name, resource.source_file)) return target.read_all() + +# +# Clever CSS +# + +class CleverCSSPlugin(Plugin): + """ + The plugin class for CleverCSS + """ + + def __init__(self, site): + super(CleverCSSPlugin, self).__init__(site) + try: + import clevercss + except ImportError, e: + raise HydeException('Unable to import CleverCSS: ' + e.message) + else: + self.clevercss = clevercss + + def _should_parse_resource(self, resource): + """ + Check user defined + """ + return resource.source_file.kind == 'ccss' and \ + getattr(resource, 'meta', {}).get('parse', True) + + def _should_replace_imports(self, resource): + return getattr(resource, 'meta', {}).get('uses_template', True) + + def begin_site(self): + """ + Find all the clevercss files and set their relative deploy path. + """ + for resource in self.site.content.walk_resources(): + if self._should_parse_resource(resource): + new_name = resource.source_file.name_without_extension + ".css" + target_folder = File(resource.relative_deploy_path).parent + resource.relative_deploy_path = target_folder.child(new_name) + + def begin_text_resource(self, resource, text): + """ + Replace @import statements with {% include %} statements. + """ + + if not self._should_parse_resource(resource) or \ + not self._should_replace_imports(resource): + return text + + import_finder = re.compile( + '^\\s*@import\s+(?:\'|\")([^\'\"]*)(?:\'|\")\s*\;\s*$', + re.MULTILINE) + + def import_to_include(match): + if not match.lastindex: + return '' + path = match.groups(1)[0] + afile = File(resource.source_file.parent.child(path)) + if len(afile.kind.strip()) == 0: + afile = File(afile.path + '.ccss') + ref = self.site.content.resource_from_path(afile.path) + if not ref: + raise self.template.exception_class( + "Cannot import from path [%s]" % afile.path) + ref.is_processable = False + return self.template.get_include_statement(ref.relative_path) + text = import_finder.sub(import_to_include, text) + return text + + def text_resource_complete(self, resource, text): + """ + Run clevercss compiler on text. + """ + if not self._should_parse_resource(resource): + return + + return self.clevercss.convert(text, self.settings) +