|
| 1 | +import sys |
| 2 | +import os |
| 3 | +import glob |
| 4 | + |
| 5 | + |
| 6 | +def extract_python_code_blocks(md_file_path): |
| 7 | + """ |
| 8 | + Extract Python code blocks from a markdown file. |
| 9 | +
|
| 10 | + Args: |
| 11 | + md_file_path (str): Path to the markdown file |
| 12 | +
|
| 13 | + Returns: |
| 14 | + list: List of extracted Python code blocks |
| 15 | + """ |
| 16 | + code_blocks = [] |
| 17 | + in_python_block = False |
| 18 | + current_block = [] |
| 19 | + |
| 20 | + with open(md_file_path, "r", encoding="utf-8") as file: |
| 21 | + for line in file: |
| 22 | + line = line.rstrip("\n") |
| 23 | + |
| 24 | + if line.strip() == "```python": |
| 25 | + in_python_block = True |
| 26 | + current_block = [] |
| 27 | + elif line.strip() == "```" and in_python_block: |
| 28 | + in_python_block = False |
| 29 | + code_blocks.append("\n".join(current_block)) |
| 30 | + elif in_python_block: |
| 31 | + current_block.append(line) |
| 32 | + |
| 33 | + return code_blocks |
| 34 | + |
| 35 | + |
| 36 | +def write_jupyter_notebook_file( |
| 37 | + code_blocks, output_file="notebook_from_md.py" |
| 38 | +): |
| 39 | + """ |
| 40 | + Writes extracted code blocks to a Python file formatted as Jupyter notebook cells. |
| 41 | +
|
| 42 | + Args: |
| 43 | + code_blocks (list): List of code blocks to write |
| 44 | + output_file (str): Path to the output file |
| 45 | + """ |
| 46 | + with open(output_file, "w", encoding="utf-8") as file: |
| 47 | + file.write( |
| 48 | + "# %% [markdown] \n # ## Notebook generated from Markdown file\n\n" |
| 49 | + ) |
| 50 | + |
| 51 | + for i, block in enumerate(code_blocks, 1): |
| 52 | + file.write(f"# %% [markdown]\n# ## Cell {i}\n\n# %%\n{block}\n\n") |
| 53 | + |
| 54 | + print( |
| 55 | + f"Successfully wrote {len(code_blocks)} code cells to" |
| 56 | + f" {output_file}" |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +def process_quiz_files(input_path, output_dir): |
| 61 | + """ |
| 62 | + Process all wrap_up_quiz files in the input path and convert them to notebooks. |
| 63 | +
|
| 64 | + Args: |
| 65 | + input_path (str): Path to look for wrap_up_quiz files in subfolders |
| 66 | + output_dir (str): Directory to write the generated notebooks |
| 67 | + """ |
| 68 | + # Create output directory if it doesn't exist |
| 69 | + if not os.path.exists(output_dir): |
| 70 | + os.makedirs(output_dir) |
| 71 | + print(f"Created output directory: {output_dir}") |
| 72 | + |
| 73 | + # Find all files containing "wrap_up_quiz" in their name in the input path subfolders |
| 74 | + quiz_files = glob.glob( |
| 75 | + f"{input_path}/**/*wrap_up_quiz*.md", recursive=True |
| 76 | + ) |
| 77 | + |
| 78 | + if not quiz_files: |
| 79 | + print(f"No wrap_up_quiz.md files found in {input_path} subfolders.") |
| 80 | + return |
| 81 | + |
| 82 | + print(f"Found {len(quiz_files)} wrap_up_quiz files to process.") |
| 83 | + |
| 84 | + # Process each file |
| 85 | + for md_file_path in quiz_files: |
| 86 | + print(f"\nProcessing: {md_file_path}") |
| 87 | + |
| 88 | + # Extract code blocks |
| 89 | + code_blocks = extract_python_code_blocks(md_file_path) |
| 90 | + |
| 91 | + # Generate output filename |
| 92 | + subfolder = md_file_path.split(os.sep)[3] # Get subfolder name |
| 93 | + output_file = os.path.join(output_dir, f"{subfolder}_wrap_up_quiz.py") |
| 94 | + |
| 95 | + # Display results and write notebook file |
| 96 | + if code_blocks: |
| 97 | + print(f"Found {len(code_blocks)} Python code blocks") |
| 98 | + write_jupyter_notebook_file(code_blocks, output_file=output_file) |
| 99 | + else: |
| 100 | + print(f"No Python code blocks found in {md_file_path}.") |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + input_path = sys.argv[1] |
| 105 | + output_dir = sys.argv[2] |
| 106 | + |
| 107 | + process_quiz_files(input_path, output_dir) |
0 commit comments