blob: 6c8e8475eddb6e3b9afe2f2e038bfc8a22b66ed3 [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_]+)\(\))')
26RE_type = re.compile(r'(struct|union|enum|typedef)\s+([\w_][\w\d_]+)')
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060027
28#
29# Many places in the docs refer to common system calls. It is
30# pointless to try to cross-reference them and, as has been known
31# to happen, somebody defining a function by these names can lead
32# to the creation of incorrect and confusing cross references. So
33# just don't even try with these names.
34#
Jonathan Neuschäfer11fec0092019-08-12 18:07:04 +020035Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
Jonathan Neuschäfer82bf8292019-08-12 18:07:05 +020036 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
37 'socket' ]
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060038
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000039def markup_refs(docname, app, node):
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060040 t = node.astext()
41 done = 0
42 repl = [ ]
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000043 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000044 # Associate each regex with the function that will markup its matches
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000045 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000046 markup_func = {RE_type: markup_c_ref,
47 RE_function: markup_c_ref}
48 match_iterators = [regex.finditer(t) for regex in markup_func]
49 #
50 # Sort all references by the starting position in text
51 #
52 sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000053 for m in sorted_matches:
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060054 #
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000055 # Include any text prior to match as a normal text node.
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060056 #
57 if m.start() > done:
58 repl.append(nodes.Text(t[done:m.start()]))
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000059
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060060 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000061 # Call the function associated with the regex that matched this text and
62 # append its return to the text
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060063 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000064 repl.append(markup_func[m.re](docname, app, m))
65
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060066 done = m.end()
67 if done < len(t):
68 repl.append(nodes.Text(t[done:]))
69 return repl
70
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000071#
72# Try to replace a C reference (function() or struct/union/enum/typedef
73# type_name) with an appropriate cross reference.
74#
75def 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
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600111def auto_markup(app, doctree, name):
112 #
113 # This loop could eventually be improved on. Someday maybe we
114 # want a proper tree traversal with a lot of awareness of which
115 # kinds of nodes to prune. But this works well for now.
116 #
117 # The nodes.literal test catches ``literal text``, its purpose is to
118 # avoid adding cross-references to functions that have been explicitly
119 # marked with cc:func:.
120 #
121 for para in doctree.traverse(nodes.paragraph):
122 for node in para.traverse(nodes.Text):
123 if not isinstance(node.parent, nodes.literal):
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000124 node.parent.replace(node, markup_refs(name, app, node))
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600125
126def setup(app):
127 app.connect('doctree-resolved', auto_markup)
128 return {
129 'parallel_read_safe': True,
130 'parallel_write_safe': True,
131 }