Пример #1
0
    def __init__(self, message, token=None):
        if token:
            message += " ("
            if token.parent:
                message += "while processing %s in \"%s\"" % (
                    token.__class__.__name__,
                    token.parent.toString(cssparser.CSSOptions(colorize=True)))
            else:
                message += "while processing %s: \"%s\"" % (
                    token.__class__.__name__,
                    token.toString(cssparser.CSSOptions(colorize=True)))
            message += ")"

        Exception.__init__(self, message)
Пример #2
0
    def compile(self, token, options=cssparser.CSSOptions()):
        if token.isRuleSet():
            self.pushScope()

        if token.isAtRule():
            self.compileAtRule(token, options)
            if token.parent == None:  # the token could have been removed
                return

        i = 0
        while i < len(token.children):
            child = token.children[i]
            if child.parent == None:
                raise SCSSCompileError(
                    "Child is looking for its parents, please report at the tent next to the main stage",
                    child)
            self.compile(child, options)
            if child.parent:  # the token could have been removed
                i += 1

        if token.isAnyToken():
            self.compileAny(token)
        elif token.isValue():
            self.compileValue(token)
        elif token.isDeclaration():
            self.compileDeclaration(token)
        elif token.isAssignment():
            SCSSExpression.processAssignment(token, self.getCurrentScope())
        elif token.isRuleSet():
            self.compileRuleSet(token)
            self.popScope()
        elif token.isAtRule() and token.getKeyWord() == "extend":
            self.processExtend(token)
Пример #3
0
 def toString(self, options = cssparser.CSSOptions()):
     strings = []
     for item in self.items:
         if isinstance(item, SCSSList) and len(item.items) == 0:
             continue
         strings.append(item.toString(options))
     return self.separator.join(strings)
Пример #4
0
 def fromStream(stream):
     options = cssparser.CSSOptions(compileScss=True)
     value = cssparser.CSSValueToken(None)
     token = value
     while token and not stream.isEndOfFile():
         token = token.process(stream, options)
     return SCSSExpression(value.getStrippedChildren())
Пример #5
0
 def fromString(expression, options=None):
     if options:
         options.compileScss = True
     else:
         options = cssparser.CSSOptions(compileScss=True)
     stream = cssparser.CSSStream(expression, options)
     return SCSSExpression.fromStream(stream)
Пример #6
0
 def toString(self, options = cssparser.CSSOptions()):
     result = ""
     stream = cssparser.CSSStream(self.value)
     character = stream.take()
     while character != cssparser.CSS_EOF:
         if character == "\"":
             result += "\\"
         result += character
         character = stream.take()
     if options.stripQuotes:
         return result
     else:
         return "\"" + result + "\""
Пример #7
0
    def start(self, options=cssparser.CSSOptions()):
        compiler = scsscompiler.SCSSCompiler()
        scope = compiler.getGlobalScope()
        while True:
            statement = raw_input(">> ")
            if statement == "\\q" or statement == "exit()":
                print "Bye."
                return

            try:
                expression = scssexpression.SCSSExpression.fromString(
                    statement, options)
                expression.evaluate(scope)
                for token in expression.tokens:
                    print token.toString(options)
            except Exception, exception:
                print exception
Пример #8
0
 def processExtend(self, token):
     ruleSets = token.getStyleSheet().getRuleSets()
     mySelector = token.getParentRuleSet().getSelector()
     options = cssparser.CSSOptions(stripWhiteSpace=True)
     signatureString = cssparser.tokenListToString(token.getSignature(),
                                                   options)
     for ruleSet in ruleSets:
         selector = ruleSet.getSelector()
         for sub1 in selector.getSubSelectors():
             if cssparser.tokenListToString(sub1,
                                            options) == signatureString:
                 for sub2 in mySelector.getSubSelectors():
                     if selector.getLastChild().isWhiteSpace():
                         selector.getLastChild().remove()
                     selector.createDelimiterChild(",")
                     for child in sub2:
                         selector.add(child.clone())
                 break
     token.remove()
