1
- #!/usr/bin/python2.7
1
+ #!/usr/bin/python3
2
2
# -*- coding: utf-8 -*-
3
3
4
4
# Converts from the source markup format to HTML for the web version.
5
5
6
- import sys
7
- reload (sys )
8
- sys .setdefaultencoding ('utf8' )
6
+ import codecs
7
+ import string
9
8
10
9
import glob
11
10
import os
@@ -163,7 +162,7 @@ def format_file(path, nav, skip_up_to_date):
163
162
164
163
# Read the markdown file and preprocess it.
165
164
contents = ''
166
- with open (path , 'r' ) as input :
165
+ with open (path , 'r' , encoding = "utf8" ) as input :
167
166
# Read each line, preprocessing the special codes.
168
167
for line in input :
169
168
stripped = line .lstrip ()
@@ -186,15 +185,16 @@ def format_file(path, nav, skip_up_to_date):
186
185
elif command == 'outline' :
187
186
isoutline = True
188
187
else :
189
- print "UNKNOWN COMMAND:" , command , args
188
+ print ( "UNKNOWN COMMAND:" , command , args )
190
189
191
190
elif extension != "xml" and stripped .startswith ('#' ):
192
191
# Build the page navigation from the headers.
193
192
index = stripped .find (" " )
194
193
headertype = stripped [:index ]
195
194
header = pretty (stripped [index :].strip ())
196
195
anchor = header .lower ().replace (' ' , '-' )
197
- anchor = anchor .translate (None , '.?!:/"' )
196
+ translator = str .maketrans ('' , '' , '.?!:/"' )
197
+ anchor = anchor .translate (translator )
198
198
199
199
# Add an anchor to the header.
200
200
contents += indentation + headertype
@@ -210,11 +210,11 @@ def format_file(path, nav, skip_up_to_date):
210
210
modified = datetime .fromtimestamp (os .path .getmtime (path ))
211
211
mod_str = modified .strftime ('%B %d, %Y' )
212
212
213
- with open ("asset/template." + extension ) as f :
213
+ with open ("asset/template." + extension , encoding = "utf8" ) as f :
214
214
template = f .read ()
215
215
216
216
# Write the output.
217
- with open (output_path (basename ), 'w' ) as out :
217
+ with open (output_path (basename ), 'w' , encoding = "utf8" ) as out :
218
218
title_text = title
219
219
section_header = ""
220
220
@@ -257,19 +257,19 @@ def format_file(path, nav, skip_up_to_date):
257
257
num_chapters += 1
258
258
if word_count < 50 :
259
259
empty_chapters += 1
260
- print " {}" .format (basename )
260
+ print ( " {}" .format (basename ) )
261
261
elif word_count < 2000 :
262
262
empty_chapters += 1
263
- print "{}-{} {} ({} words)" .format (
264
- YELLOW , DEFAULT , basename , word_count )
263
+ print ( "{}-{} {} ({} words)" .format (
264
+ YELLOW , DEFAULT , basename , word_count ))
265
265
else :
266
266
total_words += word_count
267
- print "{}✓{} {} ({} words)" .format (
268
- GREEN , DEFAULT , basename , word_count )
267
+ print ( "{}✓{} {} ({} words)" .format (
268
+ GREEN , DEFAULT , basename , word_count ))
269
269
else :
270
270
# Section header chapters aren't counted like regular chapters.
271
- print "{}•{} {} ({} words)" .format (
272
- GREEN , DEFAULT , basename , word_count )
271
+ print ( "{}•{} {} ({} words)" .format (
272
+ GREEN , DEFAULT , basename , word_count ))
273
273
274
274
275
275
def clean_up_xml (output ):
@@ -411,7 +411,7 @@ def navigation_to_html(chapter, headers):
411
411
412
412
413
413
def include_code (pattern , index , indentation ):
414
- with open (cpp_path (pattern ), 'r' ) as source :
414
+ with open (cpp_path (pattern ), 'r' , encoding = "utf8" ) as source :
415
415
lines = source .readlines ()
416
416
417
417
code = indentation + ' :::cpp\n '
@@ -450,8 +450,8 @@ def include_code(pattern, index, indentation):
450
450
else :
451
451
code_line = line [blockindent :]
452
452
if len (code_line ) > 64 :
453
- print "Warning long source line ({} chars):\n {}" .format (
454
- len (code_line ), code_line )
453
+ print ( "Warning long source line ({} chars):\n {}" .format (
454
+ len (code_line ), code_line ))
455
455
code += indentation + ' ' + code_line
456
456
457
457
else :
@@ -467,7 +467,7 @@ def buildnav(searchpath):
467
467
nav = nav + '<h1><a href="/">目录</a></h1>\n '
468
468
469
469
# Read the chapter outline from the index page.
470
- with open ('html/index.html' , 'r' ) as source :
470
+ with open ('html/index.html' , 'r' , encoding = "utf8" ) as source :
471
471
innav = False
472
472
473
473
for line in source :
@@ -499,7 +499,7 @@ def check_sass():
499
499
return
500
500
501
501
subprocess .call (['sass' , 'asset/style.scss' , 'html/style.css' ])
502
- print "{}✓{} style.css" .format (GREEN , DEFAULT )
502
+ print ( "{}✓{} style.css" .format (GREEN , DEFAULT ) )
503
503
504
504
505
505
searchpath = ('book/*.markdown' )
0 commit comments