1414# file, You can obtain one at http://mozilla.org/MPL/2.0/.
1515import io
1616import os
17- import mkdocs_gen_files
1817from pathlib import Path
19- from typing import Any
2018
2119from docs .python .generators import regexes , common
2220
2321
24- def collect_srcs (top : str ):
25- for root , _ , files in os .walk (top ):
26- for f in files :
27- if f .endswith (('.h' , 'cpp' , 'CMakeLists.txt' )):
28- yield Path (root ) / f
29-
30- def get_id (title : str ) -> str :
31- assert title .startswith ("# " )
32-
33- title = title .lstrip ("# " ).rstrip ("\n " )
34-
35- if "{" in title :
36- id = title [title .find ("{#" )+ 2 :title .find ("}" )]
37- title = title [:title .find ("{#" )]
38-
39- else :
40- id = title .lower ()
41- for i in " _" :
42- id = id .replace (i , "-" )
43-
44- id = "" .join (ch for ch in id if ch .isalnum () or ch == '-' )
45- id = id .strip ('-' )
46-
47- return id
48-
49- def descriptions (readlines : list [str ]):
50- description = ""
51- for line in readlines :
52- if m := regexes .AUI_EXAMPLE .match (line ):
53- category = m .group (1 )
54- for description_line in readlines :
55- description_line = description_line .strip ("\n " )
56- description += " " + description_line
57- description = description .strip ()
58-
59- return category , description
60-
6122def examine ():
6223 EXAMPLES_DIR = Path .cwd () / "examples"
6324 EXAMPLES_DIR = EXAMPLES_DIR .as_posix ()
6425
6526 examples_lists = {}
6627
67- for root , _ , files in os .walk (EXAMPLES_DIR ):
28+ for root , dirs , files in os .walk (EXAMPLES_DIR ):
6829 for file in files :
6930 if file != "README.md" :
7031 continue
7132
7233 example_path = str (Path (root ).relative_to (EXAMPLES_DIR ))
73- if os . sep not in example_path :
34+ if "/" not in example_path and " \\ " not in example_path :
7435 continue
7536
7637 example_path = example_path .replace (os .sep , "_" )
7738
39+ def collect_srcs (top ):
40+ for root , dirs , files in os .walk (top ):
41+ for f in files :
42+ if any (f .endswith (ext ) for ext in ['.h' , 'cpp' , 'CMakeLists.txt' ]):
43+ yield Path (root ) / f
44+
7845 srcs = list (collect_srcs (root ))
7946
8047 input_file = Path (root ) / file
8148 with open (input_file , 'r' , encoding = 'utf-8' ) as fis :
8249 title = fis .readline ()
83- id = get_id (title )
84- category , description = descriptions (fis .readlines ())
85-
50+ assert title .startswith ("# " )
51+ title = title .lstrip ("# " ).rstrip ("\n " )
52+ if "{" in title :
53+ id = title [title .find ("{#" )+ 2 :title .find ("}" )]
54+ title = title [:title .find ("{#" )]
55+ else :
56+ id = title .lower ()
57+ for i in [" " , "_" ]:
58+ id = id .replace (i , "-" )
59+ id = "" .join (ch for ch in id if ch .isalnum () or ch == '-' )
60+ id = id .strip ('-' )
61+
62+ category = None
63+ description = ""
64+ it = iter (fis .readlines ())
65+ for line in it :
66+ if m := regexes .AUI_EXAMPLE .match (line ):
67+ category = m .group (1 )
68+ for description_line in it :
69+ description_line = description_line .strip ("\n " )
70+ if not description_line :
71+ break
72+ description += " " + description_line
73+
74+ description = description .strip ()
8675 if not id :
8776 raise RuntimeError (f"no id provided in { input_file } " )
8877 if not category :
@@ -98,15 +87,12 @@ def examine():
9887 'srcs' : srcs ,
9988 })
10089
101- if not examples_lists :
102- raise RuntimeError ("no examples provided" )
103-
10490 return examples_lists
10591
10692
107-
10893examples_lists = examine ()
109-
94+ if not examples_lists :
95+ raise RuntimeError ("no examples provided" )
11096
11197def define_env (env ):
11298 @env .macro
@@ -132,27 +118,23 @@ def example(category: str):
132118"""
133119
134120def gen_pages ():
121+ import mkdocs_gen_files
135122 for category_name , category in examples_lists .items ():
136123 for example in category :
137124 id = example ['id' ]
138125 page_path = example ['page_path' ]
139-
140126 mkdocs_gen_files .set_edit_path (f"{ id } .md" , '..' / page_path .relative_to (Path .cwd ()))
141-
142127 with mkdocs_gen_files .open (f"{ id } .md" , "w" ) as fos :
143128 with io .open (page_path , 'r' , encoding = 'utf-8' ) as fis :
144129 contents = fis .read ()
145-
146130 print (contents , file = fos , end = '' )
147131
148132 if not example ['srcs' ]:
149133 if "## Source Code" not in contents :
150134 raise RuntimeError (f'{ page_path } contains neither "## Source Code" section nor source files.' )
151135 continue
152-
153136 print ('\n ## Source Code\n \n ' , file = fos )
154137 print (f'[ <!-- aui:icon octicons-link-external-16 --> Repository ](https://github.com/aui-framework/aui/tree/master/{ page_path .relative_to (Path .cwd ())} )\n ' , file = fos )
155-
156138 for f in example ['srcs' ]:
157139 filename = f .relative_to (page_path .parent )
158140 print (f'\n ### { filename } \n ' , file = fos )
@@ -170,5 +152,5 @@ def skip_license(iterator):
170152
171153 for line in skip_license (iter (f .read_text ().splitlines ())):
172154 print (f'{ line } ' , file = fos )
173- print ('```' , file = fos )
155+ print (f '```' , file = fos )
174156
0 commit comments