|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# This is free and unencumbered software released into the public |
| 4 | +# domain. |
| 5 | + |
| 6 | +# Anyone is free to copy, modify, publish, use, compile, sell, or |
| 7 | +# distribute this software, either in source code form or as a |
| 8 | +# compiled binary, for any purpose, commercial or non-commercial, and |
| 9 | +# by any means. |
| 10 | + |
| 11 | +# In jurisdictions that recognize copyright laws, the author or |
| 12 | +# authors of this software dedicate any and all copyright interest in |
| 13 | +# the software to the public domain. We make this dedication for the |
| 14 | +# benefit of the public at large and to the detriment of our heirs |
| 15 | +# and successors. We intend this dedication to be an overt act of |
| 16 | +# relinquishment in perpetuity of all present and future rights to |
| 17 | +# this software under copyright law. |
| 18 | + |
| 19 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
| 23 | +# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 24 | +# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +# For more information, please refer to <http://unlicense.org> |
| 28 | + |
| 29 | +"""Things that other scripts import and use.""" |
| 30 | + |
| 31 | +import itertools |
| 32 | +import re |
| 33 | +import sys |
| 34 | + |
| 35 | + |
| 36 | +_LINK_REGEX = r'\[(.*?)\]\((.*?)\)' |
| 37 | + |
| 38 | + |
| 39 | +def find_links(file): |
| 40 | + """Find all markdown links in a file object. |
| 41 | +
|
| 42 | + Yield (lineno, regexmatch) tuples. |
| 43 | + """ |
| 44 | + # don't yield same link twice |
| 45 | + seen = set() |
| 46 | + |
| 47 | + # we need to loop over the file two lines at a time to support |
| 48 | + # multi-line (actually two-line) links, so this is kind of a mess |
| 49 | + firsts, seconds = itertools.tee(file) |
| 50 | + next(seconds) # first line is never second line |
| 51 | + |
| 52 | + # we want 1-based indexing instead of 0-based and one-line links get |
| 53 | + # caught from linepair[1], so we need to start at two |
| 54 | + for lineno, linepair in enumerate(zip(firsts, seconds), start=2): |
| 55 | + lines = linepair[0] + linepair[1] |
| 56 | + for match in re.finditer(_LINK_REGEX, lines, flags=re.DOTALL): |
| 57 | + if match.group(0) not in seen: |
| 58 | + seen.add(match.group(0)) |
| 59 | + yield match, lineno |
| 60 | + |
| 61 | + |
| 62 | +def get_markdown_files(): |
| 63 | + """Yield the names of all markdown files in this tutorial. |
| 64 | +
|
| 65 | + This assumes that the README contains links to everything. |
| 66 | + """ |
| 67 | + yield 'README.md' |
| 68 | + with open('README.md', 'r') as f: |
| 69 | + for match, lineno in find_links(f): |
| 70 | + target = match.group(2) |
| 71 | + # Currently the README doesn't link to itself, but I don't |
| 72 | + # want to break things if it will in the future. |
| 73 | + if target.endswith('.md') and target != 'README.md': |
| 74 | + yield target |
| 75 | + |
| 76 | + |
| 77 | +def askyesno(question, default=True): |
| 78 | + """Ask a yes/no question and return True or False. |
| 79 | +
|
| 80 | + The default answer is yes if default is True and no if default is |
| 81 | + False. |
| 82 | + """ |
| 83 | + if default: |
| 84 | + # yes by default |
| 85 | + question += ' [Y/n] ' |
| 86 | + else: |
| 87 | + # no by default |
| 88 | + question += ' [y/N] ' |
| 89 | + while True: |
| 90 | + result = input(question).upper().strip() |
| 91 | + if result == 'Y': |
| 92 | + return True |
| 93 | + if result == 'N': |
| 94 | + return False |
| 95 | + if not result: |
| 96 | + return default |
0 commit comments