Skip to content

Commit 1ac4cfb

Browse files
Nícolas F. R. A. PradoJonathan Corbet
authored andcommitted
docs: Allow multiple automarkup functions
The automarkup script previously matched expressions and substituted them with markup to enable automatic cross-reference all in the same function. Split the expression matching iteration and the markup substitution into different functions to make it easier to add new regular expressions and functions to treat each of them. Signed-off-by: Nícolas F. R. A. Prado <nfraprado@protonmail.com> Link: https://lore.kernel.org/r/20200911133339.327721-2-nfraprado@protonmail.com Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent 78ff97e commit 1ac4cfb

1 file changed

Lines changed: 55 additions & 42 deletions

File tree

‎Documentation/sphinx/automarkup.py‎

Lines changed: 55 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -36,65 +36,78 @@
3636
'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
3737
'socket' ]
3838

39-
#
40-
# Find all occurrences of C references (function() and struct/union/enum/typedef
41-
# type_name) and try to replace them with appropriate cross references.
42-
#
43-
def markup_c_refs(docname, app, node):
44-
class_str = {RE_function: 'c-func', RE_type: 'c-type'}
45-
reftype_str = {RE_function: 'function', RE_type: 'type'}
46-
47-
cdom = app.env.domains['c']
39+
def markup_refs(docname, app, node):
4840
t = node.astext()
4941
done = 0
5042
repl = [ ]
5143
#
52-
# Sort all C references by the starting position in text
44+
# Associate each regex with the function that will markup its matches
45+
#
46+
markup_func = {RE_type: markup_c_ref,
47+
RE_function: markup_c_ref}
48+
match_iterators = [regex.finditer(t) for regex in markup_func]
5349
#
54-
sorted_matches = sorted(chain(RE_type.finditer(t), RE_function.finditer(t)),
55-
key=lambda m: m.start())
50+
# Sort all references by the starting position in text
51+
#
52+
sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
5653
for m in sorted_matches:
5754
#
5855
# Include any text prior to match as a normal text node.
5956
#
6057
if m.start() > done:
6158
repl.append(nodes.Text(t[done:m.start()]))
59+
6260
#
63-
# Go through the dance of getting an xref out of the C domain
64-
#
65-
target = m.group(2)
66-
target_text = nodes.Text(m.group(0))
67-
xref = None
68-
if not (m.re == RE_function and target in Skipfuncs):
69-
lit_text = nodes.literal(classes=['xref', 'c', class_str[m.re]])
70-
lit_text += target_text
71-
pxref = addnodes.pending_xref('', refdomain = 'c',
72-
reftype = reftype_str[m.re],
73-
reftarget = target, modname = None,
74-
classname = None)
75-
#
76-
# XXX The Latex builder will throw NoUri exceptions here,
77-
# work around that by ignoring them.
78-
#
79-
try:
80-
xref = cdom.resolve_xref(app.env, docname, app.builder,
81-
reftype_str[m.re], target, pxref,
82-
lit_text)
83-
except NoUri:
84-
xref = None
85-
#
86-
# Toss the xref into the list if we got it; otherwise just put
87-
# the function text.
61+
# Call the function associated with the regex that matched this text and
62+
# append its return to the text
8863
#
89-
if xref:
90-
repl.append(xref)
91-
else:
92-
repl.append(target_text)
64+
repl.append(markup_func[m.re](docname, app, m))
65+
9366
done = m.end()
9467
if done < len(t):
9568
repl.append(nodes.Text(t[done:]))
9669
return repl
9770

71+
#
72+
# Try to replace a C reference (function() or struct/union/enum/typedef
73+
# type_name) with an appropriate cross reference.
74+
#
75+
def markup_c_ref(docname, app, match):
76+
class_str = {RE_function: 'c-func', RE_type: 'c-type'}
77+
reftype_str = {RE_function: 'function', RE_type: 'type'}
78+
79+
cdom = app.env.domains['c']
80+
#
81+
# Go through the dance of getting an xref out of the C domain
82+
#
83+
target = match.group(2)
84+
target_text = nodes.Text(match.group(0))
85+
xref = None
86+
if not (match.re == RE_function and target in Skipfuncs):
87+
lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
88+
lit_text += target_text
89+
pxref = addnodes.pending_xref('', refdomain = 'c',
90+
reftype = reftype_str[match.re],
91+
reftarget = target, modname = None,
92+
classname = None)
93+
#
94+
# XXX The Latex builder will throw NoUri exceptions here,
95+
# work around that by ignoring them.
96+
#
97+
try:
98+
xref = cdom.resolve_xref(app.env, docname, app.builder,
99+
reftype_str[match.re], target, pxref,
100+
lit_text)
101+
except NoUri:
102+
xref = None
103+
#
104+
# Return the xref if we got it; otherwise just return the plain text.
105+
#
106+
if xref:
107+
return xref
108+
else:
109+
return target_text
110+
98111
def auto_markup(app, doctree, name):
99112
#
100113
# This loop could eventually be improved on. Someday maybe we
@@ -108,7 +121,7 @@ def auto_markup(app, doctree, name):
108121
for para in doctree.traverse(nodes.paragraph):
109122
for node in para.traverse(nodes.Text):
110123
if not isinstance(node.parent, nodes.literal):
111-
node.parent.replace(node, markup_c_refs(name, app, node))
124+
node.parent.replace(node, markup_refs(name, app, node))
112125

113126
def setup(app):
114127
app.connect('doctree-resolved', auto_markup)

0 commit comments

Comments
 (0)