Skip to content

Commit 71e552a

Browse files
committed
docs: cdomain.py: add support for a new Sphinx 3.1+ tag
Since Sphinx 3.0, the C domain code was rewritten, but only after version 3.1 it got support for setting namespaces on C domains, with is something that it is required, in order to document system calls, like ioctl() and others. As part of changing the documentation subsystem to properly build with Sphinx 3.1+, add support for such new tag: .. c:namespace::" Such tag optionally replaces the optional "name" tag for functions, setting a single namespace domain for all C references found at the file. With that, it should be possible to convert existing documentation to be compatible with both Sphinx 1.x/2.x and 3.1+. Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>
1 parent 6e9e415 commit 71e552a

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

‎Documentation/sphinx/cdomain.py‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,56 @@
4040
from sphinx.domains.c import c_funcptr_sig_re, c_sig_re
4141
from sphinx.domains.c import CObject as Base_CObject
4242
from sphinx.domains.c import CDomain as Base_CDomain
43+
from itertools import chain
44+
import re
4345

44-
__version__ = '1.0'
46+
__version__ = '1.1'
4547

4648
# Get Sphinx version
4749
major, minor, patch = sphinx.version_info[:3]
4850

51+
# Namespace to be prepended to the full name
52+
namespace = None
53+
54+
#
55+
# Handle trivial newer c domain tags that are part of Sphinx 3.1 c domain tags
56+
# - Store the namespace if ".. c:namespace::" tag is found
57+
58+
RE_namespace = re.compile(r'^\s*..\s*c:namespace::\s*(\S+)\s*$')
59+
60+
def markup_namespace(match):
61+
global namespace
62+
63+
namespace = match.group(1)
64+
65+
return ""
66+
67+
def c_markups(app, docname, source):
68+
result = ""
69+
markup_func = {
70+
RE_namespace: markup_namespace,
71+
}
72+
73+
lines = iter(source[0].splitlines(True))
74+
for n in lines:
75+
match_iterators = [regex.finditer(n) for regex in markup_func]
76+
matches = sorted(chain(*match_iterators), key=lambda m: m.start())
77+
for m in matches:
78+
n = n[:m.start()] + markup_func[m.re](m) + n[m.end():]
79+
80+
result = result + n
81+
82+
source[0] = result
83+
84+
#
85+
# Now implements support for the cdomain namespacing logic
86+
#
87+
4988
def setup(app):
5089

90+
# Handle easy Sphinx 3.1+ simple new tags: :c:expr and .. c:namespace::
91+
app.connect('source-read', c_markups)
92+
5193
if (major == 1 and minor < 8):
5294
app.override_domain(CDomain)
5395
else:
@@ -75,6 +117,8 @@ def handle_func_like_macro(self, sig, signode):
75117
function-like macro, the name of the macro is returned. Otherwise
76118
``False`` is returned. """
77119

120+
global namespace
121+
78122
if not self.objtype == 'function':
79123
return False
80124

@@ -107,11 +151,16 @@ def handle_func_like_macro(self, sig, signode):
107151
param += nodes.emphasis(argname, argname)
108152
paramlist += param
109153

154+
if namespace:
155+
fullname = namespace + "." + fullname
156+
110157
return fullname
111158

112159
def handle_signature(self, sig, signode):
113160
"""Transform a C signature into RST nodes."""
114161

162+
global namespace
163+
115164
fullname = self.handle_func_like_macro(sig, signode)
116165
if not fullname:
117166
fullname = super(CObject, self).handle_signature(sig, signode)
@@ -122,6 +171,10 @@ def handle_signature(self, sig, signode):
122171
else:
123172
# FIXME: handle :name: value of other declaration types?
124173
pass
174+
else:
175+
if namespace:
176+
fullname = namespace + "." + fullname
177+
125178
return fullname
126179

127180
def add_target_and_index(self, name, sig, signode):

0 commit comments

Comments
 (0)