Beispiel #1
0
def stylesheet_declarations(string, is_merc=False, scale=1):
    """ Parse a string representing a stylesheet into a list of declarations.
    
        Required boolean is_merc indicates whether the projection should
        be interpreted as spherical mercator, so we know what to do with
        zoom/scale-denominator in parse_rule().
    """
    # everything is display: map by default
    display_map = Declaration(Selector(SelectorElement(['*'], [])),
                              Property('display'), Value('map', False),
                              (False, (0, 0, 0), (0, 0)))

    declarations = [display_map]

    tokens = cssTokenizer().tokenize(string)
    variables = {}

    while True:
        try:
            for declaration in parse_rule(tokens, variables, [], [], is_merc):
                if scale != 1:
                    declaration.scaleBy(scale)

                declarations.append(declaration)
        except StopIteration:
            break

    # sort by a css-like method
    return sorted(declarations, key=operator.attrgetter('sort_key'))
Beispiel #2
0
def stylesheet_declarations(string, is_merc=False, scale=1):
    """ Parse a string representing a stylesheet into a list of declarations.
    
        Required boolean is_merc indicates whether the projection should
        be interpreted as spherical mercator, so we know what to do with
        zoom/scale-denominator in parse_rule().
    """
    # everything is display: map by default
    display_map = Declaration(Selector(SelectorElement(['*'], [])),
                              Property('display'), Value('map', False),
                              (False, (0, 0, 0), (0, 0)))
    
    declarations = [display_map]

    tokens = cssTokenizer().tokenize(string)
    variables = {}
    
    while True:
        try:
            for declaration in parse_rule(tokens, variables, [], [], is_merc):
                if scale != 1:
                    declaration.scaleBy(scale)
            
                declarations.append(declaration)
        except StopIteration:
            break
    
    # sort by a css-like method
    return sorted(declarations, key=operator.attrgetter('sort_key'))
Beispiel #3
0
def stylesheet_rulesets(string, base=None, is_gym=False):
    """ Parse a string representing a stylesheet into a list of rulesets.
    
        Optionally, accept a base string so we know where linked files come from,
        and a flag letting us know whether this is a Google/VEarth mercator projection
        so we know what to do with zoom/scale-denominator in postprocess_selector().
    """
    in_selectors = False
    in_block = False
    in_declaration = False  # implies in_block
    in_property = False  # implies in_declaration

    rulesets = []
    tokens = cssTokenizer().tokenize(string)

    for token in tokens:
        nname, value, line, col = token

        try:
            if not in_selectors and not in_block:
                if nname == "CHAR" and value == "{":
                    #
                    raise ParseException('Encountered unexpected opening "{"', line, col)

                elif (nname in ("IDENT", "HASH")) or (nname == "CHAR" and value != "{"):
                    # beginning of a
                    rulesets.append({"selectors": [[(nname, value)]], "declarations": []})
                    in_selectors = True

            elif in_selectors and not in_block:
                ruleset = rulesets[-1]

                if nname == "CHAR" and value == "{":
                    # open curly-brace means we're on to the actual rule sets
                    ruleset["selectors"][-1] = postprocess_selector(ruleset["selectors"][-1], is_gym, line, col)
                    in_selectors = False
                    in_block = True

                elif nname == "CHAR" and value == ",":
                    # comma means there's a break between selectors
                    ruleset["selectors"][-1] = postprocess_selector(ruleset["selectors"][-1], is_gym, line, col)
                    ruleset["selectors"].append([])

                elif nname not in ("COMMENT"):
                    # we're just in a selector is all
                    ruleset["selectors"][-1].append((nname, value))

            elif in_block and not in_declaration:
                ruleset = rulesets[-1]

                if nname == "IDENT":
                    # right at the start of a declaration
                    ruleset["declarations"].append({"property": [(nname, value)], "value": [], "position": (line, col)})
                    in_declaration = True
                    in_property = True

                elif nname == "CHAR" and value == "}":
                    # end of block
                    in_block = False

                elif nname not in ("S", "COMMENT"):
                    # something else
                    raise ParseException("Unexpected %(nname)s while looking for a property" % locals(), line, col)

            elif in_declaration and in_property:
                declaration = rulesets[-1]["declarations"][-1]

                if nname == "CHAR" and value == ":":
                    # end of property
                    declaration["property"] = postprocess_property(declaration["property"], line, col)
                    in_property = False

                elif nname not in ("COMMENT"):
                    # in a declaration property
                    declaration["property"].append((nname, value))

            elif in_declaration and not in_property:
                declaration = rulesets[-1]["declarations"][-1]

                if nname == "CHAR" and value == ";":
                    # end of declaration
                    declaration["value"] = postprocess_value(
                        declaration["value"], declaration["property"], base, line, col
                    )
                    in_declaration = False

                elif nname not in ("COMMENT"):
                    # in a declaration value
                    declaration["value"].append((nname, value))

        except ParseException, e:
            # raise ParseException(e.message + ' (line %(line)d, column %(col)d)' % locals(), line, col)
            raise
