summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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)