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 | # |
| 49 | # Many places in the docs refer to common system calls. It is |
| 50 | # pointless to try to cross-reference them and, as has been known |
| 51 | # to happen, somebody defining a function by these names can lead |
| 52 | # to the creation of incorrect and confusing cross references. So |
| 53 | # just don't even try with these names. |
| 54 | # |
Jonathan Neuschäfer | 11fec009 | 2019-08-12 18:07:04 +0200 | [diff] [blame] | 55 | Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap', |
Jonathan Neuschäfer | 82bf829 | 2019-08-12 18:07:05 +0200 | [diff] [blame] | 56 | 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl', |
| 57 | 'socket' ] |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 58 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 59 | def markup_refs(docname, app, node): |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 60 | t = node.astext() |
| 61 | done = 0 |
| 62 | repl = [ ] |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 63 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 64 | # 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] | 65 | # |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 66 | markup_func_sphinx2 = {RE_doc: markup_doc_ref, |
| 67 | RE_function: markup_c_ref, |
| 68 | RE_generic_type: markup_c_ref} |
| 69 | |
| 70 | markup_func_sphinx3 = {RE_doc: markup_doc_ref, |
| 71 | RE_function: markup_c_ref, |
| 72 | RE_struct: markup_c_ref, |
| 73 | RE_union: markup_c_ref, |
| 74 | RE_enum: markup_c_ref, |
| 75 | RE_typedef: markup_c_ref} |
| 76 | |
| 77 | if sphinx.version_info[0] >= 3: |
| 78 | markup_func = markup_func_sphinx3 |
| 79 | else: |
| 80 | markup_func = markup_func_sphinx2 |
| 81 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 82 | match_iterators = [regex.finditer(t) for regex in markup_func] |
| 83 | # |
| 84 | # Sort all references by the starting position in text |
| 85 | # |
| 86 | 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] | 87 | for m in sorted_matches: |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 88 | # |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 89 | # Include any text prior to match as a normal text node. |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 90 | # |
| 91 | if m.start() > done: |
| 92 | repl.append(nodes.Text(t[done:m.start()])) |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 93 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 94 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 95 | # Call the function associated with the regex that matched this text and |
| 96 | # append its return to the text |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 97 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 98 | repl.append(markup_func[m.re](docname, app, m)) |
| 99 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 100 | done = m.end() |
| 101 | if done < len(t): |
| 102 | repl.append(nodes.Text(t[done:])) |
| 103 | return repl |
| 104 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 105 | # |
| 106 | # Try to replace a C reference (function() or struct/union/enum/typedef |
| 107 | # type_name) with an appropriate cross reference. |
| 108 | # |
| 109 | def markup_c_ref(docname, app, match): |
Nícolas F. R. A. Prado | 06dc65b | 2020-10-13 23:13:11 +0000 | [diff] [blame] | 110 | class_str = {RE_function: 'c-func', |
| 111 | # Sphinx 2 only |
| 112 | RE_generic_type: 'c-type', |
| 113 | # Sphinx 3+ only |
| 114 | RE_struct: 'c-struct', |
| 115 | RE_union: 'c-union', |
| 116 | RE_enum: 'c-enum', |
| 117 | RE_typedef: 'c-type', |
| 118 | } |
| 119 | reftype_str = {RE_function: 'function', |
| 120 | # Sphinx 2 only |
| 121 | RE_generic_type: 'type', |
| 122 | # Sphinx 3+ only |
| 123 | RE_struct: 'struct', |
| 124 | RE_union: 'union', |
| 125 | RE_enum: 'enum', |
| 126 | RE_typedef: 'type', |
| 127 | } |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 128 | |
| 129 | cdom = app.env.domains['c'] |
| 130 | # |
| 131 | # Go through the dance of getting an xref out of the C domain |
| 132 | # |
| 133 | target = match.group(2) |
| 134 | target_text = nodes.Text(match.group(0)) |
| 135 | xref = None |
| 136 | if not (match.re == RE_function and target in Skipfuncs): |
| 137 | lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]]) |
| 138 | lit_text += target_text |
| 139 | pxref = addnodes.pending_xref('', refdomain = 'c', |
| 140 | reftype = reftype_str[match.re], |
| 141 | reftarget = target, modname = None, |
| 142 | classname = None) |
| 143 | # |
| 144 | # XXX The Latex builder will throw NoUri exceptions here, |
| 145 | # work around that by ignoring them. |
| 146 | # |
| 147 | try: |
| 148 | xref = cdom.resolve_xref(app.env, docname, app.builder, |
| 149 | reftype_str[match.re], target, pxref, |
| 150 | lit_text) |
| 151 | except NoUri: |
| 152 | xref = None |
| 153 | # |
| 154 | # Return the xref if we got it; otherwise just return the plain text. |
| 155 | # |
| 156 | if xref: |
| 157 | return xref |
| 158 | else: |
| 159 | return target_text |
| 160 | |
Nícolas F. R. A. Prado | d18b017 | 2020-09-11 13:34:39 +0000 | [diff] [blame] | 161 | # |
| 162 | # Try to replace a documentation reference of the form Documentation/... with a |
| 163 | # cross reference to that page |
| 164 | # |
| 165 | def markup_doc_ref(docname, app, match): |
| 166 | stddom = app.env.domains['std'] |
| 167 | # |
| 168 | # Go through the dance of getting an xref out of the std domain |
| 169 | # |
| 170 | target = match.group(1) |
| 171 | xref = None |
| 172 | pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc', |
| 173 | reftarget = target, modname = None, |
| 174 | classname = None, refexplicit = False) |
| 175 | # |
| 176 | # XXX The Latex builder will throw NoUri exceptions here, |
| 177 | # work around that by ignoring them. |
| 178 | # |
| 179 | try: |
| 180 | xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc', |
| 181 | target, pxref, None) |
| 182 | except NoUri: |
| 183 | xref = None |
| 184 | # |
| 185 | # Return the xref if we got it; otherwise just return the plain text. |
| 186 | # |
| 187 | if xref: |
| 188 | return xref |
| 189 | else: |
| 190 | return nodes.Text(match.group(0)) |
| 191 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 192 | def auto_markup(app, doctree, name): |
| 193 | # |
| 194 | # This loop could eventually be improved on. Someday maybe we |
| 195 | # want a proper tree traversal with a lot of awareness of which |
| 196 | # kinds of nodes to prune. But this works well for now. |
| 197 | # |
| 198 | # The nodes.literal test catches ``literal text``, its purpose is to |
| 199 | # avoid adding cross-references to functions that have been explicitly |
| 200 | # marked with cc:func:. |
| 201 | # |
| 202 | for para in doctree.traverse(nodes.paragraph): |
| 203 | for node in para.traverse(nodes.Text): |
| 204 | if not isinstance(node.parent, nodes.literal): |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame] | 205 | node.parent.replace(node, markup_refs(name, app, node)) |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 206 | |
| 207 | def setup(app): |
| 208 | app.connect('doctree-resolved', auto_markup) |
| 209 | return { |
| 210 | 'parallel_read_safe': True, |
| 211 | 'parallel_write_safe': True, |
| 212 | } |