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 | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 25 | RE_function = re.compile(r'(([\w_][\w\d_]+)\(\))') |
| 26 | RE_type = re.compile(r'(struct|union|enum|typedef)\s+([\w_][\w\d_]+)') |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 27 | |
| 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äfer | 11fec009 | 2019-08-12 18:07:04 +0200 | [diff] [blame] | 35 | Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap', |
Jonathan Neuschäfer | 82bf829 | 2019-08-12 18:07:05 +0200 | [diff] [blame] | 36 | 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl', |
| 37 | 'socket' ] |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 38 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 39 | def markup_refs(docname, app, node): |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 40 | t = node.astext() |
| 41 | done = 0 |
| 42 | repl = [ ] |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 43 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 44 | # 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] | 45 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 46 | 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. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 53 | for m in sorted_matches: |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 54 | # |
Nícolas F. R. A. Prado | d82b1e8 | 2020-09-03 00:58:19 +0000 | [diff] [blame] | 55 | # Include any text prior to match as a normal text node. |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 56 | # |
| 57 | if m.start() > done: |
| 58 | repl.append(nodes.Text(t[done:m.start()])) |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 59 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 60 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 61 | # Call the function associated with the regex that matched this text and |
| 62 | # append its return to the text |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 63 | # |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 64 | repl.append(markup_func[m.re](docname, app, m)) |
| 65 | |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 66 | done = m.end() |
| 67 | if done < len(t): |
| 68 | repl.append(nodes.Text(t[done:])) |
| 69 | return repl |
| 70 | |
Nícolas F. R. A. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 71 | # |
| 72 | # Try to replace a C reference (function() or struct/union/enum/typedef |
| 73 | # type_name) with an appropriate cross reference. |
| 74 | # |
| 75 | def 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 Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 111 | def 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. Prado | 1ac4cfb | 2020-09-11 13:34:33 +0000 | [diff] [blame^] | 124 | node.parent.replace(node, markup_refs(name, app, node)) |
Jonathan Corbet | d74b0d3 | 2019-04-25 07:55:07 -0600 | [diff] [blame] | 125 | |
| 126 | def setup(app): |
| 127 | app.connect('doctree-resolved', auto_markup) |
| 128 | return { |
| 129 | 'parallel_read_safe': True, |
| 130 | 'parallel_write_safe': True, |
| 131 | } |