11"""
2- sphinxnotes.any.directives
3- ~~~~~~~~~~~~~~~~~~~~~~~~~~
2+ sphinxnotes.any.directives
3+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
44
5- Directive implementations.
5+ Directive implementations.
66
7- :copyright: Copyright 2021 Shengyu Zhang
8- :license: BSD, see LICENSE for details.
7+ :copyright: Copyright 2021 Shengyu Zhang
8+ :license: BSD, see LICENSE for details.
99"""
10+
1011from __future__ import annotations
1112from typing import Type
1213
2425
2526logger = logging .getLogger (__name__ )
2627
28+
2729class AnyDirective (SphinxDirective ):
2830 """
2931 Directive to describe anything. Not used directly,
@@ -32,17 +34,17 @@ class AnyDirective(SphinxDirective):
3234 The class is modified from sphinx.directives.ObjectDescription
3335 """
3436
35- schema :Schema
37+ schema : Schema
3638
3739 # Member of parent
38- has_content :bool = True
39- required_arguments :int = 0
40- optional_arguments :int = 0
41- final_argument_whitespace :bool = True
42- option_spec :dict [str ,callable ] = {}
40+ has_content : bool = True
41+ required_arguments : int = 0
42+ optional_arguments : int = 0
43+ final_argument_whitespace : bool = True
44+ option_spec : dict [str , callable ] = {}
4345
4446 @classmethod
45- def derive (cls , schema :Schema ) -> Type [" AnyDirective" ]:
47+ def derive (cls , schema : Schema ) -> Type [' AnyDirective' ]:
4648 """Generate an AnyDirective child class for describing object."""
4749 has_content = schema .content is not None
4850
@@ -64,24 +66,30 @@ def derive(cls, schema:Schema) -> Type["AnyDirective"]:
6466 option_spec [name ] = directives .unchanged
6567
6668 # Generate directive class
67- return type ('Any%sDirective' % schema .objtype .title (),
68- (AnyDirective ,),
69- {'schema' : schema ,
70- 'has_content' : has_content ,
71- 'required_arguments' : required_arguments ,
72- 'optional_arguments' : optional_arguments ,
73- 'option_spec' : option_spec , })
74-
69+ return type (
70+ 'Any%sDirective' % schema .objtype .title (),
71+ (AnyDirective ,),
72+ {
73+ 'schema' : schema ,
74+ 'has_content' : has_content ,
75+ 'required_arguments' : required_arguments ,
76+ 'optional_arguments' : optional_arguments ,
77+ 'option_spec' : option_spec ,
78+ },
79+ )
7580
7681 def _build_object (self ) -> Object :
7782 """Build object information for template rendering."""
78- return self .schema .object (name = self .arguments [0 ] if self .arguments else None ,
79- attrs = self .options ,
80- # Convert docutils.statemachine.ViewList.data -> str
81- content = '\n ' .join (list (self .content .data )))
82-
83-
84- def _setup_nodes (self , obj :Object , sectnode :Element , ahrnode :Element | None , contnode :Element ) -> None :
83+ return self .schema .object (
84+ name = self .arguments [0 ] if self .arguments else None ,
85+ attrs = self .options ,
86+ # Convert docutils.statemachine.ViewList.data -> str
87+ content = '\n ' .join (list (self .content .data )),
88+ )
89+
90+ def _setup_nodes (
91+ self , obj : Object , sectnode : Element , ahrnode : Element | None , contnode : Element
92+ ) -> None :
8593 """
8694 Attach necessary informations to nodes and note them.
8795
@@ -116,23 +124,26 @@ def _setup_nodes(self, obj:Object, sectnode:Element, ahrnode:Element|None, contn
116124 ahrnode ['names' ].extend ([fully_normalize_name (x ) for x in name ])
117125 self .state .document .note_explicit_target (ahrnode )
118126 # Note object by docu fields
119- domain .note_object (self .env .docname , ahrid , self .schema , obj ) # FIXME: Cast to AnyDomain
127+ domain .note_object (
128+ self .env .docname , ahrid , self .schema , obj
129+ ) # FIXME: Cast to AnyDomain
120130
121131 # Parse description
122- nested_parse_with_titles (self .state ,
123- StringList (self .schema .render_description (obj )),
124- contnode )
125-
132+ nested_parse_with_titles (
133+ self .state , StringList (self .schema .render_description (obj )), contnode
134+ )
126135
127- def _run_section (self , obj :Object ) -> list [Node ]:
136+ def _run_section (self , obj : Object ) -> list [Node ]:
128137 # Get the title of the "section" where the directive is located
129138 sectnode = self .state .parent
130139 titlenode = sectnode .next_node (nodes .title )
131140 if not titlenode or titlenode .parent != sectnode :
132141 # Title should be direct child of section
133142 msg = 'Failed to get title of current section'
134143 logger .warning (msg , location = sectnode )
135- sm = nodes .system_message (msg , type = 'WARNING' , level = 2 , backrefs = [], source = '' )
144+ sm = nodes .system_message (
145+ msg , type = 'WARNING' , level = 2 , backrefs = [], source = ''
146+ )
136147 sectnode += sm
137148 title = ''
138149 else :
@@ -152,8 +163,7 @@ def _run_section(self, obj:Object) -> list[Node]:
152163 # Add all content to existed section, so return nothing
153164 return []
154165
155-
156- def _run_objdesc (self , obj :Object ) -> list [Node ]:
166+ def _run_objdesc (self , obj : Object ) -> list [Node ]:
157167 descnode = addnodes .desc ()
158168
159169 # Generate signature node
@@ -175,7 +185,6 @@ def _run_objdesc(self, obj:Object) -> list[Node]:
175185 self ._setup_nodes (obj , descnode , signode , contnode )
176186 return [descnode ]
177187
178-
179188 def run (self ) -> list [Node ]:
180189 obj = self ._build_object ()
181190 if self .schema .title_of (obj ) == '_' :
0 commit comments