From d489c7a0579d13d29d42f29d6284cf51e57013d5 Mon Sep 17 00:00:00 2001 From: sommersoft Date: Sun, 28 Jul 2019 15:59:26 -0500 Subject: [PATCH] change 'c2rst' from source_parser -> extension; allows use of sphinx 2.x --- conf.py | 6 +++--- docs/c2rst.py | 46 +++++++++++++++++++++++++++++----------------- 2 files changed, 32 insertions(+), 20 deletions(-) diff --git a/conf.py b/conf.py index 7a00454e09..56625e758f 100644 --- a/conf.py +++ b/conf.py @@ -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' diff --git a/docs/c2rst.py b/docs/c2rst.py index 904fb74084..76489dca30 100644 --- a/docs/c2rst.py +++ b/docs/c2rst.py @@ -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)