Skip to content

Commit 0d1999c

Browse files
committed
Add a sphinx extension to automatically bold RFC 2119 words
1 parent 08f12ce commit 0d1999c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

Diff for: src/_array_api_conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"sphinx_favicon",
3838
"sphinx_markdown_tables",
3939
"sphinxcontrib.jquery",
40+
"autobold_rfc2119",
4041
]
4142

4243
autosummary_generate = True

Diff for: src/autobold_rfc2119.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from sphinx.application import Sphinx
2+
import re
3+
4+
rfc_2119_words = [
5+
'must',
6+
'must not',
7+
'required',
8+
'shall',
9+
'shall not',
10+
'should',
11+
'should not',
12+
'recommended',
13+
'not recommended',
14+
'may',
15+
'optional',
16+
]
17+
18+
def bold_terms(source):
19+
return re.sub(rf'\b({"|".join(rfc_2119_words)})\b(?!\[)', r'**\1**', source, flags=re.IGNORECASE)
20+
21+
def bold_terms_source_read(app, docname, source):
22+
source[0] = bold_terms(source[0])
23+
24+
def bold_terms_autodoc(app, what, name, obj, options, lines):
25+
for i in range(len(lines)):
26+
lines[i] = bold_terms(lines[i])
27+
28+
def setup(app: Sphinx):
29+
app.connect('source-read', bold_terms_source_read)
30+
app.connect('autodoc-process-docstring', bold_terms_autodoc)

0 commit comments

Comments
 (0)