55import re
66
77
8- BASIC_END = """
9- ***
10-
8+ BASIC_END = """\
119 You may use this tutorial freely at your own risk. See
1210[LICENSE](LICENSE).
1311
1412[Back to the list of contents](README.md#list-of-contents)
1513"""
1614
17- CHAPTER_END = """
18- ***
19-
15+ CHAPTER_END = """\
2016 You may use this tutorial freely at your own risk. See
2117[LICENSE](LICENSE).
2218
2521"""
2622
2723
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 '$'
3026
3127
3228def get_filenames ():
@@ -53,28 +49,32 @@ def get_filenames():
5349
5450 # now let's find other links to markdown files
5551 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 ())
5753 others = set (all_files ) - set (chapters )
5854
5955 return chapters , others
6056
6157
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
6461 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
6867
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 ])
7574
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 )
7878
7979
8080def main ():
@@ -85,25 +85,16 @@ def main():
8585 prevs = ['README.md' ] + chapter_files [:- 1 ]
8686 nexts = chapter_files [1 :] + ['README.md' ]
8787
88- print ("Chapter files" )
88+ print ("Chapter files: " )
8989 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 )
9692
9793 print ()
9894
99- print ("Other files" )
95+ print ("Other files: " )
10096 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 )
10798
10899
109100if __name__ == '__main__' :
0 commit comments