Blame progit/doc_utils.py

Pierre-Yves Chibon 9f7aba
#-*- coding: utf-8 -*-
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
"""
Pierre-Yves Chibon 9f7aba
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
 Authors:
Pierre-Yves Chibon 9f7aba
   Ralph Bean <rbean@redhat.com></rbean@redhat.com>
Pierre-Yves Chibon 9f7aba
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
"""
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
import docutils
Pierre-Yves Chibon 9f7aba
import docutils.examples
Pierre-Yves Chibon 9f7aba
import markupsafe
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
def modify_rst(rst):
Pierre-Yves Chibon 9f7aba
    """ Downgrade some of our rst directives if docutils is too old. """
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    ## We catch Exception if we want :-p
Pierre-Yves Chibon 9f7aba
    # pylint: disable=W0703
Pierre-Yves Chibon 9f7aba
    try:
Pierre-Yves Chibon 9f7aba
        # The rst features we need were introduced in this version
Pierre-Yves Chibon 9f7aba
        minimum = [0, 9]
Pierre-Yves Chibon 9f7aba
        version = [int(cpt) for cpt in docutils.__version__.split('.')]
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
        # If we're at or later than that version, no need to downgrade
Pierre-Yves Chibon 9f7aba
        if version >= minimum:
Pierre-Yves Chibon 9f7aba
            return rst
Pierre-Yves Chibon 9f7aba
    except Exception:  # pragma: no cover
Pierre-Yves Chibon 9f7aba
        # If there was some error parsing or comparing versions, run the
Pierre-Yves Chibon 9f7aba
        # substitutions just to be safe.
Pierre-Yves Chibon 9f7aba
        pass
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    # On Fedora this will never work as the docutils version is to recent
Pierre-Yves Chibon 9f7aba
    # Otherwise, make code-blocks into just literal blocks.
Pierre-Yves Chibon 9f7aba
    substitutions = {  # pragma: no cover
Pierre-Yves Chibon 9f7aba
        '.. code-block:: javascript': '::',
Pierre-Yves Chibon 9f7aba
    }
Pierre-Yves Chibon 9f7aba
    for old, new in substitutions.items():  # pragma: no cover
Pierre-Yves Chibon 9f7aba
        rst = rst.replace(old, new)
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    return rst  # pragma: no cover
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
def modify_html(html):
Pierre-Yves Chibon 9f7aba
    """ Perform style substitutions where docutils doesn't do what we want.
Pierre-Yves Chibon 9f7aba
    """
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    substitutions = {
Pierre-Yves Chibon 9f7aba
        '<tt class="docutils literal">': '',</tt>
Pierre-Yves Chibon 9f7aba
        '': '',
Pierre-Yves Chibon 9f7aba
    }
Pierre-Yves Chibon 9f7aba
    for old, new in substitutions.items():
Pierre-Yves Chibon 9f7aba
        html = html.replace(old, new)
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    return html
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
def convert_doc(rst_string):
Pierre-Yves Chibon 9f7aba
    """ Utility to load an RST file and turn it into fancy HTML. """
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    rst = modify_rst(rst_string)
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    html_string = docutils.examples.html_body(rst)
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    html_string = modify_html(html_string)
Pierre-Yves Chibon 9f7aba
Pierre-Yves Chibon 9f7aba
    html_string = markupsafe.Markup(html_string)
Pierre-Yves Chibon 9f7aba
    return html_string