|
| 1 | +import glob |
| 2 | +import shutil |
| 3 | +import os |
| 4 | +import sys |
| 5 | +import webbrowser |
| 6 | + |
| 7 | +try: |
| 8 | + import mistune |
| 9 | +except ImportError: |
| 10 | + print("mistune isn't installed. You can install it like this:\n\n", |
| 11 | + " thispython -m pip install mistune", file=sys.stderr) |
| 12 | + sys.exit(1) |
| 13 | + |
| 14 | + |
| 15 | +def main(): |
| 16 | + if os.path.exists('html'): |
| 17 | + if input("html exists. Do you want to remove it [Y/n] ") == 'N': |
| 18 | + print("Interrupt.") |
| 19 | + return |
| 20 | + if os.path.isdir('html'): |
| 21 | + shutil.rmtree('html') |
| 22 | + else: |
| 23 | + os.remove('html') |
| 24 | + os.mkdir('html') |
| 25 | + print("Generating HTML files...") |
| 26 | + for markdownfile in glob.glob('*.md'): |
| 27 | + htmlfile = os.path.join('html', markdownfile[:-3] + '.html') |
| 28 | + print(' ', markdownfile, '->', htmlfile) |
| 29 | + with open(markdownfile, 'r') as f1: |
| 30 | + with open(htmlfile, 'w') as f2: |
| 31 | + md = f1.read() |
| 32 | + md = md.replace('.md)', '.html)') |
| 33 | + html = mistune.markdown(md) |
| 34 | + print(html, file=f2) |
| 35 | + os.rename(os.path.join('html', 'README.html'), |
| 36 | + os.path.join('html', 'index.html')) |
| 37 | + shutil.copytree('images', os.path.join('html', 'images')) |
| 38 | + print() |
| 39 | + print("*********************") |
| 40 | + print() |
| 41 | + print("Ready! The files are in the html directory,") |
| 42 | + print("double-click html/index.html to read the tutorial.") |
| 43 | + print() |
| 44 | + if input("Do you want to view the tutorial now? [Y/n] ").upper() != 'N': |
| 45 | + print("Opening the tutorial...") |
| 46 | + webbrowser.open(os.path.join('html', 'index.html')) |
| 47 | + |
| 48 | + |
| 49 | +if __name__ == '__main__': |
| 50 | + main() |
0 commit comments