From f40fceed53fd64ea65689cb15e5a0af24e4137f5 Mon Sep 17 00:00:00 2001 From: Johannes Löthberg Date: Sun, 27 Nov 2016 13:47:36 +0100 Subject: Add a ruby character role MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Löthberg --- scripts/utils/reST.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/scripts/utils/reST.py b/scripts/utils/reST.py index d99758c..bcd688f 100644 --- a/scripts/utils/reST.py +++ b/scripts/utils/reST.py @@ -1,4 +1,6 @@ -from docutils import core +from docutils import core, nodes +from docutils.parsers.rst import roles + def reST_to_html(source): settings_overrides = { @@ -11,3 +13,22 @@ def reST_to_html(source): writer_name='html4css1', settings_overrides=settings_overrides) return formatted['body'] + + +def ruby_role(name, rawtext, text, lineno, inliner, options={}, content=[]): + base, ruby = text.split('|', 1) + template = '{base}{ruby}' + + if 'format' not in options: + options['format'] = 'html' + elif options['format'] != 'html': + msg = inliner.reporter.error( + 'The ruby role only works with the HTML format.', line=lineno) + prb = inliner.problematic(rawtext, rawtext, msg) + return ([prb], [msg]) + + node = nodes.raw(rawtext, template.format(base=base, ruby=ruby), **options) + node.source, node.line = inliner.reporter.get_source_and_line(lineno) + return ([node], []) + +roles.register_canonical_role('ruby', ruby_role) -- cgit v1.2.3-54-g00ecf