Browse Source

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/
main
Elliott Sales de Andrade 12 years ago
committed by Lakshmi Vyasarajan
parent
commit
cd9b1891c0
1 changed files with 80 additions and 1 deletions
  1. +80
    -1
      hyde/ext/plugins/css.py

+ 80
- 1
hyde/ext/plugins/css.py View File

@@ -3,7 +3,9 @@
CSS plugins CSS plugins
""" """


from hyde.plugin import CLTransformer

from hyde.plugin import CLTransformer, Plugin
from hyde.exceptions import HydeException


import re import re
import subprocess import subprocess
@@ -227,3 +229,80 @@ class StylusPlugin(CLTransformer):
"processing [%s]" % (stylus.name, resource.source_file)) "processing [%s]" % (stylus.name, resource.source_file))
return target.read_all() 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)


Loading…
Cancel
Save