Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
Version 0.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
klen committed Feb 22, 2011
1 parent ec1689a commit 642cce7
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 64 deletions.
7 changes: 7 additions & 0 deletions ChangeLog.txt
@@ -1,6 +1,13 @@
Changes
=======

## 2011-02-21 0.6.0
-------------------
* Add warnings ( not found mix and etc )
* Add @warn
* Add 'add', 'or', 'not' operators in expressions
* Add mode compass support

## 2011-02-20 0.5.9
-------------------
* Add @options sort, @option comments, @option cache
Expand Down
2 changes: 2 additions & 0 deletions Todo.txt
@@ -1,3 +1,5 @@
Fix recursion in compass vars
Fix expression evalute in progress, and mixin too
Fix cache
Add warnings on unknown properties
Add zen codding abbr
Expand Down
10 changes: 7 additions & 3 deletions docs/conf.py
Expand Up @@ -13,6 +13,10 @@

import sys, os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from scss import VERSION, AUTHOR

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
Expand Down Expand Up @@ -41,16 +45,16 @@

# General information about the project.
project = u'python-scss'
copyright = u'2011, Kirill Klenov'
copyright = AUTHOR

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '0.4.7'
version = VERSION
# The full version, including alpha/beta/rc tags.
release = '0.4.7'
release = VERSION

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
32 changes: 18 additions & 14 deletions scss/__init__.py
Expand Up @@ -2,7 +2,7 @@
import operator


VERSION_INFO = (0, 5, 9)
VERSION_INFO = (0, 6, 0)

__project__ = PROJECT = __name__
__version__ = VERSION = '.'.join(str(i) for i in VERSION_INFO)
Expand Down Expand Up @@ -44,19 +44,23 @@
del t, m, k, f

OPRT = {
'^' : operator.__pow__,
'+' : operator.__add__,
'-' : operator.__sub__,
'*' : operator.__mul__,
'/' : operator.__div__,
'!' : operator.__neg__,
'<' : operator.__lt__,
'<=': operator.__le__,
'>' : operator.__gt__,
'>=': operator.__ge__,
'==': operator.__eq__,
'=' : operator.__eq__,
'!=': operator.__ne__,
'^' : operator.__pow__,
'+' : operator.__add__,
'-' : operator.__sub__,
'*' : operator.__mul__,
'/' : operator.__div__,
'!' : operator.__neg__,
'<' : operator.__lt__,
'<=' : operator.__le__,
'>' : operator.__gt__,
'>=' : operator.__ge__,
'==' : operator.__eq__,
'=' : operator.__eq__,
'!=' : operator.__ne__,
'&': operator.__and__,
'|' : operator.__or__,
'and': lambda x,y: x and y,
'or': lambda x,y: x or y,
}

ELEMENTS_OF_TYPE = {
Expand Down
10 changes: 10 additions & 0 deletions scss/base.py
@@ -1,3 +1,13 @@
import sys


def warn(warning):
if not isinstance(warning, str):
warning = str(warning[1])
print >> sys.stderr, "\nWarning: %s" % warning
return ''


class Node(object):
delim = ''

Expand Down
2 changes: 1 addition & 1 deletion scss/function.py
Expand Up @@ -237,7 +237,7 @@ def _nest(*args):
' '.join(
s.strip() for s in p
) for p in product(
*( sel.value.split(',') for sel in args )
*( StringValue( sel ).value.split(',') for sel in args )
)
)

Expand Down
32 changes: 15 additions & 17 deletions scss/grammar.py
Expand Up @@ -15,23 +15,16 @@
SCSS_COMMENT = dblSlashComment
COMMENT = CSS_COMMENT | SCSS_COMMENT

# SCSS directives
MIXIN_SYM = Suppress("@mixin")
INCLUDE_SYM = Suppress("@include")
EXTEND_SYM = Suppress("@extend")
FOR_SYM = Suppress("@for")
DEBUG_SYM = Suppress("@debug")

