77"""
88
99from __future__ import annotations
10+ from os import wait
1011from typing import Any
1112
1213from docutils import nodes
1314from docutils .parsers .rst import directives
14-
15+ from docutils . statemachine import StringList
1516from sphinx .util import logging
1617from sphinx .util .docutils import SphinxDirective , SphinxRole
1718from sphinx .application import Sphinx
2122from .freestyle import FreeStyleDirective , FreeStyleOptionSpec
2223from .data import Data , Field , Schema , Raw
2324from .render import Template , Phase , render , JinjaEnv
24- from .utils import find_current_document , find_current_section , TempData
25+ from .utils import find_current_document , find_current_section , TempData , find_first_child
2526from .context_proxy import proxy
2627
2728logger = logging .getLogger (__name__ )
@@ -126,8 +127,56 @@ def sphinx_context(v: SphinxDirective | SphinxRole | SphinxTransform) -> dict[st
126127 ctx ['_config' ] = proxy (v .config )
127128 return ctx
128129
130+ class BaseDefineDirective ():
131+ def extract_raw_data (self ) -> Raw :
132+ return Raw (
133+ self .arguments [0 ] if self .arguments else None ,
134+ self .options .copy (),
135+ '\n ' .join (self .content ) if self .has_content else None ,
136+ )
129137
130- class DataDefineDirective (FreeStyleDirective ):
138+ class AutoDirective (BaseDefineDirective ):
139+ def _lookup_titular_node (self , node : nodes .Element | None ) -> nodes .Node | None :
140+ if node is None :
141+ logger .warning ('none' )
142+ return None
143+ if isinstance (node , (nodes .section , nodes .sidebar )):
144+ logger .warning ('section' )
145+ if title := find_first_child (node , nodes .title ):
146+ logger .warning ('title' )
147+ return title
148+ if isinstance (node , nodes .definition_list_item ):
149+ logger .warning ('definition_list_item' )
150+ if term := find_first_child (node , nodes .term ):
151+ logger .warning ('definition_list_item' )
152+ return term
153+ if isinstance (node , nodes .field ):
154+ logger .warning ('field' )
155+ if field := find_first_child (node , nodes .field_name ):
156+ logger .warning ('field_name' )
157+ return field
158+ if isinstance (node , nodes .list_item ):
159+ logger .warning ('listitem' )
160+ if para := find_first_child (node , nodes .paragraph ):
161+ logger .warning ('paragraph' )
162+ return para
163+ return self ._lookup_titular_node (node .parent )
164+
165+
166+ def extract_raw_data (self ) -> Raw :
167+ raw = super ().extract_raw_data ()
168+ if not raw .name :
169+ logger .warning ('>>>>>>>>>>>>>>>>>>>>>>>>' )
170+ if title := self ._lookup_titular_node (self .state .parent ):
171+ raw .name = title .astext ()
172+ logger .warning (f'{ raw .name } ' )
173+ logger .warning ('<<<<<<<<<<<<<<<<<<<<<<<<' )
174+ if not raw .content :
175+ raw .content_missing = True
176+ return raw
177+
178+
179+ class DefineDirective (FreeStyleDirective , AutoDirective ):
131180 optional_arguments = 1
132181 has_content = True
133182
@@ -137,7 +186,7 @@ def run(self) -> list[nodes.Node]:
137186 return []
138187
139188 schema = SchemaStore .get (self .state .document )
140- rawdata = self ._raw_data ()
189+ rawdata = self .extract_raw_data ()
141190
142191 if tmpl .phase != Phase .Parsing :
143192 n = pending_node ()
@@ -164,23 +213,22 @@ def run(self) -> list[nodes.Node]:
164213
165214 return [n ]
166215
167- def _raw_data (self ) -> Raw :
168- return Raw (
169- self .arguments [0 ] if self .arguments else None ,
170- self .options .copy (),
171- '\n ' .join (self .content ),
172- )
173-
174-
175216class ParsedHookDirective (SphinxDirective ):
176217 def run (self ) -> list [nodes .Node ]:
177218 for pending in self .state .document .findall (pending_node ):
178219 tmpl = TemplateStore .get (pending )
179220 schema = SchemaStore .get (pending )
180221 rawdata = RawDataStore .get (pending )
181222
182- assert tmpl
183- assert rawdata
223+ assert tmpl and rawdata
224+
225+ if rawdata .content_missing :
226+ contnodes = []
227+ for i , child in enumerate (pending .parent ):
228+ if child == pending :
229+ contnodes = pending .parent [i :]
230+ rawdata .content = '\n \n ' .join ([n .astext () for n in contnodes ])
231+ rawdata .content_missing = False
184232
185233 if schema :
186234 try :
@@ -200,7 +248,6 @@ def run(self) -> list[nodes.Node]:
200248
201249 return [] # nothing to return
202250
203-
204251def on_source_read (app , docname , content ):
205252 # NOTE: content is a single element list, representing the content of the
206253 # source file.
@@ -239,7 +286,7 @@ def setup(app: Sphinx):
239286 app .add_directive ('data:tmpl' , TemplateDirective , False )
240287 app .add_directive ('data:schema' , DataSchemaDirective , False )
241288 # app.add_directive('data:use-tmpl', DataTemplateDirective, False)
242- app .add_directive ('data:def' , DataDefineDirective , False )
289+ app .add_directive ('data:def' , DefineDirective , False )
243290 app .add_directive ('data:parsed-hook' , ParsedHookDirective , False )
244291
245292 app .connect ('source-read' , on_source_read )
0 commit comments