|
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 |
2 | 7 |
|
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 |
6 | 13 |
|
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