@@ -39,28 +39,26 @@ def preprocess_html_merge_css(src_path, dst_path):
3939
4040 with open (src_path , 'r' ) as a_file :
4141 content = a_file .read ()
42+ parser = etree .HTMLParser ()
43+ stripped = content .strip ()
44+ root = etree .fromstring (stripped , parser )
4245
4346 with warnings .catch_warnings ():
4447 warnings .simplefilter ("ignore" )
45- premailer = Premailer (content , base_url = src_path , preserve_all_links = True ,
48+ premailer = Premailer (root , base_url = src_path , preserve_all_links = True ,
4649 remove_classes = True )
47- content = premailer .transform ()
50+
51+ root = premailer .transform ().getroot ()
4852
4953 head = os .path .dirname (dst_path )
5054 os .makedirs (head , exist_ok = True )
5155
52- with open (dst_path ,"w" ) as a_file :
53- a_file .write (content )
54-
55- with open (dst_path , encoding = 'utf-8' ) as a_file :
56- # completely remove content of style tags and tags
57- parser = etree .HTMLParser ()
58- tree = etree .parse (StringIO (content ), parser )
59- nondata_tags = ['style' ]
60- strip_elements (tree , * nondata_tags )
56+ # completely remove content of style tags and tags
57+ nondata_tags = ['style' ]
58+ strip_elements (root , * nondata_tags )
6159
6260 with open (dst_path , 'wb' ) as a_file :
63- tree . write (dst_path , pretty_print = True , method = "html" ,
64- encoding = 'utf-8' )
61+ root . getroottree (). write (a_file , pretty_print = True , method = "html" ,
62+ encoding = 'utf-8' )
6563
6664 return output .getvalue ()
0 commit comments