|
| 1 | +# IPython Notebook to Markdown conversion script |
| 2 | +# |
| 3 | +# Sebastian Raschka 2014-2021 |
| 4 | +# |
| 5 | +# Author: Sebastian Raschka <sebastianraschka.com> |
| 6 | +# |
| 7 | +# License: BSD 3 clause |
| 8 | + |
| 9 | +import subprocess |
| 10 | +import glob |
| 11 | +import shutil |
| 12 | +import os |
| 13 | +import markdown |
| 14 | +from markdown.treeprocessors import Treeprocessor |
| 15 | +from markdown.extensions import Extension |
| 16 | +from nbconvert.exporters import MarkdownExporter |
| 17 | + |
| 18 | + |
| 19 | +class ImgExtractor(Treeprocessor): |
| 20 | + def run(self, doc): |
| 21 | + self.markdown.images = [] |
| 22 | + for image in doc.findall('.//img'): |
| 23 | + self.markdown.images.append(image.get('src')) |
| 24 | + |
| 25 | + |
| 26 | +class ImgExtExtension(Extension): |
| 27 | + def extendMarkdown(self, md, md_globals): |
| 28 | + img_ext = ImgExtractor(md) |
| 29 | + md.treeprocessors.add('imgext', img_ext, '>inline') |
| 30 | + |
| 31 | + |
| 32 | +def ipynb_to_md(ipynb_path): |
| 33 | + orig_path = os.getcwd() |
| 34 | + os.chdir(os.path.dirname(ipynb_path)) |
| 35 | + file_name = os.path.basename(ipynb_path) |
| 36 | + subprocess.call(['python', '-m', 'nbconvert', |
| 37 | + '--to', 'markdown', file_name]) |
| 38 | + |
| 39 | + new_s = [] |
| 40 | + md_name = file_name.replace('.ipynb', '.md') |
| 41 | + with open(md_name, 'r') as f: |
| 42 | + for line in f: |
| 43 | + if line.startswith('#'): |
| 44 | + new_s.append(line) |
| 45 | + break |
| 46 | + for line in f: |
| 47 | + if line.startswith(('## API', '# API')): |
| 48 | + new_s.append(line) |
| 49 | + new_s.append('\n') |
| 50 | + break |
| 51 | + new_s.append(line) |
| 52 | + for line in f: |
| 53 | + if line.lstrip().startswith('#'): |
| 54 | + break |
| 55 | + for line in f: |
| 56 | + new_s.append(line[4:]) |
| 57 | + |
| 58 | + with open(md_name, 'w') as f: |
| 59 | + f.write(''.join(new_s)) |
| 60 | + os.chdir(orig_path) |
| 61 | + |
| 62 | + |
| 63 | +# md = markdown.Markdown(extensions=[ImgExtExtension()]) |
| 64 | +# html = md.convert(data) |
| 65 | +# print(md.images) |
| 66 | + |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + |
| 70 | + import argparse |
| 71 | + parser = argparse.ArgumentParser( |
| 72 | + description='Convert docstring into a markdown API documentation.', |
| 73 | + formatter_class=argparse.RawTextHelpFormatter) |
| 74 | + |
| 75 | + parser.add_argument('-i', '--ipynb', |
| 76 | + help='Path to the IPython file') |
| 77 | + |
| 78 | + parser.add_argument('-a', '--all', |
| 79 | + help='Path to parse all ipynb recursively') |
| 80 | + |
| 81 | + parser.add_argument('-v', '--version', |
| 82 | + action='version', |
| 83 | + version='v. 0.1') |
| 84 | + |
| 85 | + args = parser.parse_args() |
| 86 | + |
| 87 | + if args.all and args.ipynb: |
| 88 | + raise AttributeError('Conflicting flags --ipynb and --all; choose one') |
| 89 | + |
| 90 | + if args.ipynb: |
| 91 | + ipynb_to_md(ipynb_path=args.ipynb) |
| 92 | + else: |
| 93 | + tree = os.walk(args.all) |
| 94 | + for d in tree: |
| 95 | + filenames = glob.glob(os.path.join(d[0], '*')) |
| 96 | + for f in filenames: |
| 97 | + if f.endswith('.ipynb'): |
| 98 | + print(f) |
| 99 | + |
| 100 | + ipynb_to_md(ipynb_path=f) |
0 commit comments