ReSTful underline in Nikola

Sometimes you want to underline something. *italics* bzw. <em>i­tal­ic­s</em> just switches to an italics iversion of your font. You can fix that with CSS; but what if you want both?

This is how to do it in my blog sys­tem (Niko­la):

  • in plugins/under.plugin:

    name = rest_under
    module = under
    compiler = rest
    plugincategory = CompilerExtension
    author = Matthias Urlichs
    version = 0.1
    website =
    description = UnderLine the marked text
  • in plugins/

    # -*- coding: utf-8 -*-
    # Copyright © 2015 Matthias Urlichs
    """reST role for underlining."""
    from docutils import nodes
    from docutils.parsers.rst import roles
    from nikola.plugin_categories import RestExtension
    class Plugin(RestExtension):
        """Plugin for doc role."""
        name = 'rest_under'
        def set_site(self, site):
            """Set Nikola site."""
            roles.register_canonical_role('u', under_role)
            return super(Plugin, self).set_site(site)
    def under_role(name, rawtext, text, lineno, inliner,
                 options={}, content=[]):
        """Generate a underline-class emphasis."""
        options = dict(**options)
        options['classes'] = ['underline']
        return [nodes.emphasis(rawtext, text, *content, **options)],[]
  • in files/assets/css/custom.css:

    em.underline { font-style: normal; text-decoration: underline; }
  • … et voilà, you can now un­der­line.

    … et voilà, you can now :u:`underline`.

You're wel­come.