From 1d6147c6cdbef1b4987752ff09e31dbb263d2e78 Mon Sep 17 00:00:00 2001 From: TheVice Date: Sun, 6 Mar 2016 18:29:25 +0200 Subject: [PATCH] [Python3] Added support to make book at python 3 environment, change instruction for describe the process how to make with python 3. --- README.md | 12 ++++++------ script/format.py | 31 +++++++++++++++++-------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a5e4c85d..d1823993 100644 --- a/README.md +++ b/README.md @@ -3,19 +3,19 @@ Issues and pull requests are more than welcome! ## Building the Book -The book is written in Markdown (in `book/`). A little Python script (`script/format.py`) converts that along with a SASS file (`asset/style.scss`) and HTML template (`asset/template.html`) to the final HTML (in `html/`). To run the format script locally, you'll need to have Python 2.7-ish, and install Python Markdown, Pygments, and SmartyPants: +The book is written in Markdown (in `book/`). A little Python script (`script/format.py`) converts that along with a SASS file (`asset/style.scss`) and HTML template (`asset/template.html`) to the final HTML (in `html/`). To run the format script locally, you'll need to have Python 3.5-ish, and install Python Markdown, Pygments, and SmartyPants: - $ pip install markdown - $ pip install pygments - $ pip install smartypants + $ pip3 install markdown + $ pip3 install pygments + $ pip3 install smartypants You may need `sudo` for those. Once that's done, you can run: - $ python script/format.py + $ python3 script/format.py Make sure to run this from the root directory of the repo. That will regenerate all of the chapter and section intro HTML files. If you're editing stuff, the script can also be run in watch mode: - $ python script/format.py --watch + $ python3 script/format.py --watch That will monitor the file system for changes to the markdown files, SASS file, or HTML template, and reprocess them as needed. diff --git a/script/format.py b/script/format.py index 9dc7297a..1ecd7753 100755 --- a/script/format.py +++ b/script/format.py @@ -152,7 +152,7 @@ def format_file(path, nav, skip_up_to_date): elif command == 'outline': isoutline = True else: - print "UNKNOWN COMMAND:", command, args + print ("UNKNOWN COMMAND:", command, args) elif extension != "xml" and stripped.startswith('#'): # Build the page navigation from the headers. @@ -160,7 +160,10 @@ def format_file(path, nav, skip_up_to_date): headertype = stripped[:index] header = pretty(stripped[index:].strip()) anchor = header.lower().replace(' ', '-') - anchor = anchor.translate(None, '.?!:/"') + if 2 == sys.version_info[0]: + anchor = anchor.translate(None, '.?!:/"') + else: + anchor = anchor.translate('.?!:/"') # Add an anchor to the header. contents += indentation + headertype @@ -224,19 +227,19 @@ def format_file(path, nav, skip_up_to_date): num_chapters += 1 if word_count < 50: empty_chapters += 1 - print " {}".format(basename) + print (" {}".format(basename)) elif word_count < 2000: empty_chapters += 1 - print "{}-{} {} ({} words)".format( - YELLOW, DEFAULT, basename, word_count) + print ("{}-{} {} ({} words)".format( + YELLOW, DEFAULT, basename, word_count)) else: total_words += word_count - print "{}✓{} {} ({} words)".format( - GREEN, DEFAULT, basename, word_count) + print ("{}✓{} {} ({} words)".format( + GREEN, DEFAULT, basename, word_count)) else: # Section header chapters aren't counted like regular chapters. - print "{}•{} {} ({} words)".format( - GREEN, DEFAULT, basename, word_count) + print ("{}•{} {} ({} words)".format( + GREEN, DEFAULT, basename, word_count)) def clean_up_xml(output): @@ -417,8 +420,8 @@ def include_code(pattern, index, indentation): else: code_line = line[blockindent:] if len(code_line) > 64: - print "Warning long source line ({} chars):\n{}".format( - len(code_line), code_line) + print ("Warning long source line ({} chars):\n{}".format( + len(code_line), code_line)) code += indentation + ' ' + code_line else: @@ -466,7 +469,7 @@ def check_sass(): return subprocess.call(['sass', 'asset/style.scss', 'html/style.css']) - print "{}✓{} style.css".format(GREEN, DEFAULT) + print ("{}✓{} style.css".format(GREEN, DEFAULT)) searchpath = ('book/*.markdown') @@ -494,5 +497,5 @@ def check_sass(): estimated_word_count = total_words + (empty_chapters * average_word_count) percent_finished = total_words * 100 / estimated_word_count - print "{}/~{} words ({}%)".format( - total_words, estimated_word_count, percent_finished) + print ("{}/~{} words ({}%)".format( + total_words, estimated_word_count, percent_finished))