Skip to content

Commit b521029

Browse files
committed
simplifying the scripts
1 parent e2fa059 commit b521029

File tree

4 files changed

+57
-93
lines changed

4 files changed

+57
-93
lines changed

common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#!/usr/bin/env python3
2-
31
# This is free and unencumbered software released into the public
42
# domain.
53

@@ -67,7 +65,8 @@ def find_links(file):
6765
def get_markdown_files():
6866
"""Yield the names of all markdown files in this tutorial.
6967
70-
This assumes that the README contains links to everything.
68+
This assumes that the README contains links to everything. The
69+
yielded paths use / as the path separator.
7170
"""
7271
yield 'README.md'
7372
with open('README.md', 'r') as f:

linkcheck.py

Lines changed: 41 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -41,93 +41,62 @@
4141
[local header](#some-header)
4242
"""
4343

44-
# The markdown files use posix-style paths, so we need posixpath for
45-
# processing them. See help('posixpath').
4644
import os
4745
import posixpath
4846

4947
import common
5048

5149

52-
class Link:
50+
def check(filepath, target):
51+
"""Check if a link's target is like it should be.
5352
54-
def __init__(self, regexmatch, filepath, lineno):
55-
# The .group(0) is not perfect, but it's good enough.
56-
self.markdown = regexmatch.group(0)
57-
self.text = regexmatch.group(1)
58-
self.target = regexmatch.group(2)
59-
self.filepath = filepath
60-
self.lineno = lineno
61-
self.status = None
53+
Return an error message string or "ok".
54+
"""
55+
if target.startswith(('http://', 'https://')):
56+
# We don't need this currently, but checking these links could
57+
# be added later.
58+
return "ok"
6259

63-
def _get_status(self):
64-
if self.target.startswith(('http://', 'https://')):
65-
# Checking for http(s) links can be added later, but
66-
# currently it's not needed.
60+
if '#' in target:
61+
where = target.index('#')
62+
if where == 0:
63+
# It's a link to a title in the same file, we need to skip it.
6764
return "ok"
68-
69-
target = self.target
70-
if '#' in target:
71-
where = target.index('#')
72-
if where == 0:
73-
# It's a link to a title in the same file, we need to
74-
# skip it.
75-
return "ok"
76-
target = target[:where]
77-
78-
path = posixpath.join(posixpath.dirname(self.filepath), target)
79-
realpath = path.replace('/', os.sep)
80-
81-
if not os.path.exists(realpath):
82-
return "doesn't exist"
83-
if target.endswith('/'):
84-
# A directory.
85-
if os.path.isdir(realpath):
86-
return "ok"
87-
return "not a directory"
88-
else:
89-
# A file.
90-
if os.path.isfile(realpath):
91-
return "ok"
92-
return "not a file"
93-
94-
def check(self):
95-
"""Check if the link's target is like it should be.
96-
97-
Return an error message string or "ok". The return value is also
98-
assigned to the status attribute.
99-
"""
100-
self.status = self._get_status()
101-
return self.status
102-
103-
def print_status(self):
104-
print(" file {0.filepath}, line {0.lineno}: {0.status}".format(self))
105-
print(" " + self.markdown)
106-
print()
65+
target = target[:where]
66+
67+
path = posixpath.join(posixpath.dirname(filepath), target)
68+
realpath = common.slashfix(path)
69+
if not os.path.exists(realpath):
70+
return "doesn't exist"
71+
if target.endswith('/'):
72+
# A directory.
73+
if os.path.isdir(realpath):
74+
return "ok"
75+
return "not a directory"
76+
else:
77+
# A file.
78+
if os.path.isfile(realpath):
79+
return "ok"
80+
return "not a file"
10781

10882

10983
def main():
110-
print("Searching links...")
111-
links = []
84+
print("Searching and checking links...")
85+
broken = 0
86+
total = 0
11287
for path in common.get_markdown_files():
11388
with common.slashfix_open(path, 'r') as f:
11489
for match, lineno in common.find_links(f):
115-
links.append(Link(match, path, lineno))
116-
print(" found", len(links), "links")
117-
118-
print("Checking for broken links...")
119-
brokens = 0
120-
for link in links:
121-
if link.check() != "ok":
122-
link.print_status()
123-
brokens += 1
124-
125-
if brokens == 0:
126-
print("All links seem to be OK.")
127-
elif brokens == 1:
128-
print("1 link is broken!")
129-
else:
130-
print(brokens, "links are broken!")
90+
text, target = match.groups()
91+
status = check(path, target)
92+
if status != "ok":
93+
# The .group(0) is not perfect, but it's good enough.
94+
print(" file %s, line %d: %s" % (path, lineno, status))
95+
print(" " + match.group(0))
96+
print()
97+
broken += 1
98+
total += 1
99+
print("%d/%d links seem to be broken." % (broken, total))
131100

132101

133102
if __name__ == '__main__':

make-html.py

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

3131
import os
32+
import posixpath
3233
import shutil
3334
import string
3435
import sys
@@ -69,14 +70,14 @@
6970

7071
def mkdir_slashfix_open(filename, mode):
7172
"""Like common.slashfix_open(), but make directories as needed."""
72-
filename = common.slashfix(filename)
73-
directory = os.path.dirname(filename)
73+
real_filename = common.slashfix(filename)
74+
directory = os.path.dirname(real_filename)
7475
os.makedirs(directory, exist_ok=True)
75-
return open(filename, mode)
76+
return open(real_filename, mode)
7677

7778

7879
def fix_filename(filename):
79-
if filename == 'README.md' or filename.endswith('/README.md'):
80+
if posixpath.basename(filename) == 'README.md':
8081
# 'README.md' -> 'index.html'
8182
# 'some/place/README.md' -> 'some/place/index.html'
8283
return filename[:-9] + 'index.html'
@@ -156,8 +157,7 @@ def block_code(self, code, lang=None):
156157
else:
157158
lexer = pygments.lexers.PythonLexer()
158159
formatter = pygments.formatters.HtmlFormatter(
159-
style='tango',
160-
noclasses=True)
160+
style='tango', noclasses=True)
161161
return pygments.highlight(code, lexer, formatter)
162162
# we can't highlight it
163163
return super().block_code(code, lang)
@@ -197,7 +197,7 @@ def main():
197197

198198
print("Generating HTML files...")
199199
for markdownfile in common.get_markdown_files():
200-
htmlfile = os.path.join('html', fix_filename(markdownfile))
200+
htmlfile = posixpath.join('html', fix_filename(markdownfile))
201201
print(' ', markdownfile, '->', htmlfile)
202202
with common.slashfix_open(markdownfile, 'r') as f:
203203
markdown = f.read()

update-ends.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,23 @@ def main():
109109

110110
print("Chapter files:")
111111
for prevpath, thispath, nextpath in zip(prevs, chapter_files, nexts):
112-
# thispath is always like 'section/file.md', never e.g. 'README.md'
113-
thissection, thisfile = thispath.split('/')
114-
prev = posixpath.relpath(prevpath, thissection)
115-
next_ = posixpath.relpath(nextpath, thissection)
112+
where = posixpath.dirname(thispath)
113+
prev = posixpath.relpath(prevpath, where)
114+
next_ = posixpath.relpath(nextpath, where)
116115
extralinks = "[Previous](%s) | [Next](%s) |\n" % (prev, next_)
117116
end = END_TEMPLATE.format(
118117
license='../LICENSE', readme='../README.md',
119-
extralinks=extralinks, readmeheader=thissection)
118+
extralinks=extralinks, readmeheader=where)
120119
update_end(thispath, end)
121120

122121
print()
123122

124123
print("Other files:")
125124
for filename in other_files:
126-
# move to the top level as needed
127-
parts = ['..'] * filename.count('/')
128-
licenseparts = parts + ['LICENSE']
129-
readmeparts = parts + ['README.md']
125+
where = posixpath.dirname(filename)
130126
end = END_TEMPLATE.format(
131-
license=posixpath.join(*licenseparts),
132-
readme=posixpath.join(*readmeparts),
127+
readme=posixpath.relpath('README.md', where),
128+
license=posixpath.relpath('LICENSE', where),
133129
extralinks="", readmeheader='list-of-contents')
134130
update_end(filename, end)
135131

0 commit comments

Comments
 (0)