blob: db13fb15cedc8cb2b040921668ca1640d663af73 [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. Pradod82b1e82020-09-03 00:58:19 +000025RE_function = re.compile(r'(([\w_][\w\d_]+)\(\))')
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#
30RE_generic_type = re.compile(r'(struct|union|enum|typedef)\s+([\w_][\w\d_]+)')
31
32#
33# Sphinx 3 uses a different C role for each one of struct, union, enum and
34# typedef
35#
36RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
37RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
38RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
39RE_typedef = re.compile(r'\b(typedef)\s+([a-zA-Z_]\w+)', flags=re.ASCII)
40
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +000041#
42# Detects a reference to a documentation page of the form Documentation/... with
43# an optional extension
44#
45RE_doc = re.compile(r'Documentation(/[\w\-_/]+)(\.\w+)*')
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060046
47#
48# Many places in the docs refer to common system calls. It is
49# pointless to try to cross-reference them and, as has been known
50# to happen, somebody defining a function by these names can lead
51# to the creation of incorrect and confusing cross references. So
52# just don't even try with these names.
53#
Jonathan Neuschäfer11fec0092019-08-12 18:07:04 +020054Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
Jonathan Neuschäfer82bf8292019-08-12 18:07:05 +020055 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
56 'socket' ]
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060057
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000058def markup_refs(docname, app, node):
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060059 t = node.astext()
60 done = 0
61 repl = [ ]
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000062 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000063 # Associate each regex with the function that will markup its matches
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000064 #
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +000065 markup_func_sphinx2 = {RE_doc: markup_doc_ref,
66 RE_function: markup_c_ref,
67 RE_generic_type: markup_c_ref}
68
69 markup_func_sphinx3 = {RE_doc: markup_doc_ref,
70 RE_function: markup_c_ref,
71 RE_struct: markup_c_ref,
72 RE_union: markup_c_ref,
73 RE_enum: markup_c_ref,
74 RE_typedef: markup_c_ref}
75
76 if sphinx.version_info[0] >= 3:
77 markup_func = markup_func_sphinx3
78 else:
79 markup_func = markup_func_sphinx2
80
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000081 match_iterators = [regex.finditer(t) for regex in markup_func]
82 #
83 # Sort all references by the starting position in text
84 #
85 sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000086 for m in sorted_matches:
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060087 #
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000088 # Include any text prior to match as a normal text node.
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060089 #
90 if m.start() > done:
91 repl.append(nodes.Text(t[done:m.start()]))
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000092
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060093 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000094 # Call the function associated with the regex that matched this text and
95 # append its return to the text
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060096 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000097 repl.append(markup_func[m.re](docname, app, m))
98
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060099 done = m.end()
100 if done < len(t):
101 repl.append(nodes.Text(t[done:]))
102 return repl
103
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000104#
105# Try to replace a C reference (function() or struct/union/enum/typedef
106# type_name) with an appropriate cross reference.
107#
108def markup_c_ref(docname, app, match):
Nícolas F. R. A. Prado06dc65b2020-10-13 23:13:11 +0000109 class_str = {RE_function: 'c-func',
110 # Sphinx 2 only
111 RE_generic_type: 'c-type',
112 # Sphinx 3+ only
113 RE_struct: 'c-struct',
114 RE_union: 'c-union',
115 RE_enum: 'c-enum',
116 RE_typedef: 'c-type',
117 }
118 reftype_str = {RE_function: 'function',
119 # Sphinx 2 only
120 RE_generic_type: 'type',
121 # Sphinx 3+ only
122 RE_struct: 'struct',
123 RE_union: 'union',
124 RE_enum: 'enum',
125 RE_typedef: 'type',
126 }
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000127
128 cdom = app.env.domains['c']
129 #
130 # Go through the dance of getting an xref out of the C domain
131 #
132 target = match.group(2)
133 target_text = nodes.Text(match.group(0))
134 xref = None
135 if not (match.re == RE_function and target in Skipfuncs):
136 lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
137 lit_text += target_text
138 pxref = addnodes.pending_xref('', refdomain = 'c',
139 reftype = reftype_str[match.re],
140 reftarget = target, modname = None,
141 classname = None)
142 #
143 # XXX The Latex builder will throw NoUri exceptions here,
144 # work around that by ignoring them.
145 #
146 try:
147 xref = cdom.resolve_xref(app.env, docname, app.builder,
148 reftype_str[match.re], target, pxref,
149 lit_text)
150 except NoUri:
151 xref = None
152 #
153 # Return the xref if we got it; otherwise just return the plain text.
154 #
155 if xref:
156 return xref
157 else:
158 return target_text
159
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +0000160#
161# Try to replace a documentation reference of the form Documentation/... with a
162# cross reference to that page
163#
164def markup_doc_ref(docname, app, match):
165 stddom = app.env.domains['std']
166 #
167 # Go through the dance of getting an xref out of the std domain
168 #
169 target = match.group(1)
170 xref = None
171 pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
172 reftarget = target, modname = None,
173 classname = None, refexplicit = False)
174 #
175 # XXX The Latex builder will throw NoUri exceptions here,
176 # work around that by ignoring them.
177 #
178 try:
179 xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
180 target, pxref, None)
181 except NoUri:
182 xref = None
183 #
184 # Return the xref if we got it; otherwise just return the plain text.
185 #
186 if xref:
187 return xref
188 else:
189 return nodes.Text(match.group(0))
190
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600191def auto_markup(app, doctree, name):
192 #
193 # This loop could eventually be improved on. Someday maybe we
194 # want a proper tree traversal with a lot of awareness of which
195 # kinds of nodes to prune. But this works well for now.
196 #
197 # The nodes.literal test catches ``literal text``, its purpose is to
198 # avoid adding cross-references to functions that have been explicitly
199 # marked with cc:func:.
200 #
201 for para in doctree.traverse(nodes.paragraph):
202 for node in para.traverse(nodes.Text):
203 if not isinstance(node.parent, nodes.literal):
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000204 node.parent.replace(node, markup_refs(name, app, node))
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600205
206def setup(app):
207 app.connect('doctree-resolved', auto_markup)
208 return {
209 'parallel_read_safe': True,
210 'parallel_write_safe': True,
211 }