| OLD | NEW |
| (Empty) |
| 1 from scss import SORTING | |
| 2 | |
| 3 | |
| 4 class Node(object): | |
| 5 """ Base class for parsed objects. | |
| 6 """ | |
| 7 delim = ' ' | |
| 8 root = None | |
| 9 | |
| 10 def __init__(self, s, n, t): | |
| 11 self.num, self.data = n, t | |
| 12 self.parent = self._ctx = None | |
| 13 | |
| 14 def __str__(self): | |
| 15 return self.delim.join(map(str, self.data)) | |
| 16 | |
| 17 def __repr__(self): | |
| 18 return '(%s%s)' % ( | |
| 19 self.__class__.__name__, | |
| 20 ': %s' % ' '.join(map(repr, self.data)) if self.data else '' | |
| 21 ) | |
| 22 | |
| 23 def parse(self, target): | |
| 24 self.parent = target | |
| 25 | |
| 26 def copy(self): | |
| 27 return self | |
| 28 | |
| 29 @property | |
| 30 def ctx(self): | |
| 31 if self._ctx: | |
| 32 return self._ctx | |
| 33 | |
| 34 if self.parent: | |
| 35 return self.parent.ctx | |
| 36 | |
| 37 self._ctx = dict() | |
| 38 return self._ctx | |
| 39 | |
| 40 @ctx.setter | |
| 41 def ctx(self, value): | |
| 42 self._ctx = value | |
| 43 | |
| 44 | |
| 45 class Empty(Node): | |
| 46 | |
| 47 def __str__(self): | |
| 48 return '' | |
| 49 | |
| 50 | |
| 51 class ParseNode(Node): | |
| 52 | |
| 53 def parse(self, target): | |
| 54 super(ParseNode, self).parse(target) | |
| 55 for n in self.data: | |
| 56 if isinstance(n, Node): | |
| 57 n.parse(self) | |
| 58 | |
| 59 def copy(self): | |
| 60 t = [n.copy() if isinstance(n, Node) else n for n in self.data] | |
| 61 return self.__class__(None, self.num, t) | |
| 62 | |
| 63 | |
| 64 class ContentNode(ParseNode): | |
| 65 | |
| 66 def __init__(self, s, n, t): | |
| 67 super(ContentNode, self).__init__(s, n, t) | |
| 68 self.name = self.data[0] if self.data else '' | |
| 69 self.declareset = [] | |
| 70 self.ruleset = [] | |
| 71 | |
| 72 def __str__(self): | |
| 73 # Sort declaration | |
| 74 if self.root.get_opt('sort'): | |
| 75 self.declareset.sort( | |
| 76 key=lambda x: SORTING.get(x.name, 999 )) | |
| 77 | |
| 78 nl, ws, ts = self.root.cache['delims'] | |
| 79 semicolon = '' if self.root.cache['opts'].get('compress') else ';' | |
| 80 | |
| 81 return ''.join(( | |
| 82 | |
| 83 # Self | |
| 84 ''.join(( | |
| 85 | |
| 86 # Selector tree | |
| 87 str(self.name), | |
| 88 | |
| 89 "{%s%s%s" % (nl, ws, ws) if self.name else '', | |
| 90 | |
| 91 # Declarations | |
| 92 (';%s%s%s' % ( nl, ws, ws )).join(str(d) for d in self.declarese
t), | |
| 93 | |
| 94 semicolon, | |
| 95 | |
| 96 '%s}%s%s' % ( nl, nl, nl ) if self.name else '' | |
| 97 | |
| 98 )) if self.declareset else '', | |
| 99 | |
| 100 # Children | |
| 101 ''.join(str(r) for r in self.ruleset) | |
| 102 )) | |
| 103 | |
| 104 | |
| 105 class IncludeNode(ParseNode): | |
| 106 | |
| 107 def parse(self, target): | |
| 108 for node in self.data: | |
| 109 if isinstance(node, Node): | |
| 110 node.ctx.update(self.ctx) | |
| 111 node.parse(target) | |
| 112 | |
| 113 def __str__(self): | |
| 114 node = ContentNode(None, None, []) | |
| 115 self.parse(node) | |
| 116 return str(node) | |
| OLD | NEW |