1- #!/usr/bin/env python
1+ #!/usr/bin/env python3
22"""
33build.py
44
55Build HTML output
66"""
7- from __future__ import absolute_import , division , print_function
7+
88
99import re
1010import os
@@ -122,7 +122,7 @@ def tag_set_sort(item):
122122 for fn in non_idx_items :
123123 index_text .append (":doc:`{0} <items/{1}>`\n " .format (titles [fn ], fn ))
124124
125- with open (section_fn , 'w' ) as f :
125+ with open (section_fn , 'w' , encoding = 'utf-8' ) as f :
126126 sec_title = titles .get (section_base_fn , tag_id )
127127 f .write ("{0}\n {1}\n \n " .format (sec_title , "=" * len (sec_title )))
128128
@@ -143,7 +143,7 @@ def tag_set_sort(item):
143143 f .write (" {0}\n " .format (fn ))
144144
145145 # Write index
146- with open (index_rst , 'w' ) as f :
146+ with open (index_rst , 'w' , encoding = 'utf-8' ) as f :
147147 f .write (".. toctree::\n "
148148 " :maxdepth: 1\n "
149149 " :hidden:\n \n " )
@@ -186,7 +186,7 @@ def generate_files(dst_path):
186186 created_stamp = created .get (basename , 0 )
187187 modified_stamp = modified .get (basename , created_stamp )
188188 p = subprocess .Popen (['git' , 'log' , '--format=%at:%an' , 'ef45029096..' , fn ],
189- stdout = subprocess .PIPE )
189+ stdout = subprocess .PIPE , encoding = 'utf-8' )
190190 names , _ = p .communicate ()
191191 for name in names .splitlines ():
192192 timestamp , name = name .strip ().split (':' , 1 )
@@ -249,52 +249,46 @@ def convert_file(dst_path, fn, editors, created, modified):
249249 headers = container .xpath ('//h1' )
250250 if headers :
251251 title = headers [0 ].text
252- if isinstance (title , unicode ):
253- title = title .encode ('utf-8' )
254252 h1_parent = headers [0 ].getparent ()
255253 h1_parent .remove (headers [0 ])
256254
257- lines .extend ([u ".. raw:: html" , u "" ])
255+ lines .extend ([".. raw:: html" , "" ])
258256
259257 for element in head .getchildren ():
260258 if element .tag in ('script' ,):
261- text = lxml .html .tostring (element )
259+ text = lxml .html .tostring (element , encoding = 'utf-8' ). decode ( 'utf-8' )
262260 lines .extend (" " + x for x in text .splitlines ())
263261
264- text = lxml .html .tostring (container )
262+ text = lxml .html .tostring (container , encoding = 'utf-8' ). decode ( 'utf-8' )
265263
266- m = re .search (ur '<p>TAGS:\s*(.*)\s*</p>' , text )
264+ m = re .search (r '<p>TAGS:\s*(.*)\s*</p>' , text )
267265 if m :
268266 tag_line = m .group (1 ).strip ().replace (';' , ',' )
269- if isinstance (tag_line , unicode ):
270- tag_line = tag_line .encode ('utf-8' )
271267 tags .update ([x .strip () for x in tag_line .split ("," )])
272268 text = text [:m .start ()] + text [m .end ():]
273269
274- m = re .search (ur '<p>AUTHORS:\s*(.*)\s*</p>' , text )
270+ m = re .search (r '<p>AUTHORS:\s*(.*)\s*</p>' , text )
275271 if m :
276272 # Author lines override editors
277273 if legacy_editors :
278274 editors = []
279275 legacy_editors = False
280276 author_line = m .group (1 ).strip ().replace (';' , ',' )
281- if isinstance (author_line , unicode ):
282- author_line = author_line .encode ('utf-8' )
283277 for author in author_line .split ("," ):
284278 author = author .strip ()
285279 if author and author not in editors :
286280 editors .append (author )
287281
288282 text = text [:m .start ()] + text [m .end ():]
289283
290- text = text .replace (u 'attachments/{0}/' .format (basename ),
291- u '../_downloads/' )
284+ text = text .replace ('attachments/{0}/' .format (basename ),
285+ '../_downloads/' )
292286
293- lines .extend (u " " + x for x in text .splitlines ())
294- lines .append (u "" )
287+ lines .extend (" " + x for x in text .splitlines ())
288+ lines .append ("" )
295289
296290 # Produce output
297- text = u "\n " .join (lines ). encode ( 'utf-8' )
291+ text = "\n " .join (lines )
298292
299293 if not title :
300294 title = basename
@@ -317,15 +311,15 @@ def fmt_time(timestamp):
317311 updateinfo ,
318312 text )
319313
320- with open (rst_fn , 'w' ) as f :
314+ with open (rst_fn , 'w' , encoding = 'utf-8' ) as f :
321315 f .write (text )
322316 if authors :
323317 f .write ("\n \n .. sectionauthor:: {0}" .format (authors ))
324318 del text
325319
326320 attach_dir = os .path .join ('ipython' , 'attachments' , basename )
327321 if os .path .isdir (attach_dir ) and len (os .listdir (attach_dir )) > 0 :
328- with open (rst_fn , 'a' ) as f :
322+ with open (rst_fn , 'a' , encoding = 'utf-8' ) as f :
329323 f .write ("""
330324
331325.. rubric:: Attachments
@@ -343,7 +337,7 @@ def parse_wiki_legacy_tags():
343337 tags = [None , None , None ]
344338 items = {}
345339
346- with open ('wiki-legacy-tags.txt' , 'r' ) as f :
340+ with open ('wiki-legacy-tags.txt' , 'r' , encoding = 'utf-8' ) as f :
347341 prev_line = None
348342
349343 for line in f :
@@ -383,7 +377,7 @@ def parse_wiki_legacy_tags():
383377def parse_wiki_legacy_users ():
384378 items = {}
385379
386- with open ('wiki-legacy-users.txt' , 'r' ) as f :
380+ with open ('wiki-legacy-users.txt' , 'r' , encoding = 'utf-8' ) as f :
387381 for line in f :
388382 line = line .strip ()
389383 if not line or line .startswith ('#' ):
@@ -401,7 +395,7 @@ def parse_wiki_legacy_timestamps():
401395 created = {}
402396 modified = {}
403397
404- with open ('wiki-legacy-timestamps.txt' , 'r' ) as f :
398+ with open ('wiki-legacy-timestamps.txt' , 'r' , encoding = 'utf-8' ) as f :
405399 for line in f :
406400 line = line .strip ()
407401 if not line or line .startswith ('#' ):
0 commit comments