Skip to content

Commit e182e8b

Browse files
committed
cleaning up stuff
1 parent e5ed260 commit e182e8b

File tree

4 files changed

+32
-50
lines changed

4 files changed

+32
-50
lines changed

common.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import itertools
3232
import re
33-
import sys
3433

3534

3635
_LINK_REGEX = r'\[(.*?)\]\((.*?)\)'
@@ -44,12 +43,12 @@ def find_links(file):
4443
# don't yield same link twice
4544
seen = set()
4645

47-
# we need to loop over the file two lines at a time to support
46+
# we need to loop over the file two lines at a time to support
4847
# multi-line (actually two-line) links, so this is kind of a mess
4948
firsts, seconds = itertools.tee(file)
5049
next(seconds) # first line is never second line
5150

52-
# we want 1-based indexing instead of 0-based and one-line links get
51+
# we want 1-based indexing instead of 0-based and one-line links get
5352
# caught from linepair[1], so we need to start at two
5453
for lineno, linepair in enumerate(zip(firsts, seconds), start=2):
5554
lines = linepair[0] + linepair[1]
@@ -68,7 +67,7 @@ def get_markdown_files():
6867
with open('README.md', 'r') as f:
6968
for match, lineno in find_links(f):
7069
target = match.group(2)
71-
# Currently the README doesn't link to itself, but I don't
70+
# Currently the README doesn't link to itself, but I don't
7271
# want to break things if it will in the future.
7372
if target.endswith('.md') and target != 'README.md':
7473
yield target

linkcheck.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@
4343

4444
# The markdown files use posix-style paths, so we need posixpath for
4545
# processing them. See help('posixpath').
46-
import collections
4746
import os
4847
import posixpath
49-
import re
5048

5149
import common
5250

@@ -64,15 +62,15 @@ def __init__(self, regexmatch, filepath, lineno):
6462

6563
def _get_status(self):
6664
if self.target.startswith(('http://', 'https://')):
67-
# Checking for http(s) links can be added later, but
65+
# Checking for http(s) links can be added later, but
6866
# currently it's not needed.
6967
return "ok"
7068

7169
target = self.target
7270
if '#' in target:
7371
where = target.index('#')
7472
if where == 0:
75-
# It's a link to a title in the same file, we need to
73+
# It's a link to a title in the same file, we need to
7674
# skip it.
7775
return "ok"
7876
target = target[:where]
@@ -84,7 +82,7 @@ def _get_status(self):
8482
return "doesn't exist"
8583
if target.endswith('/'):
8684
# A directory.
87-
if os.path.isdir(directory):
85+
if os.path.isdir(realpath):
8886
return "ok"
8987
return "not a directory"
9088
else:
@@ -96,7 +94,7 @@ def _get_status(self):
9694
def check(self):
9795
"""Check if the link's target is like it should be.
9896
99-
Return an error message string or "ok". The return value is also
97+
Return an error message string or "ok". The return value is also
10098
assigned to the status attribute.
10199
"""
102100
self.status = self._get_status()

strip.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828

2929
"""Strip trailing whitespace from markdown files."""
3030

31+
import os
32+
3133
import common
3234

3335

34-
def needs_stripping(file):
35-
with open(file, 'r') as f:
36+
def needs_stripping(realfile):
37+
with open(realfile, 'r') as f:
3638
for line in f:
3739
line = line.rstrip('\n')
3840
if line != line.rstrip():
@@ -41,21 +43,22 @@ def needs_stripping(file):
4143
return False
4244

4345

44-
def strip(file):
46+
def strip(realfile):
4547
lines = []
46-
with open(file, 'r') as f:
48+
with open(realfile, 'r') as f:
4749
for line in f:
4850
lines.append(line.rstrip())
49-
with open(file, 'w') as f:
51+
with open(realfile, 'w') as f:
5052
for line in lines:
5153
print(line, file=f)
5254

5355

