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 |
| 8 | from sphinx import addnodes |
| 9 | import re |
| 10 | |
| 11 | # |
| 12 | # Regex nastiness. Of course. |
| 13 | # Try to identify "function()" that's not already marked up some |
| 14 | # other way. Sphinx doesn't like a lot of stuff right after a |
| 15 | # :c:func: block (i.e. ":c:func:`mmap()`s" flakes out), so the last |
| 16 | # bit tries to restrict matches to things that won't create trouble. |
| 17 | # |
| 18 | RE_function = re.compile(r'([\w_][\w\d_]+\(\))') |
| 19 | |
| 20 | # |
| 21 | # Many places in the docs refer to common system calls. It is |
| 22 | # pointless to try to cross-reference them and, as has been known |
| 23 | # to happen, somebody defining a function by these names can lead |
| 24 | # to the creation of incorrect and confusing cross references. So |
| 25 | # just don't even try with these names. |
| 26 | # |
| 27 | Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap' |
| 28 | 'select', 'poll', 'fork', 'execve', 'clone', 'ioctl'] |
| 29 | |
| 30 | # |
| 31 | # Find all occurrences of function() and try to replace them with |
| 32 | # appropriate cross references. |
| 33 | # |
| 34 | def markup_funcs(docname, app, node): |
| 35 | cdom = app.env.domains['c'] |
| 36 | t = node.astext() |
| 37 | done = 0 |
| 38 | repl = [ ] |
| 39 | for m in RE_function.finditer(t): |
| 40 | # |
| 41 | # Include any text prior to function() as a normal text node. |
| 42 | # |
| 43 | if m.start() > done: |
| 44 | repl.append(nodes.Text(t[done:m.start()])) |
| 45 | # |
| 46 | # Go through the dance of getting an xref out of the C domain |
| 47 | # |
| 48 | target = m.group(1)[:-2] |
| 49 | target_text = nodes.Text(target + '()') |
| 50 | xref = None |
| 51 | if target not in Skipfuncs: |
| 52 | lit_text = nodes.literal(classes=['xref', 'c', 'c-func']) |
| 53 | lit_text += target_text |
| 54 | pxref = addnodes.pending_xref('', refdomain = 'c', |
| 55 | reftype = 'function', |
| 56 | reftarget = target, modname = None, |
| 57 | classname = None) |
| 58 | xref = cdom.resolve_xref(app.env, docname, app.builder, |
| 59 | 'function', target, pxref, lit_text) |
| 60 | # |
| 61 | # Toss the xref into the list if we got it; otherwise just put |
| 62 | # the function text. |
| 63 | # |
| 64 | if xref: |
| 65 | repl.append(xref) |
| 66 | else: |
| 67 | repl.append(target_text) |
| 68 | done = m.end() |
| 69 | if done < len(t): |
| 70 | repl.append(nodes.Text(t[done:])) |
| 71 | return repl |
| 72 | |
| 73 | def auto_markup(app, doctree, name): |
| 74 | # |
| 75 | # This loop could eventually be improved on. Someday maybe we |
| 76 | # want a proper tree traversal with a lot of awareness of which |
| 77 | # kinds of nodes to prune. But this works well for now. |
| 78 | # |
| 79 | # The nodes.literal test catches ``literal text``, its purpose is to |
| 80 | # avoid adding cross-references to functions that have been explicitly |
| 81 | # marked with cc:func:. |
| 82 | # |
| 83 | for para in doctree.traverse(nodes.paragraph): |
| 84 | for node in para.traverse(nodes.Text): |
| 85 | if not isinstance(node.parent, nodes.literal): |
| 86 | node.parent.replace(node, markup_funcs(name, app, node)) |
| 87 | |
| 88 | def setup(app): |
| 89 | app.connect('doctree-resolved', auto_markup) |
| 90 | return { |
| 91 | 'parallel_read_safe': True, |
| 92 | 'parallel_write_safe': True, |
| 93 | } |