forked from metalshark/lesscss-python
/
mixin.py
129 lines (88 loc) · 2.86 KB
/
mixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright 2010 Beech Horn
This file is part of lesscss-python.
lesscss-python is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
lesscss-python is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with lesscss-python. If not, see <http://www.gnu.org/licenses/>.
'''
import re
from nested import parse_nested
from params import Param, parse_params
from property import Property
from rules import Rules
MIXIN = re.compile('''
(?P<name>
\.
[a-z 0-9 \- _ \* \s , :]+
)
\s*
(
(?P<param_detect>
\(
(?P<params>
.*?
)
)
\)
\s*
)?
\s*
(
(?P<nested>
{
)
|
;
|
}
)
''', re.DOTALL | re.VERBOSE)
def parse_mixin(less, parent=None, **kwargs):
match = MIXIN.match(less)
if not match:
raise ValueError()
code = match.group()
contents = None
if match.group('nested'):
matched_length = len(match.group())
remaining_less = less[matched_length:]
contents = parse_nested(remaining_less)
code += contents + '}'
params = parse_params(match.group('params'))
if contents:
for param in params:
if param['value'] and not param['name']:
param['name'] = param['value']
param['value'] = None
name = match.group('name')
if match.group('nested') and not match.group('param_detect'):
raise ValueError()
return Mixin(parent=parent, code=code, name=name, params=params,
contents=contents)
class Mixin(Rules):
__slots__ = ['__name', '__params']
def __init__(self, parent, code, name, params, contents):
Rules.__init__(self, parent=parent, code=code, contents=contents)
self.__name = name
self.__params = list()
for param in params:
param = Param(code=param['code'],
name=param['name'],
value=param['value'],
parent=self)
self.__params.append(param)
def __get_name(self):
return self.__name
def __get_params(self):
return self.__params
name = property(fget=__get_name)
params = property(fget=__get_params)