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) +