summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJohannes Löthberg <johannes@kyriasis.com>2016-11-27 13:47:36 +0100
committerJohannes Löthberg <johannes@kyriasis.com>2016-11-27 13:47:36 +0100
commitf40fceed53fd64ea65689cb15e5a0af24e4137f5 (patch)
tree5cd096f72c37fa2cb49677af66e13202fb468ca5
parent66be1311a50f65ccd13b501d4a4f34c7453fefb4 (diff)
downloadkyblo-f40fceed53fd64ea65689cb15e5a0af24e4137f5.tar.xz
Add a ruby character role
Signed-off-by: Johannes Löthberg <johannes@kyriasis.com>
-rw-r--r--scripts/utils/reST.py23
1 files changed, 22 insertions, 1 deletions
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 = '<ruby><rb>{base}</rb><rp>(</rp><rt>{ruby}</rt><rp>)</rp></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)