5456
def main():
5557
print("Stripping trailing whitespace...")
5658
for file in common.get_markdown_files():
57-
if needs_stripping(file):
58-
strip(file)
59+
realfile = file.replace('/', os.sep)
60+
if needs_stripping(realfile):
61+
strip(realfile)
5962
print(" Was stripped:", file)
6063
else:
6164
print(" No trailing whitespace:", file)

update-ends.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@
2828

2929
"""Update ends of markdown files."""
3030

31-
# Markdown and HTML links use / as a path separator so we need posixpath
32-
# for parsing them and we do need to replace / with os.sep when opening
31+
# Markdown and HTML links use / as a path separator so we need posixpath
32+
# for parsing them and we do need to replace / with os.sep when opening
3333
# the files.
34+
import os
3435
import posixpath
3536
import re
3637

@@ -50,7 +51,7 @@
5051
def get_filenames():
5152
"""Get chapter files and other files from README.
5253
53-
Return a two-tuple of chapter file names and other file names as
54+
Return a two-tuple of chapter file names and other file names as
5455
iterables of strings.
5556
"""
5657
chapters = []
@@ -79,12 +80,12 @@ def get_filenames():
7980
def update_end(filename, end):
8081
"""Add *** and end to a file if it doesn't have them already.
8182
82-
filename should be relative to the toplevel using / as a path
83+
filename should be relative to the toplevel using / as a path
8384
separator.
8485
"""
85-
filename = filename
86+
real_filename = filename.replace('/', os.sep)
8687
end = '\n***\n\n' + end
87-
with open(filename, 'r') as f:
88+
with open(real_filename, 'r') as f:
8889
content = f.read()
8990
if content.endswith(end):
9091
# No need to do anything.
@@ -95,11 +96,11 @@ def update_end(filename, end):
9596
# We need to remove the old ending first.
9697
print(" Removing old end:", filename)
9798
where = content.index('\n***\n')
98-
with open(filename, 'w') as f:
99+
with open(real_filename, 'w') as f:
99100
f.write(content[:where])
100101

101102
print(" Adding end:", filename)
102-
with open(filename, 'a') as f:
103+
with open(real_filename, 'a') as f:
103104
f.write(end)
104105

105106

@@ -108,34 +109,15 @@ def main():
108109

109110
# make previous of first file and next of last file to just bring
110111
# back to README
111-
prevs = ['./README.md'] + chapter_files[:-1]
112-
nexts = chapter_files[1:] + ['./README.md']
112+
prevs = ['README.md'] + chapter_files[:-1]
113+
nexts = chapter_files[1:] + ['README.md']
113114

114115
print("Chapter files:")
115116
for prevpath, thispath, nextpath in zip(prevs, chapter_files, nexts):
116-
# the paths are like 'section/file.md'
117-
prevsection, prevfile = posixpath.split(prevpath)
118-
thissection, thisfile = posixpath.split(thispath)
119-
nextsection, nextfile = posixpath.split(nextpath)
120-
121-
# make previous and next relative to this file
122-
if prevsection == thissection:
123-
# they are in the same place
124-
prev = prevfile
125-
elif prevsection == '.':
126-
# something from the top level
127-
prev = posixpath.join('..', prevfile)
128-
else:
129-
# it comes from some other place
130-
prev = posixpath.join('..', prevpath)
131-
132-
if nextsection == thissection:
133-
next_ = nextfile
134-
elif nextsection == '.':
135-
next_ = posixpath.join('..', nextfile)
136-
else:
137-
next_ = posixpath.join('..', nextpath)
138-
117+
# thispath is always like 'section/file.md', never e.g. 'README.md'
118+
thissection, thisfile = thispath.split('/')
119+
prev = posixpath.relpath(prevpath, thissection)
120+
next_ = posixpath.relpath(nextpath, thissection)
139121
extralinks = "[Previous](%s) | [Next](%s) |\n" % (prev, next_)
140122
end = END_TEMPLATE.format(
141123
license='../LICENSE', readme='../README.md',

0 commit comments

Comments
 (0)