change 'c2rst' from source_parser -> extension; allows use of sphinx 2.x

This commit is contained in:
sommersoft 2019-07-28 15:59:26 -05:00
parent 143275db04
commit d489c7a057
2 changed files with 32 additions and 20 deletions

View File

@ -54,7 +54,8 @@ extensions = [
'sphinx.ext.intersphinx',
'sphinx.ext.todo',
'sphinx.ext.coverage',
'rstjinja'
'rstjinja',
'c2rst'
]
# Add any paths that contain templates here, relative to this directory.
@ -63,8 +64,7 @@ templates_path = ['templates']
# The suffix of source filenames.
source_suffix = ['.rst', '.md', '.c', '.h']
source_parsers = {'.md': CommonMarkParser,
'.c': "c2rst.CStrip", '.h': "c2rst.CStrip"}
source_parsers = {'.md': CommonMarkParser}
# The encoding of source files.
#source_encoding = 'utf-8-sig'

View File

@ -1,19 +1,31 @@
import sphinx.parsers
def c2rst(app, docname, source):
""" Pre-parse '.c' & '.h' files that contain rST source.
"""
# Make sure we're outputting HTML
if app.builder.format != 'html':
return
class CStrip(sphinx.parsers.Parser):
def __init__(self):
self.rst_parser = sphinx.parsers.RSTParser()
fname = app.env.doc2path(docname)
if (not fname.endswith(".c") and
not fname.endswith(".h")):
#print("skipping:", fname)
return
def parse(self, inputstring, document):
# This setting is missing starting with Sphinx 1.7.1 so we set it ourself.
document.settings.tab_width = 4
document.settings.character_level_inline_markup = False
stripped = []
for line in inputstring.split("\n"):
line = line.strip()
if line == "//|":
stripped.append("")
elif line.startswith("//| "):
stripped.append(line[len("//| "):])
stripped = "\r\n".join(stripped)
self.rst_parser.parse(stripped, document)
src = source[0]
stripped = []
for line in src.split("\n"):
line = line.strip()
if line == "//|":
stripped.append("")
elif line.startswith("//| "):
stripped.append(line[len("//| "):])
stripped = "\r\n".join(stripped)
rendered = app.builder.templates.render_string(
stripped, app.config.html_context
)
source[0] = rendered
def setup(app):
app.connect("source-read", c2rst)