Skip to content

Commit d489c7a

Browse files
committed
change 'c2rst' from source_parser -> extension; allows use of sphinx 2.x
1 parent 143275d commit d489c7a

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

conf.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
'sphinx.ext.intersphinx',
5555
'sphinx.ext.todo',
5656
'sphinx.ext.coverage',
57-
'rstjinja'
57+
'rstjinja',
58+
'c2rst'
5859
]
5960

6061
# Add any paths that contain templates here, relative to this directory.
@@ -63,8 +64,7 @@
6364
# The suffix of source filenames.
6465
source_suffix = ['.rst', '.md', '.c', '.h']
6566

66-
source_parsers = {'.md': CommonMarkParser,
67-
'.c': "c2rst.CStrip", '.h': "c2rst.CStrip"}
67+
source_parsers = {'.md': CommonMarkParser}
6868

6969
# The encoding of source files.
7070
#source_encoding = 'utf-8-sig'

docs/c2rst.py

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
1-
import sphinx.parsers
1+
def c2rst(app, docname, source):
2+
""" Pre-parse '.c' & '.h' files that contain rST source.
3+
"""
4+
# Make sure we're outputting HTML
5+
if app.builder.format != 'html':
6+
return
27

3-
class CStrip(sphinx.parsers.Parser):
4-
def __init__(self):
5-
self.rst_parser = sphinx.parsers.RSTParser()
8+
fname = app.env.doc2path(docname)
9+
if (not fname.endswith(".c") and
10+
not fname.endswith(".h")):
11+
#print("skipping:", fname)
12+
return
613

7-
def parse(self, inputstring, document):
8-
# This setting is missing starting with Sphinx 1.7.1 so we set it ourself.
9-
document.settings.tab_width = 4
10-
document.settings.character_level_inline_markup = False
11-
stripped = []
12-
for line in inputstring.split("\n"):
13-
line = line.strip()
14-
if line == "//|":
15-
stripped.append("")
16-
elif line.startswith("//| "):
17-
stripped.append(line[len("//| "):])
18-
stripped = "\r\n".join(stripped)
19-
self.rst_parser.parse(stripped, document)
14+
src = source[0]
15+
16+
stripped = []
17+
for line in src.split("\n"):
18+
line = line.strip()
19+
if line == "//|":
20+
stripped.append("")
21+
elif line.startswith("//| "):
22+
stripped.append(line[len("//| "):])
23+
stripped = "\r\n".join(stripped)
24+
25+
rendered = app.builder.templates.render_string(
26+
stripped, app.config.html_context
27+
)
28+
source[0] = rendered
29+
30+
def setup(app):
31+
app.connect("source-read", c2rst)

0 commit comments

Comments
 (0)