Convert Python demos in Jupytext 'light' into MyST markdown. Uses Jupytext. These files can then be included in Sphinx documentation.
()
| 12 | |
| 13 | |
| 14 | def process(): |
| 15 | """Convert Python demos in Jupytext 'light' into MyST markdown. |
| 16 | |
| 17 | Uses Jupytext. These files can then be included in Sphinx |
| 18 | documentation. |
| 19 | """ |
| 20 | # Make demo doc directory |
| 21 | demo_doc_dir = pathlib.Path("./demos") |
| 22 | demo_doc_dir.mkdir(parents=True, exist_ok=True) |
| 23 | |
| 24 | # Directories to scan demo code files |
| 25 | demo_dirs = pathlib.Path("../../demo") |
| 26 | |
| 27 | # Iterate over subdirectories containing demos |
| 28 | for demo_subdir in demo_dirs.iterdir(): |
| 29 | if demo_subdir.is_dir(): |
| 30 | fname = pathlib.Path("/demo_" + demo_subdir.name) |
| 31 | demo_doc_subdir = demo_doc_dir / fname.name |
| 32 | demo_doc_subdir.mkdir(parents=True, exist_ok=True) |
| 33 | # Process each demo using jupytext/myst |
| 34 | for demo_file in demo_subdir.glob("main.cpp"): |
| 35 | # Copy demo files into documentation demo directory |
| 36 | shutil.copy(demo_file, demo_doc_subdir) |
| 37 | cpp_demo = jupytext.read(demo_file) |
| 38 | cpp_myst_text = jupytext.writes(cpp_demo, fmt="myst") |
| 39 | |
| 40 | # myst-parser does not process blocks with {code-cell} |
| 41 | cpp_myst_text = cpp_myst_text.replace("{code-cell}", "cpp") |
| 42 | |
| 43 | # Similarly for python file, dump python myst text in cpp myst |
| 44 | for pydemo_file in demo_subdir.glob("*.py"): |
| 45 | shutil.copy(pydemo_file, demo_doc_subdir) |
| 46 | python_demo = jupytext.read(pydemo_file) |
| 47 | python_myst_text = jupytext.writes(python_demo, fmt="myst") |
| 48 | python_myst_text = python_myst_text.replace("{code-cell}", "python") |
| 49 | cpp_myst_text = cpp_myst_text.replace("![ufl-code]", python_myst_text) |
| 50 | |
| 51 | for cmake_file in demo_subdir.glob("CMakeLists.txt"): |
| 52 | shutil.copy(cmake_file, demo_doc_subdir) |
| 53 | |
| 54 | myst_file = (demo_doc_dir / fname.name).with_suffix(".md") |
| 55 | with open(myst_file, "w") as fw: |
| 56 | fw.write(cpp_myst_text) |
| 57 | |
| 58 | # There is a possibility to use jupyter-notebooks with C++/C kernels |
| 59 | # ipynb_file = (demo_doc_dir / fname.name).with_suffix(".ipynb") |
| 60 | # jupytext.write(cpp_demo, ipynb_file, fmt="ipynb") |
| 61 | |
| 62 | |
| 63 | if __name__ == "__main__": |
no test coverage detected