Skip to content

Commit fb42d8d

Browse files
nfrapradoJonathan Corbet
authored and
Jonathan Corbet
committed
docs: automarkup: Move common logic to add and resolve xref to helper
Several of the markup functions contain the same code, calling into sphinx's pending_xref and resolve_xref functions to add and resolve a cross-reference, with only a few of the parameters changed (domain, reference type, markup content). Move this logic to its own function and reuse it in the markup functions. No functional change. Signed-off-by: Nícolas F. R. A. Prado <[email protected]> Signed-off-by: Jonathan Corbet <[email protected]> Link: https://lore.kernel.org/r/20250408-automarkup-resolve-xref-helper-v2-1-e0a9b8fc7fdd@collabora.com
1 parent 9f488cc commit fb42d8d

File tree

1 file changed

+24
-73
lines changed

1 file changed

+24
-73
lines changed

Documentation/sphinx/automarkup.py

Lines changed: 24 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,8 @@ def note_failure(target):
128128
# own C role, but both match the same regex, so we try both.
129129
#
130130
def markup_func_ref_sphinx3(docname, app, match):
131-
cdom = app.env.domains['c']
132-
#
133-
# Go through the dance of getting an xref out of the C domain
134-
#
135131
base_target = match.group(2)
136132
target_text = nodes.Text(match.group(0))
137-
xref = None
138133
possible_targets = [base_target]
139134
# Check if this document has a namespace, and if so, try
140135
# cross-referencing inside it first.
@@ -146,22 +141,8 @@ def markup_func_ref_sphinx3(docname, app, match):
146141
if (target not in Skipfuncs) and not failure_seen(target):
147142
lit_text = nodes.literal(classes=['xref', 'c', 'c-func'])
148143
lit_text += target_text
149-
pxref = addnodes.pending_xref('', refdomain = 'c',
150-
reftype = 'function',
151-
reftarget = target,
152-
modname = None,
153-
classname = None)
154-
#
155-
# XXX The Latex builder will throw NoUri exceptions here,
156-
# work around that by ignoring them.
157-
#
158-
try:
159-
xref = cdom.resolve_xref(app.env, docname, app.builder,
160-
'function', target, pxref,
161-
lit_text)
162-
except NoUri:
163-
xref = None
164-
144+
xref = add_and_resolve_xref(app, docname, 'c', 'function',
145+
target, contnode=lit_text)
165146
if xref:
166147
return xref
167148
note_failure(target)
@@ -188,13 +169,8 @@ def markup_c_ref(docname, app, match):
188169
RE_typedef: 'type',
189170
}
190171

191-
cdom = app.env.domains['c']
192-
#
193-
# Go through the dance of getting an xref out of the C domain
194-
#
195172
base_target = match.group(2)
196173
target_text = nodes.Text(match.group(0))
197-
xref = None
198174
possible_targets = [base_target]
199175
# Check if this document has a namespace, and if so, try
200176
# cross-referencing inside it first.
@@ -206,21 +182,9 @@ def markup_c_ref(docname, app, match):
206182
if not (match.re == RE_function and target in Skipfuncs):
207183
lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
208184
lit_text += target_text
209-
pxref = addnodes.pending_xref('', refdomain = 'c',
210-
reftype = reftype_str[match.re],
211-
reftarget = target, modname = None,
212-
classname = None)
213-
#
214-
# XXX The Latex builder will throw NoUri exceptions here,
215-
# work around that by ignoring them.
216-
#
217-
try:
218-
xref = cdom.resolve_xref(app.env, docname, app.builder,
219-
reftype_str[match.re], target, pxref,
220-
lit_text)
221-
except NoUri:
222-
xref = None
223-
185+
xref = add_and_resolve_xref(app, docname, 'c',
186+
reftype_str[match.re], target,
187+
contnode=lit_text)
224188
if xref:
225189
return xref
226190

@@ -231,30 +195,12 @@ def markup_c_ref(docname, app, match):
231195
# cross reference to that page
232196
#
233197
def markup_doc_ref(docname, app, match):
234-
stddom = app.env.domains['std']
235-
#
236-
# Go through the dance of getting an xref out of the std domain
237-
#
238198
absolute = match.group(1)
239199
target = match.group(2)
240200
if absolute:
241201
target = "/" + target
242-
xref = None
243-
pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
244-
reftarget = target, modname = None,
245-
classname = None, refexplicit = False)
246-
#
247-
# XXX The Latex builder will throw NoUri exceptions here,
248-
# work around that by ignoring them.
249-
#
250-
try:
251-
xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
252-
target, pxref, None)
253-
except NoUri:
254-
xref = None
255-
#
256-
# Return the xref if we got it; otherwise just return the plain text.
257-
#
202+
203+
xref = add_and_resolve_xref(app, docname, 'std', 'doc', target)
258204
if xref:
259205
return xref
260206
else:
@@ -265,10 +211,6 @@ def markup_doc_ref(docname, app, match):
265211
# with a cross reference to that page
266212
#
267213
def markup_abi_ref(docname, app, match, warning=False):
268-
stddom = app.env.domains['std']
269-
#
270-
# Go through the dance of getting an xref out of the std domain
271-
#
272214
kernel_abi = get_kernel_abi()
273215

274216
fname = match.group(1)
@@ -280,7 +222,18 @@ def markup_abi_ref(docname, app, match, warning=False):
280222
kernel_abi.log.warning("%s not found", fname)
281223
return nodes.Text(match.group(0))
282224

283-
pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'ref',
225+
xref = add_and_resolve_xref(app, docname, 'std', 'ref', target)
226+
if xref:
227+
return xref
228+
else:
229+
return nodes.Text(match.group(0))
230+
231+
def add_and_resolve_xref(app, docname, domain, reftype, target, contnode=None):
232+
#
233+
# Go through the dance of getting an xref out of the corresponding domain
234+
#
235+
dom_obj = app.env.domains[domain]
236+
pxref = addnodes.pending_xref('', refdomain = domain, reftype = reftype,
284237
reftarget = target, modname = None,
285238
classname = None, refexplicit = False)
286239

@@ -289,17 +242,15 @@ def markup_abi_ref(docname, app, match, warning=False):
289242
# work around that by ignoring them.
290243
#
291244
try:
292-
xref = stddom.resolve_xref(app.env, docname, app.builder, 'ref',
293-
target, pxref, None)
245+
xref = dom_obj.resolve_xref(app.env, docname, app.builder, reftype,
246+
target, pxref, contnode)
294247
except NoUri:
295248
xref = None
296-
#
297-
# Return the xref if we got it; otherwise just return the plain text.
298-
#
249+
299250
if xref:
300251
return xref
301-
else:
302-
return nodes.Text(match.group(0))
252+
253+
return None
303254

304255
#
305256
# Variant of markup_abi_ref() that warns whan a reference is not found

0 commit comments

Comments
 (0)