blob: a1b0f554cd82e35e7d07d5890e10d44bda791025 [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_]+)')
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +000027#
28# Detects a reference to a documentation page of the form Documentation/... with
29# an optional extension
30#
31RE_doc = re.compile(r'Documentation(/[\w\-_/]+)(\.\w+)*')
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060032
33#
34# Many places in the docs refer to common system calls. It is
35# pointless to try to cross-reference them and, as has been known
36# to happen, somebody defining a function by these names can lead
37# to the creation of incorrect and confusing cross references. So
38# just don't even try with these names.
39#
Jonathan Neuschäfer11fec0092019-08-12 18:07:04 +020040Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
Jonathan Neuschäfer82bf8292019-08-12 18:07:05 +020041 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
42 'socket' ]
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060043
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000044def markup_refs(docname, app, node):
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060045 t = node.astext()
46 done = 0
47 repl = [ ]
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000048 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000049 # Associate each regex with the function that will markup its matches
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000050 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000051 markup_func = {RE_type: markup_c_ref,
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +000052 RE_function: markup_c_ref,
53 RE_doc: markup_doc_ref}
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000054 match_iterators = [regex.finditer(t) for regex in markup_func]
55 #
56 # Sort all references by the starting position in text
57 #
58 sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000059 for m in sorted_matches:
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060060 #
Nícolas F. R. A. Pradod82b1e82020-09-03 00:58:19 +000061 # Include any text prior to match as a normal text node.
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060062 #
63 if m.start() > done:
64 repl.append(nodes.Text(t[done:m.start()]))
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000065
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060066 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000067 # Call the function associated with the regex that matched this text and
68 # append its return to the text
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060069 #
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000070 repl.append(markup_func[m.re](docname, app, m))
71
Jonathan Corbetd74b0d32019-04-25 07:55:07 -060072 done = m.end()
73 if done < len(t):
74 repl.append(nodes.Text(t[done:]))
75 return repl
76
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +000077#
78# Try to replace a C reference (function() or struct/union/enum/typedef
79# type_name) with an appropriate cross reference.
80#
81def markup_c_ref(docname, app, match):
82 class_str = {RE_function: 'c-func', RE_type: 'c-type'}
83 reftype_str = {RE_function: 'function', RE_type: 'type'}
84
85 cdom = app.env.domains['c']
86 #
87 # Go through the dance of getting an xref out of the C domain
88 #
89 target = match.group(2)
90 target_text = nodes.Text(match.group(0))
91 xref = None
92 if not (match.re == RE_function and target in Skipfuncs):
93 lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
94 lit_text += target_text
95 pxref = addnodes.pending_xref('', refdomain = 'c',
96 reftype = reftype_str[match.re],
97 reftarget = target, modname = None,
98 classname = None)
99 #
100 # XXX The Latex builder will throw NoUri exceptions here,
101 # work around that by ignoring them.
102 #
103 try:
104 xref = cdom.resolve_xref(app.env, docname, app.builder,
105 reftype_str[match.re], target, pxref,
106 lit_text)
107 except NoUri:
108 xref = None
109 #
110 # Return the xref if we got it; otherwise just return the plain text.
111 #
112 if xref:
113 return xref
114 else:
115 return target_text
116
Nícolas F. R. A. Pradod18b0172020-09-11 13:34:39 +0000117#
118# Try to replace a documentation reference of the form Documentation/... with a
119# cross reference to that page
120#
121def markup_doc_ref(docname, app, match):
122 stddom = app.env.domains['std']
123 #
124 # Go through the dance of getting an xref out of the std domain
125 #
126 target = match.group(1)
127 xref = None
128 pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
129 reftarget = target, modname = None,
130 classname = None, refexplicit = False)
131 #
132 # XXX The Latex builder will throw NoUri exceptions here,
133 # work around that by ignoring them.
134 #
135 try:
136 xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
137 target, pxref, None)
138 except NoUri:
139 xref = None
140 #
141 # Return the xref if we got it; otherwise just return the plain text.
142 #
143 if xref:
144 return xref
145 else:
146 return nodes.Text(match.group(0))
147
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600148def auto_markup(app, doctree, name):
149 #
150 # This loop could eventually be improved on. Someday maybe we
151 # want a proper tree traversal with a lot of awareness of which
152 # kinds of nodes to prune. But this works well for now.
153 #
154 # The nodes.literal test catches ``literal text``, its purpose is to
155 # avoid adding cross-references to functions that have been explicitly
156 # marked with cc:func:.
157 #
158 for para in doctree.traverse(nodes.paragraph):
159 for node in para.traverse(nodes.Text):
160 if not isinstance(node.parent, nodes.literal):
Nícolas F. R. A. Prado1ac4cfb2020-09-11 13:34:33 +0000161 node.parent.replace(node, markup_refs(name, app, node))
Jonathan Corbetd74b0d32019-04-25 07:55:07 -0600162
163def setup(app):
164 app.connect('doctree-resolved', auto_markup)
165 return {
166 'parallel_read_safe': True,
167 'parallel_write_safe': True,
168 }