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 | # |
| 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. Prado | f66e47f | 2020-10-13 23:13:17 +0000 | [diff] [blame] | 25 | RE_function = re.compile(r'\b(([a-zA-Z_]\w+)\(\))', flags=re.ASCII) |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 26 | |
| 27 | # |
| 28 | # Sphinx 2 uses the same :c:type role for struct, union, enum and typedef |
| 29 | # |
Nícolas F. R. A. Prado | f66e47f | 2020-10-13 23:13:17 +0000 | [diff] [blame] | 30 | RE_generic_type = re.compile(r'\b(struct|union|enum|typedef)\s+([a-zA-Z_]\w+)', |
| 31 | flags=re.ASCII) |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 32 | |
| 33 | # |
| 34 | # Sphinx 3 uses a different C role for each one of struct, union, enum and |
| 35 | # typedef |
| 36 | # |
| 37 | RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z_]\w+)', flags=re.ASCII) |
| 38 | RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]\w+)', flags=re.ASCII) |
| 39 | RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w+)', flags=re.ASCII) |
| 40 | RE_typedef = re.compile(r'\b(typedef)\s+([a-zA-Z_]\w+)', flags=re.ASCII) |
| 41 | |
Nícolas F. R. A. Prado | d18b017 | 2020-09-11 13:34:39 +0000 | [diff] [blame] | 42 | # |
| 43 | # Detects a reference to a documentation page of the form Documentation/... with |
| 44 | # an optional extension |
| 45 | # |
Nícolas F. R. A. Prado | f66e47f | 2020-10-13 23:13:17 +0000 | [diff] [blame] | 46 | RE_doc = re.compile(r'\bDocumentation(/[\w\-_/]+)(\.\w+)*') |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 47 | |
| 48 | # |
Nícolas F. R. A. Prado | 3050edf | 2020-10-13 23:13:23 +0000 | [diff] [blame^] | 49 | # Reserved C words that we should skip when cross-referencing |
| 50 | # |
| 51 | Skipnames = [ 'for', 'if', 'register', 'sizeof', 'struct', 'unsigned' ] |
| 52 | |
| 53 | |
| 54 | # |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 55 | # 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äfer | 11fec009 | 2019-08-12 18:07:04 +0200 | [diff] [blame] | 61 | Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap', |
Jonathan Neuschäfer | 82bf829 | 2019-08-12 18:07:05 +0200 | [diff] [blame] | 62 | 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl', |
| 63 | 'socket' ] |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 64 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 65 | def markup_refs(docname, app, node): |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 66 | t = node.astext() |
| 67 | done = 0 |
| 68 | repl = [ ] |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 69 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 70 | # 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] | 71 | # |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 72 | 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. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 88 | 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. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 93 | for m in sorted_matches: |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 94 | # |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 95 | # Include any text prior to match as a normal text node. |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 96 | # |
| 97 | if m.start() > done: |
| 98 | repl.append(nodes.Text(t[done:m.start()])) |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 99 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 100 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 101 | # Call the function associated with the regex that matched this text and |
| 102 | # append its return to the text |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 103 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 104 | repl.append(markup_func[m.re](docname, app, m)) |
| 105 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 106 | done = m.end() |
| 107 | if done < len(t): |
| 108 | repl.append(nodes.Text(t[done:])) |
| 109 | return repl |
| 110 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 111 | # |
| 112 | # Try to replace a C reference (function() or struct/union/enum/typedef |
| 113 | # type_name) with an appropriate cross reference. |
| 114 | # |
| 115 | def markup_c_ref(docname, app, match): |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 116 | 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. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 134 | |
| 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. Prado | 3050edf | 2020-10-13 23:13:23 +0000 | [diff] [blame^] | 142 | if not ((match.re == RE_function and target in Skipfuncs) |
| 143 | or (target in Skipnames)): |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 144 | 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. Prado | d18b017 | 2020-09-11 13:34:39 +0000 | [diff] [blame] | 168 | # |
| 169 | # Try to replace a documentation reference of the form Documentation/... with a |
| 170 | # cross reference to that page |
| 171 | # |
| 172 | def 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 Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 199 | def 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. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 212 | node.parent.replace(node, markup_refs(name, app, node)) |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 213 | |
| 214 | def setup(app): |
| 215 | app.connect('doctree-resolved', auto_markup) |
| 216 | return { |
| 217 | 'parallel_read_safe': True, |
| 218 | 'parallel_write_safe': True, |
| 219 | } |