Beispiel #4
0
def stylesheet_rulesets(string, is_merc=False):
    """ Parse a string representing a stylesheet into a list of rulesets.
    
        Required boolean is_merc indicates whether the projection should
        be interpreted as spherical mercator, so we know what to do with
        zoom/scale-denominator in postprocess_selector().
    """
    in_selectors = False
    in_block = False
    in_declaration = False # implies in_block
    in_property = False # implies in_declaration
    
    rulesets = []
    tokens = cssTokenizer().tokenize(string)
    
    for token in tokens:
        nname, value, line, col = token
        
        try:
            if not in_selectors and not in_block:
                if nname == 'CHAR' and value == '{':
                    # 
                    raise ParseException('Encountered unexpected opening "{"', line, col)

                elif (nname in ('IDENT', 'HASH')) or (nname == 'CHAR' and value != '{'):
                    # beginning of a 
                    rulesets.append({'selectors': [[(nname, value)]], 'declarations': []})
                    in_selectors = True
                    
            elif in_selectors and not in_block:
                ruleset = rulesets[-1]
            
                if (nname == 'CHAR' and value == '{'):
                    # open curly-brace means we're on to the actual rule sets
                    ruleset['selectors'][-1] = postprocess_selector(ruleset['selectors'][-1], is_merc, line, col)
                    in_selectors = False
                    in_block = True
    
                elif (nname == 'CHAR' and value == ','):
                    # comma means there's a break between selectors
                    ruleset['selectors'][-1] = postprocess_selector(ruleset['selectors'][-1], is_merc, line, col)
                    ruleset['selectors'].append([])
    
                elif nname not in ('COMMENT'):
                    # we're just in a selector is all
                    ruleset['selectors'][-1].append((nname, value))
    
            elif in_block and not in_declaration:
                ruleset = rulesets[-1]
            
                if nname == 'IDENT':
                    # right at the start of a declaration
                    ruleset['declarations'].append({'property': [(nname, value)], 'value': [], 'position': (line, col)})
                    in_declaration = True
                    in_property = True
                    
                elif (nname == 'CHAR' and value == '}'):
                    # end of block
                    in_block = False

                elif nname not in ('S', 'COMMENT'):
                    # something else
                    raise ParseException('Unexpected %(nname)s while looking for a property' % locals(), line, col)
    
            elif in_declaration and in_property:
                declaration = rulesets[-1]['declarations'][-1]
            
                if nname == 'CHAR' and value == ':':
                    # end of property
                    declaration['property'] = postprocess_property(declaration['property'], line, col)
                    in_property = False
    
                elif nname not in ('COMMENT'):
                    # in a declaration property
                    declaration['property'].append((nname, value))
    
            elif in_declaration and not in_property:
                declaration = rulesets[-1]['declarations'][-1]
            
                if nname == 'CHAR' and value == ';':
                    # end of declaration
                    declaration['value'] = postprocess_value(declaration['value'], declaration['property'], line, col)
                    in_declaration = False
    
                elif nname not in ('COMMENT'):
                    # in a declaration value
                    declaration['value'].append((nname, value))

        except ParseException, e:
            #raise ParseException(e.message + ' (line %(line)d, column %(col)d)' % locals(), line, col)
            raise