1
+ import os
2
+ import sys
3
+
1
4
BUILD_DIR = "_build/markdown"
2
5
3
- filenames = ["index.md" , "tutorial/tour.md" , "language/index.md" , "language/introduction.md" ,
4
- "language/fundamentals.md" , "language/methods.md" , "language/error-handling.md" ,
5
- "language/packages.md" , "language/tests.md" , "language/docs.md" , "language/ffi-and-wasm-host.md" ,
6
- "language/derive.md" , "language/async-experimental.md" ]
6
+ def collect (directory , header_level , output_file ):
7
+ def adjust_header (line , level ):
8
+ if line .startswith ('#' ):
9
+ return '#' * level + line
10
+ return line
11
+
12
+ def process_file (filepath , level , output ):
13
+ output .write (f"\n <!-- path: { filepath } -->\n " )
14
+ with open (os .path .join (BUILD_DIR , filepath ), "r" ) as file :
15
+ for line in file :
16
+ output .write (adjust_header (line , level ))
17
+
18
+ index_path = os .path .join (directory , "index.md" )
19
+ with open (index_path , "r" ) as index_file :
20
+ toctree_paths = []
21
+ collect_paths = False
22
+ for line in index_file :
23
+ line = line .strip ()
24
+ if "toctree" in line :
25
+ collect_paths = True
26
+ continue
27
+ if collect_paths :
28
+ if line .startswith (":" ):
29
+ continue
30
+ if line == "```" :
31
+ break
32
+ toctree_paths .append (os .path .join (directory , f"{ line } .md" ))
7
33
8
- with open (f"{ BUILD_DIR } /llm.md" , "w" ) as f :
9
- print ("# MoonBit Documentation" , file = f )
10
- for fname in filenames :
11
- with open (f"{ BUILD_DIR } /{ fname } " , "r" ) as g :
12
- print (f"<!-- path: { fname } -->" , file = f )
34
+ with open (output_file , "a" ) as output :
35
+ process_file (index_path , header_level , output )
36
+ for path in toctree_paths :
37
+ process_file (path , header_level + 1 , output )
38
+
39
+ def llms_txt ():
40
+ with open (f"{ BUILD_DIR } /llm.md" , "w" ) as f :
41
+ with open (f"{ BUILD_DIR } /index.md" , "r" ) as g :
42
+ print (f"<!-- path: index.md -->" , file = f )
13
43
for line in g :
14
- if line .startswith ('#' ):
15
- f .write (f"#{ line } " )
16
- else :
17
- f .write (line )
44
+ f .write (line )
45
+
46
+ collect ("tutorial" , 1 , f"{ BUILD_DIR } /llm.md" )
47
+ collect ("language" , 1 , f"{ BUILD_DIR } /llm.md" )
48
+
49
+ def main ():
50
+ os .system ("make markdown" )
51
+ llms_txt ()
52
+ for directory in ["tutorial" , "language" , "toolchain" , "example" ]:
53
+ output_file = f"download/{ directory } /summary.md"
54
+ os .makedirs (os .path .dirname (output_file ), exist_ok = True )
55
+ if os .path .exists (output_file ):
56
+ os .remove (output_file )
57
+ collect (directory , 0 , output_file )
58
+
59
+ if __name__ == "__main__" :
60
+ main ()
0 commit comments