1111
1212from .states import DummyStateMachine
1313
14-
1514class AutoStructify (transforms .Transform ):
1615 """Automatically try to transform blocks to sphinx directives.
1716
@@ -39,6 +38,8 @@ def __init__(self, *args, **kwargs):
3938 suffix_set = set (['md' , 'rst' ])
4039
4140 default_config = {
41+ 'auto_toc_tree_maxdepth' : 1 ,
42+ 'auto_toc_tree_numbered' : None ,
4243 'auto_toc_tree_section' : None ,
4344 'commonmark_suffixes' : ['.md' ],
4445 'enable_auto_doc_ref' : False ,
@@ -125,6 +126,8 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
125126 """
126127 if not self .config ['enable_auto_toc_tree' ]:
127128 return None
129+ maxdepth = self .config .get ('auto_toc_tree_maxdepth' , 1 )
130+ numbered = self .config .get ('auto_toc_tree_numbered' , None )
128131 # when auto_toc_tree_section is set
129132 # only auto generate toctree under the specified section title
130133 sec = self .config ['auto_toc_tree_section' ]
@@ -147,11 +150,12 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
147150 if title .astext ().strip () != sec :
148151 return None
149152
150- numbered = None
151- if isinstance (node , nodes .bullet_list ):
153+ if numbered is not None :
154+ pass
155+ elif isinstance (node , nodes .bullet_list ):
152156 numbered = 0
153157 elif isinstance (node , nodes .enumerated_list ):
154- numbered = 1
158+ numbered = 999
155159
156160 if numbered is None :
157161 return None
@@ -168,10 +172,18 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
168172 ref = par .children [0 ]
169173 if isinstance (ref , addnodes .pending_xref ):
170174 ref = ref .children [0 ]
171- if not isinstance (ref , nodes .reference ):
175+ if isinstance (ref , nodes .Text ):
176+ text = ref .astext ()
177+ title , uri , docpath = text , text , None
178+ elif isinstance (ref , nodes .reference ):
179+ # TODO check that this can't happen and, if so, get rid of
180+ # parse_ref()
181+ self .reporter .warning ('AutoStructify unexpected reference '
182+ '%r' % ref )
183+ title , uri , docpath = self .parse_ref (ref )
184+ else :
172185 return None
173- title , uri , docpath = self .parse_ref (ref )
174- if title is None or uri .startswith ('#' ):
186+ if uri .startswith ('#' ):
175187 return None
176188 if docpath :
177189 refs .append ((title , docpath ))
@@ -183,10 +195,11 @@ def auto_toc_tree(self, node): # pylint: disable=too-many-branches
183195 return self .state_machine .run_directive (
184196 'toctree' ,
185197 options = {
186- 'maxdepth' : 1 ,
198+ 'caption' : sec ,
199+ 'maxdepth' : maxdepth ,
187200 'numbered' : numbered
188201 },
189- content = ['%s <%s>' % ( k , v ) for k , v in refs ]
202+ content = [v for _ , v in refs ]
190203 )
191204
192205 def auto_inline_code (self , node ):
0 commit comments