-
Notifications
You must be signed in to change notification settings - Fork 131
/
cssformatter.py
74 lines (56 loc) · 2.21 KB
/
cssformatter.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
# @author Avtandil Kikabidze
# @copyright Copyright (c) 2008-2015, Avtandil Kikabidze aka LONGMAN (akalongman@gmail.com)
# @link http://longman.me
# @license The MIT License (MIT)
import re
import cssbeautifier
class CssFormatter:
def __init__(self, formatter):
self.formatter = formatter
self.options = cssbeautifier.default_options()
# parse custom options from settings file
self.fill_custom_options(formatter.settings.get('codeformatter_css_options'))
def fill_custom_options(self, options):
if not options:
return
# map of key, value if key exists and value if key not exists
custom_options = [
('indent_size', None, 4),
('indent_char', None, ' '),
('indent_with_tabs', True, False),
('selector_separator_newline', True, False),
('end_with_newline', True, False),
('eol', None, '\n'),
('space_around_combinator', True, False),
('newline_between_rules', True, False),
('format_on_save', None, False)
]
casters = {'indent_char': str}
for key, on_value, off_value in custom_options:
if key not in options:
value = off_value
else:
value = options[key]
if value and on_value:
value = on_value
else:
cast = casters.get(key)
if cast:
value = cast(value)
setattr(self.options, key, value)
def format(self, text):
text = text.decode('utf-8')
stderr = ''
stdout = ''
try:
stdout = cssbeautifier.beautify(text, self.options)
except Exception as e:
stderr = str(e)
if (not stderr and not stdout):
stderr = 'Formatting error!'
return stdout, stderr
def format_on_save_enabled(self, file_name):
format_on_save = getattr(self.options, 'format_on_save', False)
if isinstance(format_on_save, str):
format_on_save = re.search(format_on_save, file_name) is not None
return format_on_save