@@ -28,11 +28,20 @@ class ExtraDocsIndexError(Exception):
28
28
pass
29
29
30
30
31
+ class TocTreeEntry :
32
+ ref : str
33
+ title : t .Optional [str ]
34
+
35
+ def __init__ (self , ref : str , title : t .Optional [str ] = None ):
36
+ self .ref = ref
37
+ self .title = title
38
+
39
+
31
40
class Section :
32
41
title : str
33
- toctree : t .List [str ]
42
+ toctree : t .List [TocTreeEntry ]
34
43
35
- def __init__ (self , title : str , toctree : t .List [str ]):
44
+ def __init__ (self , title : str , toctree : t .List [TocTreeEntry ]):
36
45
self .title = title
37
46
self .toctree = toctree
38
47
@@ -97,23 +106,52 @@ def lint_required_conditions(content: str, collection_name: str
97
106
return sorted (labels ), errors
98
107
99
108
109
+ def _parse_toctree_entry (entry : t .Dict [t .Any , t .Any ],
110
+ toctree_index : int ,
111
+ section_index : int
112
+ ) -> t .Tuple [t .Optional [TocTreeEntry ], t .List [str ]]:
113
+ errors : t .List [str ] = []
114
+ toctree_entry : t .Optional [TocTreeEntry ] = None
115
+ for key in ('ref' , ):
116
+ if key not in entry :
117
+ errors .append (
118
+ f'Toctree entry #{ toctree_index } in section #{ section_index } '
119
+ f' does not have a "{ key } " entry' )
120
+ for key , value in entry .items ():
121
+ if not isinstance (key , str ) or not isinstance (value , str ):
122
+ errors .append (
123
+ f'Toctree entry #{ toctree_index } in section #{ section_index } '
124
+ f' must have strings for keys and values for all entries' )
125
+ if not errors :
126
+ toctree_entry = TocTreeEntry (entry ['ref' ], title = entry .get ('title' ))
127
+ return toctree_entry , errors
128
+
129
+
100
130
def load_toctree (yaml_section : t .Dict [str , t .Any ], section_index : int = 0
101
- ) -> t .Tuple [t .List [str ], t .List [str ]]:
131
+ ) -> t .Tuple [t .List [TocTreeEntry ], t .List [str ]]:
102
132
errors : t .List [str ] = []
103
- toctree : t .List [str ] = []
133
+ toctree : t .List [TocTreeEntry ] = []
104
134
if 'toctree' in yaml_section :
105
135
if not isinstance (yaml_section ['toctree' ], list ):
106
136
errors .append (
107
137
f'Toctree entry in section #{ section_index } is not a list' )
108
138
return toctree , errors
109
139
110
- for toctree_index , toctree_name in enumerate (yaml_section ['toctree' ]):
111
- if not isinstance (toctree_name , str ):
112
- errors .append (
113
- f'Toctree entry #{ toctree_index } in section #{ section_index } '
114
- f' is not a string' )
140
+ for toctree_index , toctree_entry in enumerate (yaml_section ['toctree' ]):
141
+ if isinstance (toctree_entry , str ):
142
+ toctree .append (TocTreeEntry (toctree_entry ))
115
143
continue
116
- toctree .append (toctree_name )
144
+ if isinstance (toctree_entry , dict ):
145
+ toctree_entry_obj , toctree_entry_errors = _parse_toctree_entry (
146
+ toctree_entry , toctree_index , section_index )
147
+ errors .extend (toctree_entry_errors )
148
+ if toctree_entry_obj :
149
+ toctree .append (toctree_entry_obj )
150
+ continue
151
+ errors .append (
152
+ f'Toctree entry #{ toctree_index } in section #{ section_index } '
153
+ f' is neither a string nor a dictionary' )
154
+ continue
117
155
return toctree , errors
118
156
119
157
@@ -189,8 +227,8 @@ async def load_collection_extra_docs(collection_name: str,
189
227
sections = []
190
228
191
229
for section in sections :
192
- for i , toctree in enumerate ( section .toctree ) :
193
- section . toctree [ i ] = f"{ path_prefix } /{ toctree } "
230
+ for toctree in section .toctree :
231
+ toctree . ref = f"{ path_prefix } /{ toctree . ref } "
194
232
documents = []
195
233
for abs_path , rel_path in find_extra_docs (collection_path ):
196
234
try :
0 commit comments