ReSTful underline in Nikola

Some­times you want to un­der­line some­thing. *i­tal­ic­s* bzw. <em>i­­tal­ic­s</em> just switch­es to an ital­ics iver­sion 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 plu­g­in­s/un­der.­plug­in:

    [Core]
    name = rest_under
    module = under
    
    [Nikola]
    compiler = rest
    plugincategory = CompilerExtension
    
    [Documentation]
    author = Matthias Urlichs
    version = 0.1
    website = https://matthias.urlichs.de/
    description = UnderLine the marked text
    
  • in plu­g­in­s/un­der.py:

    # -*- 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/as­set­s/c­ss/­cus­tom.c­ss:

    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.