Skip to content

Commit 112a779

Browse files
committed
增加python3支持
1 parent e9e946d commit 112a779

File tree

3 files changed

+561
-24
lines changed

3 files changed

+561
-24
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ Bob Nystrom同时在Github上无私地提供原本:[munificent/game-programmin
2525

2626
## 编译本书
2727

28-
这本书使用Markdown写就(`book/`)。使用了一些Python脚本(`script/format.py`)、SASS文件(`asset/style.scss`)及HTML模板(`asset/template.html`)转换为最终的HTML文件(`html/`)。想要自行编译,需要安装Python 2.7,以及Python Markdown, Pygments,和SmartyPants:
28+
这本书使用Markdown写就(`book/`)。使用了一些Python脚本(`script/format.py`)、SASS文件(`asset/style.scss`)及HTML模板(`asset/template.html`)转换为最终的HTML文件(`html/`)。想要自行编译,需要安装Python,以及Python Markdown, Pygments,和SmartyPants (可能需要使用sudo指令或者管理员权限。):
2929

3030
$ pip install markdown
3131
$ pip install pygments
3232
$ pip install smartypants
3333

34-
可能需要使用sudo指令或者管理员权限。在此之后,使用:
3534

36-
$ python script/format.py
35+
在此之后,使用:
36+
37+
$ python script/format.py # Python 3
38+
39+
或者
40+
41+
$ python script/format_python2.py # Python 2.7+
3742

3843
请从本项目的根目录运行该脚本,本脚本也可以以监视模式运行:
3944

script/format.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
#!/usr/bin/python2.7
1+
#!/usr/bin/python3
22
# -*- coding: utf-8 -*-
33

44
# Converts from the source markup format to HTML for the web version.
55

6-
import sys
7-
reload(sys)
8-
sys.setdefaultencoding('utf8')
6+
import codecs
7+
import string
98

109
import glob
1110
import os
@@ -163,7 +162,7 @@ def format_file(path, nav, skip_up_to_date):
163162

164163
# Read the markdown file and preprocess it.
165164
contents = ''
166-
with open(path, 'r') as input:
165+
with open(path, 'r', encoding="utf8") as input:
167166
# Read each line, preprocessing the special codes.
168167
for line in input:
169168
stripped = line.lstrip()
@@ -186,15 +185,16 @@ def format_file(path, nav, skip_up_to_date):
186185
elif command == 'outline':
187186
isoutline = True
188187
else:
189-
print "UNKNOWN COMMAND:", command, args
188+
print("UNKNOWN COMMAND:", command, args)
190189

191190
elif extension != "xml" and stripped.startswith('#'):
192191
# Build the page navigation from the headers.
193192
index = stripped.find(" ")
194193
headertype = stripped[:index]
195194
header = pretty(stripped[index:].strip())
196195
anchor = header.lower().replace(' ', '-')
197-
anchor = anchor.translate(None, '.?!:/"')
196+
translator = str.maketrans('', '', '.?!:/"')
197+
anchor = anchor.translate(translator)
198198

199199
# Add an anchor to the header.
200200
contents += indentation + headertype
@@ -210,11 +210,11 @@ def format_file(path, nav, skip_up_to_date):
210210
modified = datetime.fromtimestamp(os.path.getmtime(path))
211211
mod_str = modified.strftime('%B %d, %Y')
212212

213-
with open("asset/template." + extension) as f:
213+
with open("asset/template." + extension, encoding="utf8") as f:
214214
template = f.read()
215215

216216
# Write the output.
217-
with open(output_path(basename), 'w') as out:
217+
with open(output_path(basename), 'w', encoding="utf8") as out:
218218
title_text = title
219219
section_header = ""
220220

@@ -257,19 +257,19 @@ def format_file(path, nav, skip_up_to_date):
257257
num_chapters += 1
258258
if word_count < 50:
259259
empty_chapters += 1
260-
print " {}".format(basename)
260+
print(" {}".format(basename))
261261
elif word_count < 2000:
262262
empty_chapters += 1
263-
print "{}-{} {} ({} words)".format(
264-
YELLOW, DEFAULT, basename, word_count)
263+
print("{}-{} {} ({} words)".format(
264+
YELLOW, DEFAULT, basename, word_count))
265265
else:
266266
total_words += word_count
267-
print "{}✓{} {} ({} words)".format(
268-
GREEN, DEFAULT, basename, word_count)
267+
print("{}✓{} {} ({} words)".format(
268+
GREEN, DEFAULT, basename, word_count))
269269
else:
270270
# 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))
273273

274274

275275
def clean_up_xml(output):
@@ -411,7 +411,7 @@ def navigation_to_html(chapter, headers):
411411

412412

413413
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:
415415
lines = source.readlines()
416416

417417
code = indentation + ' :::cpp\n'
@@ -450,8 +450,8 @@ def include_code(pattern, index, indentation):
450450
else:
451451
code_line = line[blockindent:]
452452
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))
455455
code += indentation + ' ' + code_line
456456

457457
else:
@@ -467,7 +467,7 @@ def buildnav(searchpath):
467467
nav = nav + '<h1><a href="/">目录</a></h1>\n'
468468

469469
# 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:
471471
innav = False
472472

473473
for line in source:
@@ -499,7 +499,7 @@ def check_sass():
499499
return
500500

501501
subprocess.call(['sass', 'asset/style.scss', 'html/style.css'])
502-
print "{}✓{} style.css".format(GREEN, DEFAULT)
502+
print("{}✓{} style.css".format(GREEN, DEFAULT))
503503

504504

505505
searchpath = ('book/*.markdown')

0 commit comments

Comments
 (0)