cubicweb/ext/markdown.py
author Philippe Pepiot <philippe.pepiot@logilab.fr>
Fri, 15 Jun 2018 17:45:26 +0200
changeset 12318 e947954e0ffc
parent 11057 0b59724cb3f2
permissions -rw-r--r--
[py3] ensure AjaxController return bytes publish() must return bytes, when the @ajaxfunc decorated method return None, this raised: TypeError: You cannot set Response.body to a text object (use Response.text)

from __future__ import absolute_import
import markdown

import logging

log = logging.getLogger(__name__)


def markdown_publish(context, data):
    """publish a string formatted as MarkDown Text to HTML

    :type context: a cubicweb application object

    :type data: str
    :param data: some MarkDown text

    :rtype: unicode
    :return:
      the data formatted as HTML or the original data if an error occurred
    """
    md = markdown.Markdown()
    try:
        return md.convert(data)
    except:
        import traceback; traceback.print_exc()
        log.exception("Error while converting Markdown to HTML")
        return data