/
htmlformatter.py
115 lines (87 loc) · 3.16 KB
/
htmlformatter.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
# @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 os
import sys
import re
import htmlbeautifier
import sublime
directory = os.path.dirname(os.path.realpath(__file__))
libs_path = os.path.join(directory, 'lib', 'htmlbeautifier')
if libs_path not in sys.path:
sys.path.append(libs_path)
use_bs4 = True
try:
from bs4 import BeautifulSoup
except:
use_bs4 = False
class HtmlFormatter:
def __init__(self, formatter):
self.formatter = formatter
self.options = htmlbeautifier.default_options()
# fill custom options
custom_options = formatter.settings.get('codeformatter_html_options')
self.fill_custom_options(custom_options)
def fill_custom_options(self, options):
if not options:
return
custom_options = [
'formatter_version',
'indent_size',
'indent_char',
'minimum_attribute_count',
'first_attribute_on_new_line',
'indent_with_tabs',
'expand_tags',
'reduce_empty_tags',
'reduce_whole_word_tags',
'exception_on_tag_mismatch',
'custom_singletons',
'format_on_save'
]
casters = {'indent_char': str}
for key in custom_options:
value = options.get(key)
if value is None:
continue
cast = casters.get(key)
if cast:
value = cast(value)
setattr(self.options, key, value)
def format_with_bs4(self, text):
stdout, stderr = '', ''
p_indent_size = getattr(self.options, 'indent_size', 4)
try:
soup = BeautifulSoup(text, 'html.parser')
stdout = soup.prettify(formatter=None, indent_size=p_indent_size)
except Exception as e:
stderr = str(e)
return stdout, stderr
def format_with_beautifier(self, text):
stdout, stderr = '', ''
try:
stdout = htmlbeautifier.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(self, text):
text = text.decode('utf-8')
formatter = getattr(self.options, 'formatter_version')
if formatter == 'bs4':
if not use_bs4:
sublime.error_message(
u'CodeFormatter\n\nUnable to load BeautifulSoup HTML '
u'formatter. The old RegExp-based formatter was '
u'automatically used for you instead.'
)
else:
return self.format_with_bs4(text)
return self.format_with_beautifier(text)
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