blob: 43dd9025fc77d5c540da721a8c2521dc6f4e07e7 [file] [log] [blame]
Jonathan Corbetd74b0d32019-04-25 07:55:07 -06001# SPDX-License-Identifier: GPL-2.0
2# Copyright 2019 Jonathan Corbet <corbet@lwn.net>
3#
4# Apply kernel-specific tweaks after the initial document processing
5# has been done.
6#
7from docutils import nodes
Jonathan Corbetbcac3862020-01-22 16:06:28 -07008import sphinx
Jonathan Corbetd74b0d32019-04-25 07:55:07 -06009from sphinx import addnodes
Jonathan Corbetbcac3862020-01-22 16:06:28 -070010if sphinx.version_info[0] < 2 or \
11 sphinx.version_info[0] == 2 and sphinx.version_info[1] < 1:
12 from sphinx.environment import NoUri
13else:
14 from sphinx.errors import NoUri
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060015import re
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000016from itertools import chain
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060017
18#
19# Regex nastiness. Of course.
20# Try to identify "function()" that's not already marked up some
21# other way. Sphinx doesn't like a lot of stuff right after a
22# :c:func: block (i.e. ":c:func:`mmap()`s" flakes out), so the last
23# bit tries to restrict matches to things that won't create trouble.
24#
Nícolas F. R. A. Pradof66e47f2020-10-13 23:13:17 +000025RE_function = re.compile(r'\b(([a-zA-Z_]\w+)\(\))', flags=re.ASCII)
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +000026
27#
28# Sphinx 2 uses the same :c:type role for struct, union, enum and typedef
29#
Nícolas F. R. A. Pradof66e47f2020-10-13 23:13:17 +000030RE_generic_type = re.compile(r'\b(struct|union|enum|typedef)\s+([a-zA-Z_]\w+)',
31 flags=re.ASCII)
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +000032
33#
34# Sphinx 3 uses a different C role for each one of struct, union, enum and
35# typedef
36#
37RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
38RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
39RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
40RE_typedef = re.compile(r'\b(typedef)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
41
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +000042#
43# Detects a reference to a documentation page of the form Documentation/... with
44# an optional extension
45#
Nícolas F. R. A. Pradof66e47f2020-10-13 23:13:17 +000046RE_doc = re.compile(r'\bDocumentation(/[\w\-_/]+)(\.\w+)*')
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060047
48#
49# Many places in the docs refer to common system calls. It is
50# pointless to try to cross-reference them and, as has been known
51# to happen, somebody defining a function by these names can lead
52# to the creation of incorrect and confusing cross references. So
53# just don't even try with these names.
54#
Jonathan Neuschäfer11fec0092019-08-12 18:07:04 +020055Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
Jonathan Neuschäfer82bf8292019-08-12 18:07:05 +020056 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
57 'socket' ]
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060058
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000059def markup_refs(docname, app, node):
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060060 t = node.astext()
61 done = 0
62 repl = [ ]
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000063 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000064 # Associate each regex with the function that will markup its matches
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000065 #
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +000066 markup_func_sphinx2 = {RE_doc: markup_doc_ref,
67 RE_function: markup_c_ref,
68 RE_generic_type: markup_c_ref}
69
70 markup_func_sphinx3 = {RE_doc: markup_doc_ref,
71 RE_function: markup_c_ref,
72 RE_struct: markup_c_ref,
73 RE_union: markup_c_ref,
74 RE_enum: markup_c_ref,
75 RE_typedef: markup_c_ref}
76
77 if sphinx.version_info[0] >= 3:
78 markup_func = markup_func_sphinx3
79 else:
80 markup_func = markup_func_sphinx2
81
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000082 match_iterators = [regex.finditer(t) for regex in markup_func]
83 #
84 # Sort all references by the starting position in text
85 #
86 sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000087 for m in sorted_matches:
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060088 #
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000089 # Include any text prior to match as a normal text node.
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060090 #
91 if m.start() > done:
92 repl.append(nodes.Text(t[done:m.start()]))
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000093
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060094 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000095 # Call the function associated with the regex that matched this text and
96 # append its return to the text
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060097 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000098 repl.append(markup_func[m.re](docname, app, m))
99
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600100 done = m.end()
101 if done < len(t):
102 repl.append(nodes.Text(t[done:]))
103 return repl
104
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000105#
106# Try to replace a C reference (function() or struct/union/enum/typedef
107# type_name) with an appropriate cross reference.
108#
109def markup_c_ref(docname, app, match):
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +0000110 class_str = {RE_function: 'c-func',
111 # Sphinx 2 only
112 RE_generic_type: 'c-type',
113 # Sphinx 3+ only
114 RE_struct: 'c-struct',
115 RE_union: 'c-union',
116 RE_enum: 'c-enum',
117 RE_typedef: 'c-type',
118 }
119 reftype_str = {RE_function: 'function',
120 # Sphinx 2 only
121 RE_generic_type: 'type',
122 # Sphinx 3+ only
123 RE_struct: 'struct',
124 RE_union: 'union',
125 RE_enum: 'enum',
126 RE_typedef: 'type',
127 }
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000128
129 cdom = app.env.domains['c']
130 #
131 # Go through the dance of getting an xref out of the C domain
132 #
133 target = match.group(2)
134 target_text = nodes.Text(match.group(0))
135 xref = None
136 if not (match.re == RE_function and target in Skipfuncs):
137 lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
138 lit_text += target_text
139 pxref = addnodes.pending_xref('', refdomain = 'c',
140 reftype = reftype_str[match.re],
141 reftarget = target, modname = None,
142 classname = None)
143 #
144 # XXX The Latex builder will throw NoUri exceptions here,
145 # work around that by ignoring them.
146 #
147 try:
148 xref = cdom.resolve_xref(app.env, docname, app.builder,
149 reftype_str[match.re], target, pxref,
150 lit_text)
151 except NoUri:
152 xref = None
153 #
154 # Return the xref if we got it; otherwise just return the plain text.
155 #
156 if xref:
157 return xref
158 else:
159 return target_text
160
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +0000161#
162# Try to replace a documentation reference of the form Documentation/... with a
163# cross reference to that page
164#
165def markup_doc_ref(docname, app, match):
166 stddom = app.env.domains['std']
167 #
168 # Go through the dance of getting an xref out of the std domain
169 #
170 target = match.group(1)
171 xref = None
172 pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
173 reftarget = target, modname = None,
174 classname = None, refexplicit = False)
175 #
176 # XXX The Latex builder will throw NoUri exceptions here,
177 # work around that by ignoring them.
178 #
179 try:
180 xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
181 target, pxref, None)
182 except NoUri:
183 xref = None
184 #
185 # Return the xref if we got it; otherwise just return the plain text.
186 #
187 if xref:
188 return xref
189 else:
190 return nodes.Text(match.group(0))
191
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600192def auto_markup(app, doctree, name):
193 #
194 # This loop could eventually be improved on. Someday maybe we
195 # want a proper tree traversal with a lot of awareness of which
196 # kinds of nodes to prune. But this works well for now.
197 #
198 # The nodes.literal test catches ``literal text``, its purpose is to
199 # avoid adding cross-references to functions that have been explicitly
200 # marked with cc:func:.
201 #
202 for para in doctree.traverse(nodes.paragraph):
203 for node in para.traverse(nodes.Text):
204 if not isinstance(node.parent, nodes.literal):
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000205 node.parent.replace(node, markup_refs(name, app, node))
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600206
207def setup(app):
208 app.connect('doctree-resolved', auto_markup)
209 return {
210 'parallel_read_safe': True,
211 'parallel_write_safe': True,
212 }