Repository navigation
Expand file tree
/
Copy pathupdate-ends.py
More file actions
executable file
·110 lines (82 loc) · 2.98 KB
/
Copy pathupdate-ends.py
File metadata and controls
executable file
·110 lines (82 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
"""Update ends of markdown files."""
import collections
import re
BASIC_END = """
***
You may use this tutorial freely at your own risk. See
[LICENSE](LICENSE).
[Back to the list of contents](README.md#list-of-contents)
"""
CHAPTER_END = """
***
You may use this tutorial freely at your own risk. See
[LICENSE](LICENSE).
[Previous]({prev}) | [Next]({next}) |
[Back to the list of contents](README.md#list-of-contents)
"""
MARKDOWN_LINK_REGEX = r'\[.*\]\((.*\.md)\)'
CHAPTER_LINK_REGEX = '^\d+\. ' + MARKDOWN_LINK_REGEX + '$'
def get_filenames():
"""Get chapter files and other files from README.
Return a two-tuple of chapter files and other files.
"""
chapters = []
with open('README.md', 'r') as f:
# move to where the content list starts
while f.readline().strip() != "## List of contents":
pass
# now let's read the content list
for line in f:
line = line.strip()
if line:
# not empty line
match = re.search(CHAPTER_LINK_REGEX, line)
if match is None:
# end of content list
break
chapters.append(match.group(1))
# now let's find other links to markdown files
with open('README.md', 'r') as f:
all_files = re.findall(MARKDOWN_LINK_REGEX, f.read())
others = set(all_files) - set(chapters)
return chapters, others
def has_end(filename, template):
linecount = template.count('\n')
with open(filename, 'r') as f:
# get the last linecount lines
filelines = collections.deque(f, maxlen=linecount)
templatelines = template.rstrip('\n').split('\n')
for templateline, fileline in zip(templatelines, filelines):
if '{' not in templateline:
# It doesn't contain formatting, we can do something with it.
if templateline.strip() != fileline.strip():
# It's different than what using the template would result in.
return False
# All lines matched if we get here.
return True
def main():
chapter_files, other_files = get_filenames()
# make previous of first file and next of last file to just bring
# back to README
prevs = ['README.md'] + chapter_files[:-1]
nexts = chapter_files[1:] + ['README.md']
print("Chapter files")
for filename, prev, next in zip(chapter_files, prevs, nexts):
if has_end(filename, CHAPTER_END):
print(" Has end:", filename)
else:
print(" Adding end:", filename)
with open(filename, 'a') as f:
f.write(CHAPTER_END.format(prev=prev, next=next))
print()
print("Other files")
for filename in other_files:
if has_end(filename, BASIC_END):
print(" Has end:", filename)
else:
print(" Adding end:", filename)
with open(filename, 'a') as f:
f.write(BASIC_END)
if __name__ == '__main__':
main()