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 = '{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)