|
| 1 | +### Read the Mojo file |
| 2 | +import sys |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +alias GITHUB_REPO_URL = "https://github.com/ratulb/mojo_programming/blob/main/codes/" |
| 6 | +alias CODES_DIR = Path("codes") |
| 7 | +alias DOCS_DIR = Path("../docs") |
| 8 | +alias INDEX_MD = DOCS_DIR / "index.md" |
| 9 | + |
| 10 | + |
| 11 | +def main(): |
| 12 | + if len(sys.argv()) != 2: |
| 13 | + print("Usage: mojo generate_docs.mojo <path_to_mojo_file>") |
| 14 | + sys.exit(-1) |
| 15 | + mojo_file = Path(sys.argv()[1]) |
| 16 | + if not mojo_file.exists() and not mojo_file.is_file(): |
| 17 | + err = mojo_file.__str__() + " does not exist." |
| 18 | + print(err) |
| 19 | + sys.exit(-1) |
| 20 | + |
| 21 | + ## Read the Mojo file |
| 22 | + lines = mojo_file.read_text().splitlines() |
| 23 | + comments_found = lines[0].startswith("###") and lines[1].startswith("###") |
| 24 | + if not comments_found: |
| 25 | + print( |
| 26 | + "Mojo file:", |
| 27 | + '"' + mojo_file.__str__() + '"', |
| 28 | + "missing expected comments.", |
| 29 | + ) |
| 30 | + sys.exit(-1) |
| 31 | + ## Extract title and description |
| 32 | + title = lines[0].replace("###", "").strip() |
| 33 | + description = lines[1].replace("###", "").strip() |
| 34 | + |
| 35 | + ## Prepare .md file content |
| 36 | + stem = mojo_file.__str__().split("/")[-1].split(".")[0] |
| 37 | + md_filename = stem + ".md" |
| 38 | + mojo_filename = stem + ".mojo" |
| 39 | + md_path = DOCS_DIR / md_filename |
| 40 | + code_content = StringSlice("\n").join( |
| 41 | + lines[2:] |
| 42 | + ) # skip first two comment lines |
| 43 | + md_text = ( |
| 44 | + StringSlice( |
| 45 | + "### " + title + "\n### " + description + "\n\n" + "```python\n" |
| 46 | + ) |
| 47 | + + code_content |
| 48 | + + "\n\n```\n\n\n[Source](" |
| 49 | + + GITHUB_REPO_URL |
| 50 | + + mojo_filename |
| 51 | + + ")" |
| 52 | + ) |
| 53 | + print(md_text) |
| 54 | + |
| 55 | + # Write .md file |
| 56 | + md_path.write_text(md_text) |
| 57 | + print("Generated: ", md_path.__str__()) |
| 58 | + |
| 59 | + ## Update index.md |
| 60 | + update_index(title, description, md_filename) |
| 61 | + |
| 62 | + |
| 63 | +def update_index( |
| 64 | + title: StringSlice, description: StringSlice, md_filename: String |
| 65 | +): |
| 66 | + index_lines = INDEX_MD.read_text().splitlines() |
| 67 | + if not index_lines: |
| 68 | + print("Error: index.md is empty!") |
| 69 | + sys.exit(-1) |
| 70 | + |
| 71 | + last_line = index_lines[-1] |
| 72 | + if "🟢" not in last_line: |
| 73 | + print("Warning: last line of index.md does not match expected format.") |
| 74 | + |
| 75 | + # Create new entry by copying symbols from last line |
| 76 | + prefix = last_line.split("[")[0] |
| 77 | + new_entry = ( |
| 78 | + StringSlice(prefix) |
| 79 | + + "[" |
| 80 | + + title |
| 81 | + + "](" |
| 82 | + + md_filename |
| 83 | + + ") ➔ " |
| 84 | + + description |
| 85 | + ) |
| 86 | + print(new_entry) |
| 87 | + |
| 88 | + # Append new entry |
| 89 | + index_lines.append(new_entry) |
| 90 | + INDEX_MD.write_text(StringSlice("\n").join(index_lines) + "\n") |
| 91 | + print("Updated: ", INDEX_MD) |
| 92 | + print() |
| 93 | + print(INDEX_MD.read_text().splitlines()[-1]) |
| 94 | + print() |
0 commit comments