|
| 1 | +import os |
| 2 | +import nbformat |
| 3 | +import re |
| 4 | +from nbformat.v4 import new_notebook, new_markdown_cell, new_code_cell |
| 5 | + |
| 6 | +# Paths |
| 7 | +episodes_dir = "episodes" |
| 8 | +notebooks_dir = "notebooks" |
| 9 | + |
| 10 | +# List of Markdown files to ignore (no conversion needed) |
| 11 | +ignore_list = [ |
| 12 | + "01-Introduction.md", |
| 13 | + "02-Data-storage.md", |
| 14 | + "03-Notebooks-as-controllers.md", |
| 15 | + "05-Interacting-with-code-repo.md", |
| 16 | + "09-Resource-management-cleanup.md" |
| 17 | +] |
| 18 | + |
| 19 | +# Ensure notebooks directory exists |
| 20 | +os.makedirs(notebooks_dir, exist_ok=True) |
| 21 | + |
| 22 | +# Regular expression to detect code blocks (matches ```language\n...\n```) |
| 23 | +code_block_pattern = re.compile(r"```(\w+)?\n(.*?)\n```", re.DOTALL) |
| 24 | + |
| 25 | +def split_markdown(md_content): |
| 26 | + """Splits Markdown content into separate Markdown and Code cells.""" |
| 27 | + cells = [] |
| 28 | + position = 0 |
| 29 | + |
| 30 | + for match in code_block_pattern.finditer(md_content): |
| 31 | + # Extract text before the code block as Markdown |
| 32 | + before_code = md_content[position:match.start()].strip() |
| 33 | + if before_code: |
| 34 | + cells.append(new_markdown_cell(before_code)) |
| 35 | + |
| 36 | + # Extract code block content |
| 37 | + code_content = match.group(2).strip() |
| 38 | + if code_content: |
| 39 | + cells.append(new_code_cell(code_content)) |
| 40 | + |
| 41 | + position = match.end() |
| 42 | + |
| 43 | + # Add any remaining Markdown content after the last code block |
| 44 | + remaining_md = md_content[position:].strip() |
| 45 | + if remaining_md: |
| 46 | + cells.append(new_markdown_cell(remaining_md)) |
| 47 | + |
| 48 | + return cells |
| 49 | + |
| 50 | +# Convert each Markdown file in episodes/ |
| 51 | +for filename in os.listdir(episodes_dir): |
| 52 | + if filename.endswith(".md") and filename not in ignore_list: |
| 53 | + md_path = os.path.join(episodes_dir, filename) |
| 54 | + ipynb_path = os.path.join(notebooks_dir, filename.replace(".md", ".ipynb")) |
| 55 | + |
| 56 | + # Read Markdown content |
| 57 | + with open(md_path, "r", encoding="utf-8") as f: |
| 58 | + md_content = f.read() |
| 59 | + |
| 60 | + # Split into Markdown and Code cells |
| 61 | + notebook_cells = split_markdown(md_content) |
| 62 | + |
| 63 | + # Create Jupyter notebook |
| 64 | + nb = new_notebook(cells=notebook_cells) |
| 65 | + |
| 66 | + # Save as .ipynb |
| 67 | + with open(ipynb_path, "w", encoding="utf-8") as f: |
| 68 | + nbformat.write(nb, f) |
| 69 | + |
| 70 | +print("Conversion complete! Excluded:", ", ".join(ignore_list)) |
0 commit comments