# Property values
HASH = Word('#', alphanums + "_-")
HEXCOLOR = Suppress("#") + Word(hexnums, min=3, max=8)
NUMBER_VALUE = NUMBER + ( oneOf("em ex px cm mm in pt pc deg %") | EMPTY)
PATH = Word(alphanums + "_-/.", alphanums + "_-./?#&")

# Operators
MATH_OPERATOR = oneOf("+ - / *")
MATH_OPERATOR = oneOf("+ - / * and or")
COMBINATOR = oneOf("+ >")
IF_OPERATOR = oneOf("== != <= >= < >")
IF_OPERATOR = oneOf("== != <= >= < > =")

# Values
VARIABLE = "$" + IDENT
Expand Down Expand Up @@ -70,28 +63,33 @@
SELECTOR_GROUP = SELECTOR + ZeroOrMore(Optional(COMBINATOR) + SELECTOR)
SELECTOR_TREE = SELECTOR_GROUP + ZeroOrMore(COMMA + SELECTOR_GROUP)

# @warn
WARN = "@warn" + quotedString + OPT_SEMICOLON

# @include
INCLUDE = INCLUDE_SYM + IDENT + Optional(LPAREN + ZeroOrMore(COMMA | EXPRESSION) + RPAREN) + OPT_SEMICOLON
INCLUDE = "@include" + IDENT + Optional(LPAREN + ZeroOrMore(COMMA | EXPRESSION) + RPAREN) + OPT_SEMICOLON

# @extend
EXTEND = EXTEND_SYM + SELECTOR + OPT_SEMICOLON
EXTEND = "@extend" + SELECTOR + OPT_SEMICOLON

# SCSS variable assigment
VAR_DEFINITION = Suppress("$") + IDENT + COLON + (SEP_VAL_STRING | EXPRESSION ) + ("!default" | EMPTY) + OPT_SEMICOLON

# Ruleset
RULESET = Forward()
CONTENT = COMMENT | INCLUDE | VAR_DEFINITION | RULESET
IF = Forward()
CONTENT = COMMENT | WARN | IF | INCLUDE | VAR_DEFINITION | RULESET
RULE_CONTENT = CONTENT | DECLARESET | DECLARATION

# SCSS control directives
IF_CONDITION = EXPRESSION + Optional(IF_OPERATOR + EXPRESSION)
IF_BODY = LACC + ZeroOrMore(RULE_CONTENT) + RACC
ELSE = Suppress("@else") + LACC + ZeroOrMore(RULE_CONTENT) + RACC
IF = ( Suppress("@if") | Suppress("@else if") ) + IF_CONDITION + IF_BODY + (ELSE | EMPTY)
IF << (
( Suppress("@if") | Suppress("@else if") ) + IF_CONDITION + IF_BODY + (ELSE | EMPTY))

FOR_BODY = ZeroOrMore(RULE_CONTENT)
FOR = FOR_SYM + VARIABLE + Suppress("from") + VALUE + (Suppress("through") | Suppress("to")) + VALUE + LACC + FOR_BODY + RACC
DEBUG = DEBUG_SYM + EXPRESSION + OPT_SEMICOLON
FOR = "@for" + VARIABLE + Suppress("from") + VALUE + (Suppress("through") | Suppress("to")) + VALUE + LACC + FOR_BODY + RACC
DEBUG = "@debug" + EXPRESSION + OPT_SEMICOLON
CONTROL_DIR = IF | FOR | DEBUG

