blob: 1cc3a2cf2a88fe1992019a459a81663bc40fcd3c [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#
Nícolas F. R. A. Prado3050edf2020-10-13 23:13:23 +000049# Reserved C words that we should skip when cross-referencing
50#
51Skipnames = [ 'for', 'if', 'register', 'sizeof', 'struct', 'unsigned' ]
52
53
54#
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060055# Many places in the docs refer to common system calls. It is
56# pointless to try to cross-reference them and, as has been known
57# to happen, somebody defining a function by these names can lead
58# to the creation of incorrect and confusing cross references. So
59# just don't even try with these names.
60#
Jonathan Neuschäfer11fec0092019-08-12 18:07:04 +020061Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
Jonathan Neuschäfer82bf8292019-08-12 18:07:05 +020062 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
63 'socket' ]
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060064
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000065def markup_refs(docname, app, node):
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060066 t = node.astext()
67 done = 0
68 repl = [ ]
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000069 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000070 # Associate each regex with the function that will markup its matches
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000071 #
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +000072 markup_func_sphinx2 = {RE_doc: markup_doc_ref,
73 RE_function: markup_c_ref,
74 RE_generic_type: markup_c_ref}
75
76 markup_func_sphinx3 = {RE_doc: markup_doc_ref,
77 RE_function: markup_c_ref,
78 RE_struct: markup_c_ref,
79 RE_union: markup_c_ref,
80 RE_enum: markup_c_ref,
81 RE_typedef: markup_c_ref}
82
83 if sphinx.version_info[0] >= 3:
84 markup_func = markup_func_sphinx3
85 else:
86 markup_func = markup_func_sphinx2
87
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000088 match_iterators = [regex.finditer(t) for regex in markup_func]
89 #
90 # Sort all references by the starting position in text
91 #
92 sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000093 for m in sorted_matches:
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060094 #
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000095 # Include any text prior to match as a normal text node.
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060096 #
97 if m.start() > done:
98 repl.append(nodes.Text(t[done:m.start()]))
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000099
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600100 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000101 # Call the function associated with the regex that matched this text and
102 # append its return to the text
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600103 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000104 repl.append(markup_func[m.re](docname, app, m))
105
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600106 done = m.end()
107 if done < len(t):
108 repl.append(nodes.Text(t[done:]))
109 return repl
110
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000111#
112# Try to replace a C reference (function() or struct/union/enum/typedef
113# type_name) with an appropriate cross reference.
114#
115def markup_c_ref(docname, app, match):
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +0000116 class_str = {RE_function: 'c-func',
117 # Sphinx 2 only
118 RE_generic_type: 'c-type',
119 # Sphinx 3+ only
120 RE_struct: 'c-struct',
121 RE_union: 'c-union',
122 RE_enum: 'c-enum',
123 RE_typedef: 'c-type',
124 }
125 reftype_str = {RE_function: 'function',
126 # Sphinx 2 only
127 RE_generic_type: 'type',
128 # Sphinx 3+ only
129 RE_struct: 'struct',
130 RE_union: 'union',
131 RE_enum: 'enum',
132 RE_typedef: 'type',
133 }
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000134
135 cdom = app.env.domains['c']
136 #
137 # Go through the dance of getting an xref out of the C domain
138 #
139 target = match.group(2)
140 target_text = nodes.Text(match.group(0))
141 xref = None
Nícolas F. R. A. Prado3050edf2020-10-13 23:13:23 +0000142 if not ((match.re == RE_function and target in Skipfuncs)
143 or (target in Skipnames)):
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000144 lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
145 lit_text += target_text
146 pxref = addnodes.pending_xref('', refdomain = 'c',
147 reftype = reftype_str[match.re],
148 reftarget = target, modname = None,
149 classname = None)
150 #
151 # XXX The Latex builder will throw NoUri exceptions here,
152 # work around that by ignoring them.
153 #
154 try:
155 xref = cdom.resolve_xref(app.env, docname, app.builder,
156 reftype_str[match.re], target, pxref,
157 lit_text)
158 except NoUri:
159 xref = None
160 #
161 # Return the xref if we got it; otherwise just return the plain text.
162 #
163 if xref:
164 return xref
165 else:
166 return target_text
167
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +0000168#
169# Try to replace a documentation reference of the form Documentation/... with a
170# cross reference to that page
171#
172def markup_doc_ref(docname, app, match):
173 stddom = app.env.domains['std']
174 #
175 # Go through the dance of getting an xref out of the std domain
176 #
177 target = match.group(1)
178 xref = None
179 pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
180 reftarget = target, modname = None,
181 classname = None, refexplicit = False)
182 #
183 # XXX The Latex builder will throw NoUri exceptions here,
184 # work around that by ignoring them.
185 #
186 try:
187 xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
188 target, pxref, None)
189 except NoUri:
190 xref = None
191 #
192 # Return the xref if we got it; otherwise just return the plain text.
193 #
194 if xref:
195 return xref
196 else:
197 return nodes.Text(match.group(0))
198
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600199def auto_markup(app, doctree, name):
200 #
201 # This loop could eventually be improved on. Someday maybe we
202 # want a proper tree traversal with a lot of awareness of which
203 # kinds of nodes to prune. But this works well for now.
204 #
205 # The nodes.literal test catches ``literal text``, its purpose is to
206 # avoid adding cross-references to functions that have been explicitly
207 # marked with cc:func:.
208 #
209 for para in doctree.traverse(nodes.paragraph):
210 for node in para.traverse(nodes.Text):
211 if not isinstance(node.parent, nodes.literal):
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000212 node.parent.replace(node, markup_refs(name, app, node))
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600213
214def setup(app):
215 app.connect('doctree-resolved', auto_markup)
216 return {
217 'parallel_read_safe': True,
218 'parallel_write_safe': True,
219 }