Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 1 | # 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 | # |
| 7 | from docutils import nodes |
Jonathan Corbet | bcac386 | 2020-01-22 16:06:28 -0700 | [diff] [blame] | 8 | import sphinx |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 9 | from sphinx import addnodes |
Jonathan Corbet | bcac386 | 2020-01-22 16:06:28 -0700 | [diff] [blame] | 10 | if sphinx.version_info[0] < 2 or \ |
| 11 | sphinx.version_info[0] == 2 and sphinx.version_info[1] < 1: |
| 12 | from sphinx.environment import NoUri |
| 13 | else: |
| 14 | from sphinx.errors import NoUri |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 15 | import re |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 16 | from itertools import chain |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 17 | |
| 18 | # |
Jonathan Corbet | 4f3e690 | 2020-10-30 09:35:39 -0600 | [diff] [blame] | 19 | # Python 2 lacks re.ASCII... |
| 20 | # |
| 21 | try: |
| 22 | ascii_p3 = re.ASCII |
| 23 | except AttributeError: |
| 24 | ascii_p3 = 0 |
| 25 | |
| 26 | # |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 27 | # Regex nastiness. Of course. |
| 28 | # Try to identify "function()" that's not already marked up some |
| 29 | # other way. Sphinx doesn't like a lot of stuff right after a |
| 30 | # :c:func: block (i.e. ":c:func:`mmap()`s" flakes out), so the last |
| 31 | # bit tries to restrict matches to things that won't create trouble. |
| 32 | # |
Jonathan Corbet | 4f3e690 | 2020-10-30 09:35:39 -0600 | [diff] [blame] | 33 | RE_function = re.compile(r'\b(([a-zA-Z_]\w+)\(\))', flags=ascii_p3) |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 34 | |
| 35 | # |
| 36 | # Sphinx 2 uses the same :c:type role for struct, union, enum and typedef |
| 37 | # |
Nícolas F. R. A. Prado | f66e47f | 2020-10-13 23:13:17 +0000 | [diff] [blame] | 38 | RE_generic_type = re.compile(r'\b(struct|union|enum|typedef)\s+([a-zA-Z_]\w+)', |
Jonathan Corbet | 4f3e690 | 2020-10-30 09:35:39 -0600 | [diff] [blame] | 39 | flags=ascii_p3) |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 40 | |
| 41 | # |
| 42 | # Sphinx 3 uses a different C role for each one of struct, union, enum and |
| 43 | # typedef |
| 44 | # |
Jonathan Corbet | 4f3e690 | 2020-10-30 09:35:39 -0600 | [diff] [blame] | 45 | RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z_]\w+)', flags=ascii_p3) |
| 46 | RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]\w+)', flags=ascii_p3) |
| 47 | RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w+)', flags=ascii_p3) |
| 48 | RE_typedef = re.compile(r'\b(typedef)\s+([a-zA-Z_]\w+)', flags=ascii_p3) |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 49 | |
Nícolas F. R. A. Prado | d18b017 | 2020-09-11 13:34:39 +0000 | [diff] [blame] | 50 | # |
| 51 | # Detects a reference to a documentation page of the form Documentation/... with |
| 52 | # an optional extension |
| 53 | # |
Nícolas F. R. A. Prado | f66e47f | 2020-10-13 23:13:17 +0000 | [diff] [blame] | 54 | RE_doc = re.compile(r'\bDocumentation(/[\w\-_/]+)(\.\w+)*') |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 55 | |
| 56 | # |
Nícolas F. R. A. Prado | 3050edf | 2020-10-13 23:13:23 +0000 | [diff] [blame] | 57 | # Reserved C words that we should skip when cross-referencing |
| 58 | # |
| 59 | Skipnames = [ 'for', 'if', 'register', 'sizeof', 'struct', 'unsigned' ] |
| 60 | |
| 61 | |
| 62 | # |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 63 | # Many places in the docs refer to common system calls. It is |
| 64 | # pointless to try to cross-reference them and, as has been known |
| 65 | # to happen, somebody defining a function by these names can lead |
| 66 | # to the creation of incorrect and confusing cross references. So |
| 67 | # just don't even try with these names. |
| 68 | # |
Jonathan Neuschäfer | 11fec009 | 2019-08-12 18:07:04 +0200 | [diff] [blame] | 69 | Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap', |
Jonathan Neuschäfer | 82bf829 | 2019-08-12 18:07:05 +0200 | [diff] [blame] | 70 | 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl', |
| 71 | 'socket' ] |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 72 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 73 | def markup_refs(docname, app, node): |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 74 | t = node.astext() |
| 75 | done = 0 |
| 76 | repl = [ ] |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 77 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 78 | # Associate each regex with the function that will markup its matches |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 79 | # |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 80 | markup_func_sphinx2 = {RE_doc: markup_doc_ref, |
| 81 | RE_function: markup_c_ref, |
| 82 | RE_generic_type: markup_c_ref} |
| 83 | |
| 84 | markup_func_sphinx3 = {RE_doc: markup_doc_ref, |
Nícolas F. R. A. Prado | c51d9b0 | 2020-10-13 23:13:34 +0000 | [diff] [blame] | 85 | RE_function: markup_func_ref_sphinx3, |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 86 | RE_struct: markup_c_ref, |
| 87 | RE_union: markup_c_ref, |
| 88 | RE_enum: markup_c_ref, |
| 89 | RE_typedef: markup_c_ref} |
| 90 | |
| 91 | if sphinx.version_info[0] >= 3: |
| 92 | markup_func = markup_func_sphinx3 |
| 93 | else: |
| 94 | markup_func = markup_func_sphinx2 |
| 95 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 96 | match_iterators = [regex.finditer(t) for regex in markup_func] |
| 97 | # |
| 98 | # Sort all references by the starting position in text |
| 99 | # |
| 100 | sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start()) |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 101 | for m in sorted_matches: |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 102 | # |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 103 | # Include any text prior to match as a normal text node. |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 104 | # |
| 105 | if m.start() > done: |
| 106 | repl.append(nodes.Text(t[done:m.start()])) |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 107 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 108 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 109 | # Call the function associated with the regex that matched this text and |
| 110 | # append its return to the text |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 111 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 112 | repl.append(markup_func[m.re](docname, app, m)) |
| 113 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 114 | done = m.end() |
| 115 | if done < len(t): |
| 116 | repl.append(nodes.Text(t[done:])) |
| 117 | return repl |
| 118 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 119 | # |
Nícolas F. R. A. Prado | c51d9b0 | 2020-10-13 23:13:34 +0000 | [diff] [blame] | 120 | # In sphinx3 we can cross-reference to C macro and function, each one with its |
| 121 | # own C role, but both match the same regex, so we try both. |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 122 | # |
Nícolas F. R. A. Prado | c51d9b0 | 2020-10-13 23:13:34 +0000 | [diff] [blame] | 123 | def markup_func_ref_sphinx3(docname, app, match): |
| 124 | class_str = ['c-func', 'c-macro'] |
| 125 | reftype_str = ['function', 'macro'] |
| 126 | |
| 127 | cdom = app.env.domains['c'] |
| 128 | # |
| 129 | # Go through the dance of getting an xref out of the C domain |
| 130 | # |
| 131 | target = match.group(2) |
| 132 | target_text = nodes.Text(match.group(0)) |
| 133 | xref = None |
| 134 | if not (target in Skipfuncs or target in Skipnames): |
| 135 | for class_s, reftype_s in zip(class_str, reftype_str): |
| 136 | lit_text = nodes.literal(classes=['xref', 'c', class_s]) |
| 137 | lit_text += target_text |
| 138 | pxref = addnodes.pending_xref('', refdomain = 'c', |
| 139 | reftype = reftype_s, |
| 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_s, target, pxref, |
| 149 | lit_text) |
| 150 | except NoUri: |
| 151 | xref = None |
| 152 | |
| 153 | if xref: |
| 154 | return xref |
| 155 | |
| 156 | return target_text |
| 157 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 158 | def markup_c_ref(docname, app, match): |
Nícolas F. R. A. Prado | c51d9b0 | 2020-10-13 23:13:34 +0000 | [diff] [blame] | 159 | class_str = {# Sphinx 2 only |
| 160 | RE_function: 'c-func', |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 161 | RE_generic_type: 'c-type', |
| 162 | # Sphinx 3+ only |
| 163 | RE_struct: 'c-struct', |
| 164 | RE_union: 'c-union', |
| 165 | RE_enum: 'c-enum', |
| 166 | RE_typedef: 'c-type', |
| 167 | } |
Nícolas F. R. A. Prado | c51d9b0 | 2020-10-13 23:13:34 +0000 | [diff] [blame] | 168 | reftype_str = {# Sphinx 2 only |
| 169 | RE_function: 'function', |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 170 | RE_generic_type: 'type', |
| 171 | # Sphinx 3+ only |
| 172 | RE_struct: 'struct', |
| 173 | RE_union: 'union', |
| 174 | RE_enum: 'enum', |
| 175 | RE_typedef: 'type', |
| 176 | } |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 177 | |
| 178 | cdom = app.env.domains['c'] |
| 179 | # |
| 180 | # Go through the dance of getting an xref out of the C domain |
| 181 | # |
| 182 | target = match.group(2) |
| 183 | target_text = nodes.Text(match.group(0)) |
| 184 | xref = None |
Nícolas F. R. A. Prado | 3050edf | 2020-10-13 23:13:23 +0000 | [diff] [blame] | 185 | if not ((match.re == RE_function and target in Skipfuncs) |
| 186 | or (target in Skipnames)): |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 187 | lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]]) |
| 188 | lit_text += target_text |
| 189 | pxref = addnodes.pending_xref('', refdomain = 'c', |
| 190 | reftype = reftype_str[match.re], |
| 191 | reftarget = target, modname = None, |
| 192 | classname = None) |
| 193 | # |
| 194 | # XXX The Latex builder will throw NoUri exceptions here, |
| 195 | # work around that by ignoring them. |
| 196 | # |
| 197 | try: |
| 198 | xref = cdom.resolve_xref(app.env, docname, app.builder, |
| 199 | reftype_str[match.re], target, pxref, |
| 200 | lit_text) |
| 201 | except NoUri: |
| 202 | xref = None |
| 203 | # |
| 204 | # Return the xref if we got it; otherwise just return the plain text. |
| 205 | # |
| 206 | if xref: |
| 207 | return xref |
| 208 | else: |
| 209 | return target_text |
| 210 | |
Nícolas F. R. A. Prado | d18b017 | 2020-09-11 13:34:39 +0000 | [diff] [blame] | 211 | # |
| 212 | # Try to replace a documentation reference of the form Documentation/... with a |
| 213 | # cross reference to that page |
| 214 | # |
| 215 | def markup_doc_ref(docname, app, match): |
| 216 | stddom = app.env.domains['std'] |
| 217 | # |
| 218 | # Go through the dance of getting an xref out of the std domain |
| 219 | # |
| 220 | target = match.group(1) |
| 221 | xref = None |
| 222 | pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc', |
| 223 | reftarget = target, modname = None, |
| 224 | classname = None, refexplicit = False) |
| 225 | # |
| 226 | # XXX The Latex builder will throw NoUri exceptions here, |
| 227 | # work around that by ignoring them. |
| 228 | # |
| 229 | try: |
| 230 | xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc', |
| 231 | target, pxref, None) |
| 232 | except NoUri: |
| 233 | xref = None |
| 234 | # |
| 235 | # Return the xref if we got it; otherwise just return the plain text. |
| 236 | # |
| 237 | if xref: |
| 238 | return xref |
| 239 | else: |
| 240 | return nodes.Text(match.group(0)) |
| 241 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 242 | def auto_markup(app, doctree, name): |
| 243 | # |
| 244 | # This loop could eventually be improved on. Someday maybe we |
| 245 | # want a proper tree traversal with a lot of awareness of which |
| 246 | # kinds of nodes to prune. But this works well for now. |
| 247 | # |
| 248 | # The nodes.literal test catches ``literal text``, its purpose is to |
| 249 | # avoid adding cross-references to functions that have been explicitly |
| 250 | # marked with cc:func:. |
| 251 | # |
| 252 | for para in doctree.traverse(nodes.paragraph): |
| 253 | for node in para.traverse(nodes.Text): |
| 254 | if not isinstance(node.parent, nodes.literal): |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 255 | node.parent.replace(node, markup_refs(name, app, node)) |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 256 | |
| 257 | def setup(app): |
| 258 | app.connect('doctree-resolved', auto_markup) |
| 259 | return { |
| 260 | 'parallel_read_safe': True, |
| 261 | 'parallel_write_safe': True, |
| 262 | } |