22
33from __future__ import annotations
44
5+ import re
6+
57from docutils import nodes
68from sphinx import addnodes
79from sphinx .domains .changeset import (
@@ -87,9 +89,11 @@ class SoftDeprecated(PyVersionChange):
8789 with "Soft deprecated" linking to the glossary definition.
8890 """
8991
92+ _TERM_RE = re .compile (r":term:`([^`]+)`" )
93+
9094 def run (self ) -> list [Node ]:
9195 versionlabels [self .name ] = sphinx_gettext (
92- "Soft deprecated since version %s"
96+ ":term:` Soft deprecated` since version %s"
9397 )
9498 versionlabel_classes [self .name ] = "soft-deprecated"
9599 try :
@@ -108,38 +112,35 @@ def run(self) -> list[Node]:
108112
109113 return result
110114
111- @staticmethod
112- def _add_glossary_link (inline : nodes .inline ) -> None :
113- """Replace 'Soft deprecated' text with a cross-reference to the
114- :term:`soft deprecated` glossary entry."""
115- marker = sphinx_gettext ("Soft deprecated" )
116- ref = addnodes .pending_xref (
117- "" ,
118- nodes .Text (marker ),
119- refdomain = "std" ,
120- reftype = "term" ,
121- reftarget = "soft deprecated" ,
122- refwarn = True ,
123- )
124-
115+ @classmethod
116+ def _add_glossary_link (cls , inline : nodes .inline ) -> None :
117+ """Replace :term:`soft deprecated` text with a cross-reference to the
118+ 'Soft deprecated' glossary entry."""
125119 for child in inline .children :
126120 if not isinstance (child , nodes .Text ):
127121 continue
128122
129123 text = str (child )
130- idx = text . find ( marker )
131- if idx < 0 :
124+ match = cls . _TERM_RE . search ( text )
125+ if match is None :
132126 continue
133127
134- # Replace the text node with the split parts using docutils API
135- new_nodes : list [nodes .Node ] = []
136- if idx > 0 :
137- new_nodes .append (nodes .Text (text [:idx ]))
128+ ref = addnodes .pending_xref (
129+ "" ,
130+ nodes .Text (match .group (1 )),
131+ refdomain = "std" ,
132+ reftype = "term" ,
133+ reftarget = "soft deprecated" ,
134+ refwarn = True ,
135+ )
138136
137+ start , end = match .span ()
138+ new_nodes : list [nodes .Node ] = []
139+ if start > 0 :
140+ new_nodes .append (nodes .Text (text [:start ]))
139141 new_nodes .append (ref )
140- remainder = text [idx + len (marker ) :]
141- if remainder :
142- new_nodes .append (nodes .Text (remainder ))
142+ if end < len (text ):
143+ new_nodes .append (nodes .Text (text [end :]))
143144
144145 child .parent .replace (child , new_nodes )
145146 break
0 commit comments