Skip to content

Commit 4df4b7c

Browse files
committed
Preprocess: extract file operations from preprocess_html_merge_cssless()
1 parent 743539d commit 4df4b7c

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

commands/preprocess_cssless.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,24 @@
2626
import warnings
2727
import io
2828

29-
def preprocess_html_merge_css(src_path, dst_path):
29+
def preprocess_html_merge_cssless(src_path, dst_path):
30+
with open(src_path, 'r') as a_file:
31+
content = a_file.read()
32+
parser = etree.HTMLParser()
33+
stripped = content.strip()
34+
root = etree.fromstring(stripped, parser)
35+
36+
output = preprocess_html_merge_css(root, src_path)
37+
38+
head = os.path.dirname(dst_path)
39+
os.makedirs(head, exist_ok=True)
40+
41+
with open(dst_path, 'wb') as a_file:
42+
root.getroottree().write(a_file, pretty_print=True, method="html",
43+
encoding='utf-8')
44+
return output.getvalue()
45+
46+
def preprocess_html_merge_css(root, src_path):
3047
log = logging.Logger('ignore')
3148
output = io.StringIO()
3249
handler = logging.StreamHandler(stream=output)
@@ -37,28 +54,14 @@ def preprocess_html_merge_css(src_path, dst_path):
3754
# warnings to stderr in non-verbose mode
3855
cssutils.log.setLog(log)
3956

40-
with open(src_path, 'r') as a_file:
41-
content = a_file.read()
42-
parser = etree.HTMLParser()
43-
stripped = content.strip()
44-
root = etree.fromstring(stripped, parser)
45-
4657
with warnings.catch_warnings():
4758
warnings.simplefilter("ignore")
4859
premailer = Premailer(root, base_url=src_path,
4960
disable_link_rewrites=True, remove_classes=True)
50-
5161
root = premailer.transform().getroot()
5262

53-
head = os.path.dirname(dst_path)
54-
os.makedirs(head, exist_ok=True)
55-
5663
# completely remove content of style tags and tags
5764
nondata_tags = ['style']
5865
strip_elements(root, *nondata_tags)
5966

60-
with open(dst_path, 'wb') as a_file:
61-
root.getroottree().write(a_file, pretty_print=True, method="html",
62-
encoding='utf-8')
63-
64-
return output.getvalue()
67+
return output

tests/test_preprocess_cssless.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
from commands.preprocess_cssless import *
2121

2222
class TestPreprocessHtmlMergeCss(unittest.TestCase):
23-
def test_preprocess_html_merge_css(self):
23+
def test_preprocess_html_merge_cssless(self):
2424
dir_path = os.path.dirname(__file__)
2525
src_path = os.path.join(dir_path, 'preprocess_cssless_data/multiset.html')
2626
dst_path = os.path.join(dir_path, 'preprocess_cssless_data/multiset_out.html')
2727
expected_path = os.path.join(dir_path, 'preprocess_cssless_data/multiset_expected.html')
2828

29-
preprocess_html_merge_css(src_path, dst_path)
29+
preprocess_html_merge_cssless(src_path, dst_path)
3030

3131
with open(dst_path, 'r') as a_file:
3232
test = a_file.read()

0 commit comments

Comments
 (0)