new common.py · rvlambda/python-tutorial@04d459d · GitHub
Skip to content

Commit 04d459d

Browse files
committed
new common.py
1 parent 00dc3a6 commit 04d459d

3 files changed

Lines changed: 109 additions & 44 deletions

File tree

‎common.py‎

Lines changed: 96 additions & 0 deletions

‎make-html.py‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
"""Create HTML files of the tutorial."""
3030

3131
import os
32-
import re
3332
import shutil
3433
import string
3534
import sys
3635
import webbrowser
3736

37+
import common
38+
3839
try:
3940
import mistune
4041
except ImportError:
@@ -52,8 +53,6 @@
5253
pygments = None
5354

5455

55-
LINK_REGEX = r'\[.*\]\((.*\.md)\)'
56-
5756
HTML_TEMPLATE = """\
5857
<!DOCTYPE html>
5958
<html>
@@ -68,30 +67,8 @@
6867
"""
6968

7069

71-
def askyesno(question, default=True):
72-
"""Ask a yes/no question and return True or False.
73-
74-
The default answer is yes if default is True and no if default is
75-
False.
76-
"""
77-
if default:
78-
# yes by default
79-
question += ' [Y/n] '
80-
else:
81-
# no by default
82-
question += ' [y/N] '
83-
while True:
84-
result = input(question).upper().strip()
85-
if result == 'Y':
86-
return True
87-
if result == 'N':
88-
return False
89-
if not result:
90-
return default
91-
92-
9370
def mkdir_open(filename, mode):
94-
"""Like built-in open(), but make a directory as needed."""
71+
"""Like open(), but make directories as needed."""
9572
directory = os.path.dirname(filename)
9673
os.makedirs(directory, exist_ok=True)
9774
return open(filename, mode)
@@ -204,25 +181,21 @@ def main():
204181
print()
205182
print("You can also continue without Pygments, but the code examples")
206183
print("will not be in color.")
207-
if not askyesno("Continue without pygments?"):
184+
if not common.askyesno("Continue without pygments?"):
208185
print("Interrupt.")
209186
return
210187

211188
if os.path.exists('html'):
212-
if not askyesno("html exists. Do you want to remove it?"):
189+
if not common.askyesno("html exists. Do you want to remove it?"):
213190
print("Interrupt.")
214191
return
215192
if os.path.isdir('html'):
216193
shutil.rmtree('html')
217194
else:
218195
os.remove('html')
219196

220-
print("Getting a list of files to generate...")
221-
with open('README.md', 'r') as f:
222-
filelist = re.findall(LINK_REGEX, f.read()) + ['README.md']
223-
224197
print("Generating HTML files...")
225-
for markdownfile in filelist:
198+
for markdownfile in common.get_markdown_files():
226199
htmlfile = os.path.join('html', fix_filename(markdownfile))
227200
print(' ', markdownfile, '->', htmlfile)
228201
with open(markdownfile.replace('/', os.sep), 'r') as f:
@@ -241,7 +214,7 @@ def main():
241214
print("Ready! The files are in the html directory.")
242215
print("Go to html and double-click index.html to read the tutorial.")
243216
print()
244-
if askyesno("Do you want to view the tutorial now?", default=False):
217+
if common.askyesno("Do you want to view the tutorial now?", default=False):
245218
print("Opening the tutorial...")
246219
webbrowser.open(os.path.join('html', 'index.html'))
247220

‎update-ends.py‎

Lines changed: 6 additions & 10 deletions

0 commit comments

Comments
 (0)