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