Пример #9
0
    def toString(self, options = cssparser.CSSOptions()):
        if self.a == 0.0:
            return "transparent"

        # just use some long strings so they are not used by accident
        hex = "012345678901234567890123456789" 
        hsl = "012345678901234567890123456789"
        key = "012345678901234567890123456789"
        rgb = "012345678901234567890123456789"

        if not self.isRgbValue():
            if self.a == 1.0:
                hsl = "hsl(%s,%s%%,%s%%)" % (int(self.h * 360), int(self.s * 100), int(self.l * 100))
            else:
                hsl = "hsla(%s,%s,%s,%s)" % (int(self.h * 360), int(self.s * 100), int(self.l * 100), str(self.a)[1:])
            self.convertToRgb()

        if self.a == 1.0:
            keyword = self.colorToKeyword([self.r * 255, self.g * 255, self.b * 255])
            if keyword:
                key = keyword

            hex = ""
            for c in [self.r, self.g, self.b]:
                hex += string.hexdigits[15 if c * 16 > 15 else int(c * 16)] + string.hexdigits[int((c * 255) % 16)]
            if hex[0] == hex[1] and hex[2] == hex[3] and hex[4] == hex[5]:
                hex = hex[0] + hex[2] + hex[4]
            hex = "#" + hex
        else:
            rgb = "rgba(%s,%s,%s,%s)" % (int(self.r * 255), int(self.g * 255), int(self.b * 255), str(self.a)[1:])

        # always pick the shortest representation
        if len(key) < len(hsl) and len(key) < len(hex) and len(key) < len(rgb):
            return key
        elif len(hsl) < len(hex) and len(hsl) < len(rgb):
            return hsl
        elif len(hex) < len(rgb):
            return hex
        else:
            return rgb
Пример #10
0
 def processInterpolation(token, scope=None):
     result = ""
     stream = cssparser.CSSStream(
         token.data[1:-1] if token.isString() else token.data)
     character = stream.take()
     while character != cssparser.CSS_EOF:
         if character == "#" and stream.current() == "{":
             stream.take()  # skip the {
             expression = SCSSExpression.fromStream(stream)
             expression.evaluate(scope)
             result += expression.toString(
                 cssparser.CSSOptions(stripQuotes=True))
             stream.take()  # skip the }
         else:
             if character == "\"":
                 result += "\\"
             result += character
         character = stream.take()
     if token.isString():
         return cssparser.CSSStringToken(None, "\"" + result + "\"")
     else:
         return cssparser.CSSIdentifierToken(None, result)
Пример #11
0
#!/usr/bin/python

import optparse
import sys

import cssparser

if __name__ == "__main__":

    usage = "Usage: stdin | %prog [options] | stdout"
    optionParser = optparse.OptionParser(usage=usage)
    optionParser.add_option("",
                            "--color",
                            action="store_true",
                            help="Colorize the output")
    (options, args) = optionParser.parse_args()

    parser = cssparser.CSSParser()
    options = cssparser.CSSOptions(stripWhiteSpace=True,
                                   stripComments=True,
                                   minimizeValues=True,
                                   stripExtraSemicolons=True,
                                   colorize=options.color,
                                   compileScss=True)
    token = parser.parse(sys.stdin.read(), options)
    print token.toString(options)
Пример #12
0
 def toString(self, options = cssparser.CSSOptions()):
     return self.token.toString(options)
Пример #13
0
 def toString(self, options = cssparser.CSSOptions()):
     return "true" if self.value else "false"
Пример #14
0
 def toString(self, options = cssparser.CSSOptions()):
     return str(self.value) + self.unit
Пример #15
0
    for entry in sorted(os.listdir(path)):
        if entry[0] == ".":
            continue
        yield entry

if __name__ == "__main__":

    usage = "Usage: %prog [options]"
    optionParser = optparse.OptionParser(usage = usage)
    optionParser.add_option("", "--color", action = "store_true",
                      help = "Colorize the output")
    (o, args) = optionParser.parse_args()

    for test in dirEntries("test/sass"):
        parser = cssparser.CSSParser()
        options = cssparser.CSSOptions(stripWhiteSpace = True, compileScss = True)
        colorOptions = cssparser.CSSOptions(stripWhiteSpace = True, compileScss = True, colorize = True)

        with codecs.open("test/sass/%s/in.scss" % test, "r", "utf-8") as f:
            token = parser.parse(f.read(), options)
    
        compiler = scsscompiler.SCSSCompiler()
        compiler.compile(token)
        output = token.toString(options)

        with codecs.open("test/sass/%s/out.css" % test, "r", "utf-8") as f:
            expected = f.read()

        if output == expected.strip():
            print "Test %s passed." % test
        else:
Пример #16
0
 def toString(self, options=cssparser.CSSOptions()):
     string = ""
     for token in self.tokens:
         string += token.toString(options)
     return string