2626import warnings
2727import 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
0 commit comments