|
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 |
1a4999 |
import docutils.core
|
|
Pierre-Yves Chibon |
9f7aba |
import markupsafe
|
|
Pierre-Yves Chibon |
c8507f |
import markdown
|
|
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 |
rst = modify_rst(rst_string)
|
|
Pierre-Yves Chibon |
9f7aba |
|
|
Pierre-Yves Chibon |
1a9d17 |
overrides = {'report_level': 'quiet'}
|
|
Pierre-Yves Chibon |
1a4999 |
html = docutils.core.publish_parts(
|
|
Pierre-Yves Chibon |
1a4999 |
source=rst,
|
|
Pierre-Yves Chibon |
1a4999 |
writer_name='html',
|
|
Pierre-Yves Chibon |
1a4999 |
settings_overrides=overrides)
|
|
Pierre-Yves Chibon |
1a4999 |
|
|
Pierre-Yves Chibon |
1a4999 |
html_string = html['html_body']
|
|
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
|
|
Pierre-Yves Chibon |
c8507f |
|
|
Pierre-Yves Chibon |
c8507f |
|
|
Pierre-Yves Chibon |
c8507f |
def convert_readme(content, ext):
|
|
Pierre-Yves Chibon |
c8507f |
''' Convert the provided content according to the extension of the file
|
|
Pierre-Yves Chibon |
c8507f |
provided.
|
|
Pierre-Yves Chibon |
c8507f |
'''
|
|
Pierre-Yves Chibon |
c8507f |
output = content
|
|
Pierre-Yves Chibon |
c8507f |
if ext and ext in ['.rst']:
|
|
Pierre-Yves Chibon |
792a86 |
output = convert_doc(unicode(content))
|
|
Pierre-Yves Chibon |
c8507f |
elif ext and ext in ['.mk']:
|
|
Pierre-Yves Chibon |
c8507f |
output = markdown.markdown(content)
|
|
Pierre-Yves Chibon |
c8507f |
return output
|