RULESET << (
Expand All @@ -101,7 +99,7 @@
# SCSS mixin
MIXIN_PARAM = VARIABLE + Optional(COLON + EXPRESSION)
MIXIN_PARAMS = LPAREN + ZeroOrMore(COMMA | MIXIN_PARAM) + RPAREN
MIXIN = (MIXIN_SYM + IDENT + Optional(MIXIN_PARAMS) +
MIXIN = ("@mixin" + IDENT + Optional(MIXIN_PARAMS) +
LACC + ZeroOrMore(RULE_CONTENT | CONTROL_DIR) + RACC)

# Root elements
Expand Down
5 changes: 3 additions & 2 deletions scss/parser.py
Expand Up @@ -4,8 +4,8 @@
from collections import defaultdict

from scss import SORTING
from scss.base import CopyNode, Empty, ParseNode, SimpleNode, SemiNode, SepValString, Node
from scss.grammar import STYLESHEET, VAR_DEFINITION, EXPRESSION, SELECTOR_GROUP, DECLARATION, DECLARESET, EXTEND, INCLUDE, MIXIN, MIXIN_PARAM, RULESET, VARIABLE, DEC_NAME, HEXCOLOR, NUMBER_VALUE, SCSS_COMMENT, CSS_COMMENT, FUNCTION, IF, ELSE, IF_CONDITION, IF_BODY, SELECTOR, FOR, FOR_BODY, SEP_VAL_STRING, TERM, MEDIA, DEBUG, EMPTY, CHARSET, FONT_FACE, quotedString, IMPORT, VARIABLES, OPTION
from scss.base import CopyNode, Empty, ParseNode, SimpleNode, SemiNode, SepValString, Node, warn
from scss.grammar import STYLESHEET, VAR_DEFINITION, EXPRESSION, SELECTOR_GROUP, DECLARATION, DECLARESET, EXTEND, INCLUDE, MIXIN, MIXIN_PARAM, RULESET, VARIABLE, DEC_NAME, HEXCOLOR, NUMBER_VALUE, SCSS_COMMENT, CSS_COMMENT, FUNCTION, IF, ELSE, IF_CONDITION, IF_BODY, SELECTOR, FOR, FOR_BODY, SEP_VAL_STRING, TERM, MEDIA, DEBUG, EMPTY, CHARSET, FONT_FACE, quotedString, IMPORT, VARIABLES, OPTION, WARN
from scss.value import NumberValue, ColorValue, Expression, Variable, QuotedStringValue, BooleanValue
from scss.var import Function, IfNode, ForNode, Mixin, Extend, Include, VarDef

Expand Down Expand Up @@ -206,6 +206,7 @@ def __init__(self, cache = None, options=None):
SCSS_COMMENT.setParseAction(lambda s, l, t: '')

# At rules
WARN.setParseAction(warn)
MEDIA.setParseAction(self.getType(SimpleNode))
IMPORT.setParseAction(self.getType(SemiNode))
CHARSET.setParseAction(self.getType(SemiNode))
Expand Down
1 change: 1 addition & 0 deletions scss/tests/__init__.py
Expand Up @@ -12,6 +12,7 @@ def all_tests_suite():
'scss.tests.test_functions',
'scss.tests.test_if',
'scss.tests.test_options',
# 'scss.tests.test_compass',
])


