~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/sphinx/automarkup.py

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/sphinx/automarkup.py (Version linux-6.12-rc7) and /Documentation/sphinx/automarkup.py (Version linux-5.12.19)


  1 # SPDX-License-Identifier: GPL-2.0                  1 # SPDX-License-Identifier: GPL-2.0
  2 # Copyright 2019 Jonathan Corbet <corbet@lwn.ne      2 # Copyright 2019 Jonathan Corbet <corbet@lwn.net>
  3 #                                                   3 #
  4 # Apply kernel-specific tweaks after the initi      4 # Apply kernel-specific tweaks after the initial document processing
  5 # has been done.                                    5 # has been done.
  6 #                                                   6 #
  7 from docutils import nodes                          7 from docutils import nodes
  8 import sphinx                                       8 import sphinx
  9 from sphinx import addnodes                         9 from sphinx import addnodes
 10 from sphinx.errors import NoUri                !!  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
 11 import re                                          15 import re
 12 from itertools import chain                        16 from itertools import chain
 13                                                    17 
 14 #                                                  18 #
 15 # Python 2 lacks re.ASCII...                       19 # Python 2 lacks re.ASCII...
 16 #                                                  20 #
 17 try:                                               21 try:
 18     ascii_p3 = re.ASCII                            22     ascii_p3 = re.ASCII
 19 except AttributeError:                             23 except AttributeError:
 20     ascii_p3 = 0                                   24     ascii_p3 = 0
 21                                                    25 
 22 #                                                  26 #
 23 # Regex nastiness.  Of course.                     27 # Regex nastiness.  Of course.
 24 # Try to identify "function()" that's not alre     28 # Try to identify "function()" that's not already marked up some
 25 # other way.  Sphinx doesn't like a lot of stu     29 # other way.  Sphinx doesn't like a lot of stuff right after a
 26 # :c:func: block (i.e. ":c:func:`mmap()`s" fla     30 # :c:func: block (i.e. ":c:func:`mmap()`s" flakes out), so the last
 27 # bit tries to restrict matches to things that     31 # bit tries to restrict matches to things that won't create trouble.
 28 #                                                  32 #
 29 RE_function = re.compile(r'\b(([a-zA-Z_]\w+)\(     33 RE_function = re.compile(r'\b(([a-zA-Z_]\w+)\(\))', flags=ascii_p3)
 30                                                    34 
 31 #                                                  35 #
 32 # Sphinx 2 uses the same :c:type role for stru     36 # Sphinx 2 uses the same :c:type role for struct, union, enum and typedef
 33 #                                                  37 #
 34 RE_generic_type = re.compile(r'\b(struct|union     38 RE_generic_type = re.compile(r'\b(struct|union|enum|typedef)\s+([a-zA-Z_]\w+)',
 35                              flags=ascii_p3)       39                              flags=ascii_p3)
 36                                                    40 
 37 #                                                  41 #
 38 # Sphinx 3 uses a different C role for each on     42 # Sphinx 3 uses a different C role for each one of struct, union, enum and
 39 # typedef                                          43 # typedef
 40 #                                                  44 #
 41 RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z     45 RE_struct = re.compile(r'\b(struct)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
 42 RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]     46 RE_union = re.compile(r'\b(union)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
 43 RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w     47 RE_enum = re.compile(r'\b(enum)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
 44 RE_typedef = re.compile(r'\b(typedef)\s+([a-zA     48 RE_typedef = re.compile(r'\b(typedef)\s+([a-zA-Z_]\w+)', flags=ascii_p3)
 45                                                    49 
 46 #                                                  50 #
 47 # Detects a reference to a documentation page      51 # Detects a reference to a documentation page of the form Documentation/... with
 48 # an optional extension                            52 # an optional extension
 49 #                                                  53 #
 50 RE_doc = re.compile(r'(\bDocumentation/)?((\.\     54 RE_doc = re.compile(r'(\bDocumentation/)?((\.\./)*[\w\-/]+)\.(rst|txt)')
 51                                                    55 
 52 RE_namespace = re.compile(r'^\s*..\s*c:namespa     56 RE_namespace = re.compile(r'^\s*..\s*c:namespace::\s*(\S+)\s*$')
 53                                                    57 
 54 #                                                  58 #
 55 # Reserved C words that we should skip when cr     59 # Reserved C words that we should skip when cross-referencing
 56 #                                                  60 #
 57 Skipnames = [ 'for', 'if', 'register', 'sizeof     61 Skipnames = [ 'for', 'if', 'register', 'sizeof', 'struct', 'unsigned' ]
 58                                                    62 
 59                                                    63 
 60 #                                                  64 #
 61 # Many places in the docs refer to common syst     65 # Many places in the docs refer to common system calls.  It is
 62 # pointless to try to cross-reference them and     66 # pointless to try to cross-reference them and, as has been known
 63 # to happen, somebody defining a function by t     67 # to happen, somebody defining a function by these names can lead
 64 # to the creation of incorrect and confusing c     68 # to the creation of incorrect and confusing cross references.  So
 65 # just don't even try with these names.            69 # just don't even try with these names.
 66 #                                                  70 #
 67 Skipfuncs = [ 'open', 'close', 'read', 'write'     71 Skipfuncs = [ 'open', 'close', 'read', 'write', 'fcntl', 'mmap',
 68               'select', 'poll', 'fork', 'execv     72               'select', 'poll', 'fork', 'execve', 'clone', 'ioctl',
 69               'socket' ]                           73               'socket' ]
 70                                                    74 
 71 c_namespace = ''                                   75 c_namespace = ''
 72                                                    76 
 73 #                                              << 
 74 # Detect references to commits.                << 
 75 #                                              << 
 76 RE_git = re.compile(r'commit\s+(?P<rev>[0-9a-f << 
 77     flags=re.IGNORECASE | re.DOTALL)           << 
 78                                                << 
 79 def markup_refs(docname, app, node):               77 def markup_refs(docname, app, node):
 80     t = node.astext()                              78     t = node.astext()
 81     done = 0                                       79     done = 0
 82     repl = [ ]                                     80     repl = [ ]
 83     #                                              81     #
 84     # Associate each regex with the function t     82     # Associate each regex with the function that will markup its matches
 85     #                                              83     #
 86     markup_func_sphinx2 = {RE_doc: markup_doc_     84     markup_func_sphinx2 = {RE_doc: markup_doc_ref,
 87                            RE_function: markup     85                            RE_function: markup_c_ref,
 88                            RE_generic_type: ma     86                            RE_generic_type: markup_c_ref}
 89                                                    87 
 90     markup_func_sphinx3 = {RE_doc: markup_doc_     88     markup_func_sphinx3 = {RE_doc: markup_doc_ref,
 91                            RE_function: markup     89                            RE_function: markup_func_ref_sphinx3,
 92                            RE_struct: markup_c     90                            RE_struct: markup_c_ref,
 93                            RE_union: markup_c_     91                            RE_union: markup_c_ref,
 94                            RE_enum: markup_c_r     92                            RE_enum: markup_c_ref,
 95                            RE_typedef: markup_ !!  93                            RE_typedef: markup_c_ref}
 96                            RE_git: markup_git} << 
 97                                                    94 
 98     if sphinx.version_info[0] >= 3:                95     if sphinx.version_info[0] >= 3:
 99         markup_func = markup_func_sphinx3          96         markup_func = markup_func_sphinx3
100     else:                                          97     else:
101         markup_func = markup_func_sphinx2          98         markup_func = markup_func_sphinx2
102                                                    99 
103     match_iterators = [regex.finditer(t) for r    100     match_iterators = [regex.finditer(t) for regex in markup_func]
104     #                                             101     #
105     # Sort all references by the starting posi    102     # Sort all references by the starting position in text
106     #                                             103     #
107     sorted_matches = sorted(chain(*match_itera    104     sorted_matches = sorted(chain(*match_iterators), key=lambda m: m.start())
108     for m in sorted_matches:                      105     for m in sorted_matches:
109         #                                         106         #
110         # Include any text prior to match as a    107         # Include any text prior to match as a normal text node.
111         #                                         108         #
112         if m.start() > done:                      109         if m.start() > done:
113             repl.append(nodes.Text(t[done:m.st    110             repl.append(nodes.Text(t[done:m.start()]))
114                                                   111 
115         #                                         112         #
116         # Call the function associated with th    113         # Call the function associated with the regex that matched this text and
117         # append its return to the text           114         # append its return to the text
118         #                                         115         #
119         repl.append(markup_func[m.re](docname,    116         repl.append(markup_func[m.re](docname, app, m))
120                                                   117 
121         done = m.end()                            118         done = m.end()
122     if done < len(t):                             119     if done < len(t):
123         repl.append(nodes.Text(t[done:]))         120         repl.append(nodes.Text(t[done:]))
124     return repl                                   121     return repl
125                                                   122 
126 #                                                 123 #
127 # Keep track of cross-reference lookups that f << 
128 # do them again.                               << 
129 #                                              << 
130 failed_lookups = { }                           << 
131 def failure_seen(target):                      << 
132     return (target) in failed_lookups          << 
133 def note_failure(target):                      << 
134     failed_lookups[target] = True              << 
135                                                << 
136 #                                              << 
137 # In sphinx3 we can cross-reference to C macro    124 # In sphinx3 we can cross-reference to C macro and function, each one with its
138 # own C role, but both match the same regex, s    125 # own C role, but both match the same regex, so we try both.
139 #                                                 126 #
140 def markup_func_ref_sphinx3(docname, app, matc    127 def markup_func_ref_sphinx3(docname, app, match):
                                                   >> 128     class_str = ['c-func', 'c-macro']
                                                   >> 129     reftype_str = ['function', 'macro']
                                                   >> 130 
141     cdom = app.env.domains['c']                   131     cdom = app.env.domains['c']
142     #                                             132     #
143     # Go through the dance of getting an xref     133     # Go through the dance of getting an xref out of the C domain
144     #                                             134     #
145     base_target = match.group(2)                  135     base_target = match.group(2)
146     target_text = nodes.Text(match.group(0))      136     target_text = nodes.Text(match.group(0))
147     xref = None                                   137     xref = None
148     possible_targets = [base_target]              138     possible_targets = [base_target]
149     # Check if this document has a namespace,     139     # Check if this document has a namespace, and if so, try
150     # cross-referencing inside it first.          140     # cross-referencing inside it first.
151     if c_namespace:                               141     if c_namespace:
152         possible_targets.insert(0, c_namespace    142         possible_targets.insert(0, c_namespace + "." + base_target)
153                                                   143 
154     if base_target not in Skipnames:              144     if base_target not in Skipnames:
155         for target in possible_targets:           145         for target in possible_targets:
156             if (target not in Skipfuncs) and n !! 146             if target not in Skipfuncs:
157                 lit_text = nodes.literal(class !! 147                 for class_s, reftype_s in zip(class_str, reftype_str):
158                 lit_text += target_text        !! 148                     lit_text = nodes.literal(classes=['xref', 'c', class_s])
159                 pxref = addnodes.pending_xref( !! 149                     lit_text += target_text
160                                                !! 150                     pxref = addnodes.pending_xref('', refdomain = 'c',
161                                                !! 151                                                   reftype = reftype_s,
162                                                !! 152                                                   reftarget = target, modname = None,
163                                                !! 153                                                   classname = None)
164                 #                              !! 154                     #
165                 # XXX The Latex builder will t !! 155                     # XXX The Latex builder will throw NoUri exceptions here,
166                 # work around that by ignoring !! 156                     # work around that by ignoring them.
167                 #                              !! 157                     #
168                 try:                           !! 158                     try:
169                     xref = cdom.resolve_xref(a !! 159                         xref = cdom.resolve_xref(app.env, docname, app.builder,
170                                              ' !! 160                                                  reftype_s, target, pxref,
171                                              l !! 161                                                  lit_text)
172                 except NoUri:                  !! 162                     except NoUri:
173                     xref = None                !! 163                         xref = None
174                                                   164 
175                 if xref:                       !! 165                     if xref:
176                     return xref                !! 166                         return xref
177                 note_failure(target)           << 
178                                                   167 
179     return target_text                            168     return target_text
180                                                   169 
181 def markup_c_ref(docname, app, match):            170 def markup_c_ref(docname, app, match):
182     class_str = {# Sphinx 2 only                  171     class_str = {# Sphinx 2 only
183                  RE_function: 'c-func',           172                  RE_function: 'c-func',
184                  RE_generic_type: 'c-type',       173                  RE_generic_type: 'c-type',
185                  # Sphinx 3+ only                 174                  # Sphinx 3+ only
186                  RE_struct: 'c-struct',           175                  RE_struct: 'c-struct',
187                  RE_union: 'c-union',             176                  RE_union: 'c-union',
188                  RE_enum: 'c-enum',               177                  RE_enum: 'c-enum',
189                  RE_typedef: 'c-type',            178                  RE_typedef: 'c-type',
190                  }                                179                  }
191     reftype_str = {# Sphinx 2 only                180     reftype_str = {# Sphinx 2 only
192                    RE_function: 'function',       181                    RE_function: 'function',
193                    RE_generic_type: 'type',       182                    RE_generic_type: 'type',
194                    # Sphinx 3+ only               183                    # Sphinx 3+ only
195                    RE_struct: 'struct',           184                    RE_struct: 'struct',
196                    RE_union: 'union',             185                    RE_union: 'union',
197                    RE_enum: 'enum',               186                    RE_enum: 'enum',
198                    RE_typedef: 'type',            187                    RE_typedef: 'type',
199                    }                              188                    }
200                                                   189 
201     cdom = app.env.domains['c']                   190     cdom = app.env.domains['c']
202     #                                             191     #
203     # Go through the dance of getting an xref     192     # Go through the dance of getting an xref out of the C domain
204     #                                             193     #
205     base_target = match.group(2)                  194     base_target = match.group(2)
206     target_text = nodes.Text(match.group(0))      195     target_text = nodes.Text(match.group(0))
207     xref = None                                   196     xref = None
208     possible_targets = [base_target]              197     possible_targets = [base_target]
209     # Check if this document has a namespace,     198     # Check if this document has a namespace, and if so, try
210     # cross-referencing inside it first.          199     # cross-referencing inside it first.
211     if c_namespace:                               200     if c_namespace:
212         possible_targets.insert(0, c_namespace    201         possible_targets.insert(0, c_namespace + "." + base_target)
213                                                   202 
214     if base_target not in Skipnames:              203     if base_target not in Skipnames:
215         for target in possible_targets:           204         for target in possible_targets:
216             if not (match.re == RE_function an    205             if not (match.re == RE_function and target in Skipfuncs):
217                 lit_text = nodes.literal(class    206                 lit_text = nodes.literal(classes=['xref', 'c', class_str[match.re]])
218                 lit_text += target_text           207                 lit_text += target_text
219                 pxref = addnodes.pending_xref(    208                 pxref = addnodes.pending_xref('', refdomain = 'c',
220                                                   209                                               reftype = reftype_str[match.re],
221                                                   210                                               reftarget = target, modname = None,
222                                                   211                                               classname = None)
223                 #                                 212                 #
224                 # XXX The Latex builder will t    213                 # XXX The Latex builder will throw NoUri exceptions here,
225                 # work around that by ignoring    214                 # work around that by ignoring them.
226                 #                                 215                 #
227                 try:                              216                 try:
228                     xref = cdom.resolve_xref(a    217                     xref = cdom.resolve_xref(app.env, docname, app.builder,
229                                              r    218                                              reftype_str[match.re], target, pxref,
230                                              l    219                                              lit_text)
231                 except NoUri:                     220                 except NoUri:
232                     xref = None                   221                     xref = None
233                                                   222 
234                 if xref:                          223                 if xref:
235                     return xref                   224                     return xref
236                                                   225 
237     return target_text                            226     return target_text
238                                                   227 
239 #                                                 228 #
240 # Try to replace a documentation reference of     229 # Try to replace a documentation reference of the form Documentation/... with a
241 # cross reference to that page                    230 # cross reference to that page
242 #                                                 231 #
243 def markup_doc_ref(docname, app, match):          232 def markup_doc_ref(docname, app, match):
244     stddom = app.env.domains['std']               233     stddom = app.env.domains['std']
245     #                                             234     #
246     # Go through the dance of getting an xref     235     # Go through the dance of getting an xref out of the std domain
247     #                                             236     #
248     absolute = match.group(1)                     237     absolute = match.group(1)
249     target = match.group(2)                       238     target = match.group(2)
250     if absolute:                                  239     if absolute:
251        target = "/" + target                      240        target = "/" + target
252     xref = None                                   241     xref = None
253     pxref = addnodes.pending_xref('', refdomai    242     pxref = addnodes.pending_xref('', refdomain = 'std', reftype = 'doc',
254                                   reftarget =     243                                   reftarget = target, modname = None,
255                                   classname =     244                                   classname = None, refexplicit = False)
256     #                                             245     #
257     # XXX The Latex builder will throw NoUri e    246     # XXX The Latex builder will throw NoUri exceptions here,
258     # work around that by ignoring them.          247     # work around that by ignoring them.
259     #                                             248     #
260     try:                                          249     try:
261         xref = stddom.resolve_xref(app.env, do    250         xref = stddom.resolve_xref(app.env, docname, app.builder, 'doc',
262                                    target, pxr    251                                    target, pxref, None)
263     except NoUri:                                 252     except NoUri:
264         xref = None                               253         xref = None
265     #                                             254     #
266     # Return the xref if we got it; otherwise     255     # Return the xref if we got it; otherwise just return the plain text.
267     #                                             256     #
268     if xref:                                      257     if xref:
269         return xref                               258         return xref
270     else:                                         259     else:
271         return nodes.Text(match.group(0))         260         return nodes.Text(match.group(0))
272                                                   261 
273 def get_c_namespace(app, docname):                262 def get_c_namespace(app, docname):
274     source = app.env.doc2path(docname)            263     source = app.env.doc2path(docname)
275     with open(source) as f:                       264     with open(source) as f:
276         for l in f:                               265         for l in f:
277             match = RE_namespace.search(l)        266             match = RE_namespace.search(l)
278             if match:                             267             if match:
279                 return match.group(1)             268                 return match.group(1)
280     return ''                                     269     return ''
281                                                   270 
282 def markup_git(docname, app, match):           << 
283     # While we could probably assume that we a << 
284     # repository, we can't know for sure, so l << 
285     # turn them into git.kernel.org links with << 
286     # validity. (Maybe we can do something in  << 
287     # these references if this is explicitly r << 
288     text = match.group(0)                      << 
289     rev = match.group('rev')                   << 
290     return nodes.reference('', nodes.Text(text << 
291         refuri=f'https://git.kernel.org/torval << 
292                                                << 
293 def auto_markup(app, doctree, name):              271 def auto_markup(app, doctree, name):
294     global c_namespace                            272     global c_namespace
295     c_namespace = get_c_namespace(app, name)      273     c_namespace = get_c_namespace(app, name)
296     def text_but_not_a_reference(node):        << 
297         # The nodes.literal test catches ``lit << 
298         # avoid adding cross-references to fun << 
299         # marked with cc:func:.                << 
300         if not isinstance(node, nodes.Text) or << 
301             return False                       << 
302                                                << 
303         child_of_reference = False             << 
304         parent = node.parent                   << 
305         while parent:                          << 
306             if isinstance(parent, nodes.Refere << 
307                 child_of_reference = True      << 
308                 break                          << 
309             parent = parent.parent             << 
310         return not child_of_reference          << 
311                                                << 
312     #                                             274     #
313     # This loop could eventually be improved o    275     # This loop could eventually be improved on.  Someday maybe we
314     # want a proper tree traversal with a lot     276     # want a proper tree traversal with a lot of awareness of which
315     # kinds of nodes to prune.  But this works    277     # kinds of nodes to prune.  But this works well for now.
316     #                                             278     #
                                                   >> 279     # The nodes.literal test catches ``literal text``, its purpose is to
                                                   >> 280     # avoid adding cross-references to functions that have been explicitly
                                                   >> 281     # marked with cc:func:.
                                                   >> 282     #
317     for para in doctree.traverse(nodes.paragra    283     for para in doctree.traverse(nodes.paragraph):
318         for node in para.traverse(condition=te !! 284         for node in para.traverse(nodes.Text):
319             node.parent.replace(node, markup_r !! 285             if not isinstance(node.parent, nodes.literal):
                                                   >> 286                 node.parent.replace(node, markup_refs(name, app, node))
320                                                   287 
321 def setup(app):                                   288 def setup(app):
322     app.connect('doctree-resolved', auto_marku    289     app.connect('doctree-resolved', auto_markup)
323     return {                                      290     return {
324         'parallel_read_safe': True,               291         'parallel_read_safe': True,
325         'parallel_write_safe': True,              292         'parallel_write_safe': True,
326         }                                         293         }
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php