Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:sass filter for YAML-like CSS styling #143

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions hamlpy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
except ImportError, e:
_markdown_available = False

try:
from sassin import compile_with_scss as sass_compile
_sass_available = True
except ImportError, e:
_sass_available = False

class NotAvailableError(Exception):
pass

Expand Down Expand Up @@ -45,6 +51,8 @@ class NotAvailableError(Exception):
MARKDOWN_FILTER = ':markdown'
CDATA_FILTER = ':cdata'
PYGMENTS_FILTER = ':highlight'
SCSS_FILTER = ':scss'
SASS_FILTER = ':sass'

ELEMENT_CHARACTERS = (ELEMENT, ID, CLASS)

Expand Down Expand Up @@ -111,6 +119,9 @@ def create_node(haml_line):
if stripped_line == MARKDOWN_FILTER:
return MarkdownFilterNode(haml_line)

if stripped_line == SASS_FILTER:
return SassFilterNode(haml_line)

return PlaintextNode(haml_line)

class TreeNode(object):
Expand Down Expand Up @@ -604,3 +615,20 @@ def _render(self):
self.before += markdown( ''.join(lines))
else:
self.after = self.render_newlines()

class SassFilterNode(FilterNode):
def _render(self):
if self.children:
if not _sass_available:
raise NotAvailableError("Markdown is not available")

self.before = '<style type=%(attr_wrapper)stext/css%(attr_wrapper)s>\n/*<![CDATA[*/%(new_lines)s' % {
'attr_wrapper': self.attr_wrapper,
'new_lines': self.render_newlines(),
}
self.after = '/*]]>*/\n</style>\n'
indent_offset = len(self.children[0].spaces)
text = ''.join(''.join([c.spaces[indent_offset:], c.haml, c.render_newlines()]) for c in self.children)
self.before += sass_compile(text)
else:
self.after = self.render_newlines()
11 changes: 11 additions & 0 deletions hamlpy/test/templates/filtersSass.hamlpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
:sass
$highlight: #9F2
/* our special border
@mixin big-border
border: 3px solid $highlightcolor
#main,
#another_main
font:
color: $highlight
@include big-border

8 changes: 8 additions & 0 deletions hamlpy/test/templates/filtersSass.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<style type='text/css'>
/*<![CDATA[*/
#another_main, #main {
font-color: #99ff22;
border: 3px solid $highlightcolor;
}
/*]]>*/
</style>