Expand Down
1 change: 1 addition & 0 deletions scss/tests/test_scss.py
Expand Up @@ -13,6 +13,7 @@ def test_base(self):
@charset utf-8;
@import url(test);
@warn "Test warnings!"
@mixin z-base {
a:hover, a:active { outline: none; }
a, a:active, a:visited { color: #607890; }
Expand Down
5 changes: 3 additions & 2 deletions scss/tests/test_variables.py
Expand Up @@ -14,6 +14,7 @@ def test_variables(self):
$blue: #ffdd00 !default;
$test: rgb(120, 35, 64);
$test2: rgba(120, 35, 64, .4);
$len: 0px or 5px;
}
$margin: 16px;
$side: top;
Expand All @@ -24,7 +25,7 @@ def test_variables(self):
background-color: $test + 5%;
background-image: url('/test/' + $image);
color: $blue - 9%;
margin: 0 ( -$margin * 2 ) 12px;
margin: $len ( -$margin * 2 ) 12px;
}
.border {
Expand All @@ -38,6 +39,6 @@ def test_variables(self):
font: -1.5em + 50px;
}
"""
test = ".content-navigation {\n\tmargin: 0 -32px 12px;\n\tborder-color: #fd0;\n\tbackground-color: #7b1f3e;\n\tbackground-image: url('/test/test.png');\n\tcolor: #f3d40b}\n\n.border {\n\tmargin: 8px;\n\tpadding-top: 8px;\n\tpadding-left: -14px;\n\tborder-top-color: #fd0;\n\tcolor: rgba(120,35,64,0.40);\n\tfont: 2.346em}"
test = ".content-navigation {\n\tmargin: 5px -32px 12px;\n\tborder-color: #fd0;\n\tbackground-color: #7b1f3e;\n\tbackground-image: url('/test/test.png');\n\tcolor: #f3d40b}\n\n.border {\n\tmargin: 8px;\n\tpadding-top: 8px;\n\tpadding-left: -14px;\n\tborder-top-color: #fd0;\n\tcolor: rgba(120,35,64,0.40);\n\tfont: 2.346em}"
out = self.parser.parse(src)
self.assertEqual(test, out)
35 changes: 31 additions & 4 deletions scss/value.py
Expand Up @@ -10,6 +10,14 @@ def _do_op(cls, first, second, op):
@classmethod
def _do_cmps(cls, first, second, op):
return op(first.value, second.value)
@classmethod
def _do_bits(cls, first, second, op):
first = StringValue(first)
second = StringValue(second)
k = op(first.value, second.value)
return first if first.value == k else second

# Math operation
def __add__(self, other):
return self._do_op(self, other, OPRT['+'])
__radd__ = __add__
Expand All @@ -25,6 +33,9 @@ def __mul__(self, other):
return self._do_op(self, other, OPRT['*'])
def __lt__(self, other):
return self._do_cmps(self, other, OPRT['<'])
__rmul__ = __mul__

# Compare operation
def __le__(self, other):
return self._do_cmps(self, other, OPRT['<='])
def __gt__(self, other):
Expand All @@ -35,7 +46,16 @@ def __eq__(self, other):
return self._do_cmps(self, other, OPRT['=='])
def __ne__(self, other):
return self._do_cmps(self, other, OPRT['!='])
__rmul__ = __mul__

# Bit operation
def __and__(self, other):
return self._do_bits(self, other, OPRT['and'])
def __or__(self, other):
return self._do_bits(self, other, OPRT['or'])

# Boolean
def __nonzero__(self):
return getattr(self, 'value') and True or False


hex2rgba = {
Expand Down Expand Up @@ -128,7 +148,7 @@ def __float__(self):

def __str__(self):
value = ("%0.03f" % self.value).strip('0').rstrip('.') or 0
return "%s%s" % (value, self.units)
return "%s%s" % (value, self.units if self.value else '')

@classmethod
def _do_op(cls, self, other, op):
Expand Down Expand Up @@ -170,7 +190,7 @@ def __init__(self, t):
if t is None:
self.value = False
elif isinstance(t, Value):
self.value = bool(t.value)
self.value = bool(t.value) if t.value != 'false' else False
elif isinstance(t, ( str, bool )):
self.value = bool(t) if t != 'false' else False
else:
Expand Down Expand Up @@ -245,7 +265,7 @@ def do_expression(cls, data, ctx=None):
if isinstance(n, Variable):
n.ctx = ctx

if OPRT.get(data[0], None):
if not OPRT.get(data[0], None) is None:
data.insert(0, NumberValue(0))

it = iter(data)
Expand All @@ -256,6 +276,13 @@ def do_expression(cls, data, ctx=None):
if op:
second = next(it)
first = op(cls.prepare( first ), cls.prepare( second ))

if op == OPRT['and'] and not first:
raise StopIteration

elif op == OPRT['or'] and first:
raise StopIteration

res = next(it)
except StopIteration:
break
Expand Down

0 comments on commit 642cce7

Please sign in to comment.