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
3435import posixpath
3536import re
3637
5051def 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():
7980def 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