-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcss_to_sass.py
132 lines (109 loc) · 4.06 KB
/
css_to_sass.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
130
131
132
import sublime, sublime_plugin
import re
class CssToSass(sublime_plugin.TextCommand):
options = {
'openingBracket': '',
'closingBracket': '',
'colon': ':',
'unPrefix': False,
'keepColons': False,
}
depth = 0
def run(self, edit):
filename = self.view.file_name()
type = 'unknown'
if filename:
if filename.endswith('.sass'): type = 'sass'
elif filename.endswith('.styl'): type = 'stylus'
else:
if self.view.match_selector(0, 'source.sass'): type = 'sass'
elif self.view.match_selector(0, 'source.stylus'): type = 'stylus'
print(self.view.match_selector(0, 'source.stylus'))
if type is 'sass' or type is 'stylus':
settings= sublime.load_settings('css_to_sass.sublime-settings')
self.eol = self.detectEol()
self.indent = self.detectIndentation()
self.options['colon'] = ':' if type is 'sass' or settings.get('colon') else ''
self.convert(sublime.get_clipboard(), edit)
else:
self.view.run_command('paste')
def detectIndentation(self):
indent = self.view.settings().get('tab_size')
tabs = not self.view.settings().get('translate_tabs_to_spaces')
return '\t' if tabs else indent
def detectEol(self):
eol_style = self.view.line_endings().lower()
if (eol_style == 'windows'): eol = '\r\n'
elif (eol_style == 'cr'): eol = '\r'
else: eol = '\n'
return eol
def convert(self, text, edit):
if (";" in text):
sublime.set_clipboard(self.process())
self.view.run_command('paste_and_indent')
else:
self.view.run_command('paste')
def process(self):
text = sublime.get_clipboard()
tree = {'children': {}}
# Remove comments
text = re.sub("\/\*[\s\S]*?\*\/", "", text)
results = re.findall("([^{]+)\{([^}]+)\}", text)
# Process each css block
for (selector, declaration) in results:
selectors = []
path = tree
selector = selector.strip()
if re.search(",", selector):
path = self.addRule(path, selector)
else:
selector = re.sub("\s*([>\+~])\s*", r' &\1' , selector)
selector = re.sub("(\w)([:\.])", r'\1 &\2' , selector)
selectors = re.split("[\s]+", selector)
for item in selectors:
#fix back special chars
_sel = re.sub("&(.)", r'& \1 ', item)
_sel = re.sub("& ([:\.]) ", r'&\1', _sel)
path = self.addRule(path, _sel)
for (_property, value) in re.findall("([^:;]+):([^;]+)", declaration):
obj = {
"property": _property.strip(),
"value": value.strip()
}
path['declarations'].append(obj)
if len(results) == 0: return self.clean(text)
return self.generateOutput(tree)
def addRule(self, path, selector):
if selector in path['children']:
path['children'][selector] = path['children'][selector]
else:
path['children'][selector] = { 'children': {}, 'declarations': [] }
return path['children'][selector]
def generateOutput(self, tree):
output = ''
openingBracket = self.options['openingBracket']
for key in tree['children']:
sel = key
output += self.getIndent() + sel + openingBracket + '\n'
self.depth = self.depth + 1
declarations = tree['children'][key]['declarations']
for index, declaration in enumerate(declarations):
output += self.getIndent() + declaration['property'] + self.options['colon'] + ' ' + declaration['value'] + self.eol
output += self.generateOutput(tree['children'][key])
self.depth = self.depth - 1
output += self.getIndent() + self.options['closingBracket'] + '\n' + ('$n' if self.depth == 0 else '')
output = re.sub(u'(?imu)^\s*\n', u'', output)
output = re.sub('\$n', '\n', output)
return output
def getIndent(self):
if self.indent == '\t':
return '\t' * (self.depth + 1)
else:
return ' ' * self.indent * (self.depth + 1)
def clean(self, text):
print("asdasdas")
if self.options['colon'] == ":":
text = re.sub("(;|{|})", "", text)
else:
text = re.sub("(:|;|{|})", "", text)
return text