1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
from docutils import core, nodes
from docutils.parsers.rst import roles
def reST_to_html(source):
settings_overrides = {
'footnote_references': 'superscript',
'auto_id_prefix': 'id-',
'initial_header_level': 3,
'doctitle_xform': False,
}
formatted = core.publish_parts(source,
writer_name='html5_polyglot',
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)
|