5
5
import re
6
6
7
7
8
- BASIC_END = """
9
- ***
10
-
8
+ BASIC_END = """\
11
9
You may use this tutorial freely at your own risk. See
12
10
[LICENSE](LICENSE).
13
11
14
12
[Back to the list of contents](README.md#list-of-contents)
15
13
"""
16
14
17
- CHAPTER_END = """
18
- ***
19
-
15
+ CHAPTER_END = """\
20
16
You may use this tutorial freely at your own risk. See
21
17
[LICENSE](LICENSE).
22
18
25
21
"""
26
22
27
23
28
- MARKDOWN_LINK_REGEX = r'\[.*\]\((.*\.md)\)'
29
- CHAPTER_LINK_REGEX = '^\d+\. ' + MARKDOWN_LINK_REGEX + '$'
24
+ LINK_REGEX = r'\[.*\]\((.*\.md)\)'
25
+ CHAPTER_LINK_REGEX = r '^\d+\. ' + LINK_REGEX + r '$'
30
26
31
27
32
28
def get_filenames ():
@@ -53,28 +49,32 @@ def get_filenames():
53
49
54
50
# now let's find other links to markdown files
55
51
with open ('README.md' , 'r' ) as f :
56
- all_files = re .findall (MARKDOWN_LINK_REGEX , f .read ())
52
+ all_files = re .findall (LINK_REGEX , f .read ())
57
53
others = set (all_files ) - set (chapters )
58
54
59
55
return chapters , others
60
56
61
57
62
- def has_end (filename , template ):
63
- linecount = template .count ('\n ' )
58
+ def update_end (filename , end ):
59
+ """Add *** and end to a file if it doesn't contain them already."""
60
+ end = '\n ***\n \n ' + end
64
61
with open (filename , 'r' ) as f :
65
- # get the last linecount lines
66
- filelines = collections .deque (f , maxlen = linecount )
67
- templatelines = template .rstrip ('\n ' ).split ('\n ' )
62
+ content = f .read ()
63
+ if content .endswith (end ):
64
+ # No need to do anything.
65
+ print (" Has end:" , filename )
66
+ return
68
67
69
- for templateline , fileline in zip ( templatelines , filelines ) :
70
- if '{' not in templateline :
71
- # It doesn't contain formatting, we can do something with it.
72
- if templateline . strip () != fileline . strip ():
73
- # It's different than what using the template would result in.
74
- return False
68
+ if ' \n *** \n ' in content :
69
+ # We need to remove the old ending first.
70
+ print ( " Removing old end:" , filename )
71
+ where = content . index ( ' \n *** \n ' )
72
+ with open ( filename , 'w' ) as f :
73
+ f . write ( content [: where ])
75
74
76
- # All lines matched if we get here.
77
- return True
75
+ print (" Adding end:" , filename )
76
+ with open (filename , 'a' ) as f :
77
+ f .write (end )
78
78
79
79
80
80
def main ():
@@ -85,25 +85,16 @@ def main():
85
85
prevs = ['README.md' ] + chapter_files [:- 1 ]
86
86
nexts = chapter_files [1 :] + ['README.md' ]
87
87
88
- print ("Chapter files" )
88
+ print ("Chapter files: " )
89
89
for filename , prev , next in zip (chapter_files , prevs , nexts ):
90
- if has_end (filename , CHAPTER_END ):
91
- print (" Has end:" , filename )
92
- else :
93
- print (" Adding end:" , filename )
94
- with open (filename , 'a' ) as f :
95
- f .write (CHAPTER_END .format (prev = prev , next = next ))
90
+ end = CHAPTER_END .format (prev = prev , next = next )
91
+ update_end (filename , end )
96
92
97
93
print ()
98
94
99
- print ("Other files" )
95
+ print ("Other files: " )
100
96
for filename in other_files :
101
- if has_end (filename , BASIC_END ):
102
- print (" Has end:" , filename )
103
- else :
104
- print (" Adding end:" , filename )
105
- with open (filename , 'a' ) as f :
106
- f .write (BASIC_END )
97
+ update_end (filename , BASIC_END )
107
98
108
99
109
100
if __name__ == '__main__' :
0 commit comments