1 # SPDX-License-Identifier: GPL-2.0
2 # Copyright 2019 Jonathan Corbet <corbet@lwn.net>
4 # Apply kernel-specific tweaks after the initial document processing
7 from docutils import nodes
9 from sphinx import addnodes
10 if sphinx.version_info[0] < 2 or \
11 sphinx.version_info[0] == 2 and sphinx.version_info[1] < 1:
12 from sphinx.environment import NoUri
14 from sphinx.errors import NoUri
16 from itertools import chain
19 # Python 2 lacks re.ASCII...
23 except AttributeError:
27 # Regex nastiness. Of course.
28 # Try to identify "function()" that's not already marked up some
29 # other way. Sphinx doesn't like a lot of stuff right after a
30 # :c:func: block (i.e. ":c:func:`mmap()`s" flakes out), so the last
31 # bit tries to restrict matches to things that won't create trouble.
33 RE_function = re.compile(r'\b(([a-zA-Z_]\w+)\(\))', flags=ascii_p3)
36 # Sphinx 2 uses the same :c:type role for struct, union, enum and typedef
38 RE_generic_type = re.compile(r'\b(struct|union|enum|typedef)\s+([a-zA-Z_]\w+)',
42 # Sphinx 3 uses a different C role for each one of struct, union, enum and
45 RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
46 RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
47 RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
48 RE_typedef = re.compile(r'\b(typedef)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
51 # Detects a reference to a documentation page of the form Documentation/... with
52 # an optional extension
54 RE_doc = re.compile(r'\bDocumentation(/[\w\-_/]+)(\.\w+)*')
57 # Reserved C words that we should skip when cross-referencing
59 Skipnames = [ 'for', 'if', 'register', 'sizeof', 'struct', 'unsigned' ]
63 # Many places in the docs refer to common system calls. It is
64 # pointless to try to cross-reference them and, as has been known
65 # to happen, somebody defining a function by these names can lead
66 # to the creation of incorrect and confusing cross references. So
67 # just don't even try with these names.
69 Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
70 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
73 def markup_refs(docname, app, node):
78 # Associate each regex with the function that will markup its matches
80 markup_func_sphinx2 = {RE_doc: markup_doc_ref,
81 RE_function: markup_c_ref,
82 RE_generic_type: markup_c_ref}
84 markup_func_sphinx3 = {RE_doc: markup_doc_ref,
85 RE_function: markup_func_ref_sphinx3,
86 RE_struct: markup_c_ref,
87 RE_union: markup_c_ref,
88 RE_enum: markup_c_ref,
89 RE_typedef: markup_c_ref}
91 if sphinx.version_info[0] >= 3:
92 markup_func = markup_func_sphinx3
94 markup_func = markup_func_sphinx2
96 match_iterators = [regex.finditer(t) for regex in markup_func]
98 # Sort all references by the starting position in text
100 sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
101 for m in sorted_matches:
103 # Include any text prior to match as a normal text node.
106 repl.append(nodes.Text(t[done:m.start()]))
109 # Call the function associated with the regex that matched this text and
110 # append its return to the text
112 repl.append(markup_func[m.re](docname, app, m))
116 repl.append(nodes.Text(t[done:]))
120 # In sphinx3 we can cross-reference to C macro and function, each one with its
121 # own C role, but both match the same regex, so we try both.
123 def markup_func_ref_sphinx3(docname, app, match):
124 class_str = ['c-func', 'c-macro']
125 reftype_str = ['function', 'macro']
127 cdom = app.env.domains['c']
129 # Go through the dance of getting an xref out of the C domain
131 target = match.group(2)
132 target_text = nodes.Text(match.group(0))
134 if not (target in Skipfuncs or target in Skipnames):
135 for class_s, reftype_s in zip(class_str, reftype_str):
136 lit_text = nodes.literal(classes=['xref', 'c', class_s])
137 lit_text += target_text
138 pxref = addnodes.pending_xref('', refdomain = 'c',
140 reftarget = target, modname = None,
143 # XXX The Latex builder will throw NoUri exceptions here,
144 # work around that by ignoring them.
147 xref = cdom.resolve_xref(app.env, docname, app.builder,
148 reftype_s, target, pxref,
158 def markup_c_ref(docname, app, match):
159 class_str = {# Sphinx 2 only
160 RE_function: 'c-func',
161 RE_generic_type: 'c-type',
163 RE_struct: 'c-struct',
166 RE_typedef: 'c-type',
168 reftype_str = {# Sphinx 2 only
169 RE_function: 'function',
170 RE_generic_type: 'type',
178 cdom = app.env.domains['c']
180 # Go through the dance of getting an xref out of the C domain
182 target = match.group(2)
183 target_text = nodes.Text(match.group(0))
185 if not ((match.re == RE_function and target in Skipfuncs)
186 or (target in Skipnames)):
187 lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
188 lit_text += target_text
189 pxref = addnodes.pending_xref('', refdomain = 'c',
190 reftype = reftype_str[match.re],
191 reftarget = target, modname = None,
194 # XXX The Latex builder will throw NoUri exceptions here,
195 # work around that by ignoring them.
198 xref = cdom.resolve_xref(app.env, docname, app.builder,
199 reftype_str[match.re], target, pxref,
204 # Return the xref if we got it; otherwise just return the plain text.
212 # Try to replace a documentation reference of the form Documentation/... with a
213 # cross reference to that page
215 def markup_doc_ref(docname, app, match):
216 stddom = app.env.domains['std']
218 # Go through the dance of getting an xref out of the std domain
220 target = match.group(1)
222 pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
223 reftarget = target, modname = None,
224 classname = None, refexplicit = False)
226 # XXX The Latex builder will throw NoUri exceptions here,
227 # work around that by ignoring them.
230 xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
235 # Return the xref if we got it; otherwise just return the plain text.
240 return nodes.Text(match.group(0))
242 def auto_markup(app, doctree, name):
244 # This loop could eventually be improved on. Someday maybe we
245 # want a proper tree traversal with a lot of awareness of which
246 # kinds of nodes to prune. But this works well for now.
248 # The nodes.literal test catches ``literal text``, its purpose is to
249 # avoid adding cross-references to functions that have been explicitly
250 # marked with cc:func:.
252 for para in doctree.traverse(nodes.paragraph):
253 for node in para.traverse(nodes.Text):
254 if not isinstance(node.parent, nodes.literal):
255 node.parent.replace(node, markup_refs(name, app, node))
258 app.connect('doctree-resolved', auto_markup)
260 'parallel_read_safe': True,
261 'parallel_write_safe': True,