def test_to_ast(): css1 = ("margin-bottom:3em; " "margin-top:2em; " "border: solid black 1px") css2 = ("border:solid black 1px;" "margin-bottom: 3em; " "margin-top:2em") res1 = to_ast(css1) res2 = to_ast(css2) assert res1 == res2
def handle_starttag(self, tag, attributes): """ Appends start tags to the output. If a style tag is present, its contents are added to the current stylesheet, and it is replaced by a class tag pointing to the same style information. """ pred = lambda x: x[0] == 'style' styles, attrs = partition(attributes, pred) attrs = ['%s="%s"' % (a, v) for (a, v) in attrs] if styles: _, value = styles[0] # list has one 2-tuple s = to_ast(value) # create a new entry if s not in self.sty: self.sty[s] = self.next self.next += 1 attribute = 'class="{prefix}{num}"'.format(prefix=self.prefix, num=self.sty[s]) attrs.append(attribute) res = '<{tag}{attrs_str}>'.format(tag=tag, attrs_str=(' ' + ' '.join( attrs) if attrs else ' ')) self.out.append(res)