Skip to content

Commit e75e323

Browse files
committed
Committing doc genv1
1 parent 5174723 commit e75e323

File tree

4 files changed

+95
-2
lines changed

4 files changed

+95
-2
lines changed

codes/generate_docs

138 KB
Binary file not shown.

codes/generate_docs.mojo

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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()

docs/3sum.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,4 @@ fn main():
9090
```
9191

9292

93-
[Source](https://github.com/ratulb/mojo_programming/blob/main/codes/3sum.mojo)
93+
[Source](https://github.com/ratulb/mojo_programming/blob/main/codes/3sum.mojo)

docs/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,3 @@
3939

4040
🟢 [ Container With Most Water](water_container_max_area.md) ➔ Given an array of line heights, find the two lines that form the container holding the most water.
4141
🟢 [ Count 1s](num_ones.md) ➔ Count the number of set bits (Hamming weight) in the binary representation of a positive integer n.
42-
🟢 [Find all unique triplets [a, b, c] in the array such that a + b + c = 0.](3sum.md) ➔ This solution also returns indices of the triplets along with their values

0 commit comments

Comments
 (0)