Skip to content
This repository
tag: v0.8.1
Fetching contributors…

Cannot retrieve contributors at this time

file 80 lines (61 sloc) 2.002 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
import logging

from wok import util

# Check for pygments
try:
    import pygments
    have_pygments = True
except ImportError:
    logging.info('Pygments not enabled.')
    have_pygments = False

# List of available renderers
all = []

class Renderer(object):
    extensions = []

    @classmethod
    def render(cls, plain):
        return plain
all.append(Renderer)

class Plain(Renderer):
    """Plain text renderer. Replaces new lines with html </br>s"""
    extensions = ['txt']

    @classmethod
    def render(cls, plain):
        return plain.replace('\n', '<br>')
all.append(Plain)

# Include markdown, if it is available.
try:
    from markdown import markdown
    class Markdown(Renderer):
        """Markdown renderer."""
        extensions = ['markdown', 'mkd']

        plugins = ['def_list', 'footnotes']
        if have_pygments:
            plugins.append('codehilite(css_class=highlight)')

        @classmethod
        def render(cls, plain):
            return markdown(plain, Markdown.plugins)

    all.append(Markdown)

except ImportError:
    logging.info('Markdown not enabled.')

# Include ReStructuredText Parser, if we have docutils
try:

    import docutils.core
    from docutils.writers.html4css1 import Writer as rst_html_writer
    from docutils.parsers.rst import directives

    if have_pygments:
        from wok.rst_pygments import Pygments as RST_Pygments
        directives.register_directive('Pygments', RST_Pygments)

    class ReStructuredText(Renderer):
        """reStructuredText renderer."""
        extensions = ['rst']

        @classmethod
        def render(cls, plain):
            w = rst_html_writer()
            return docutils.core.publish_parts(plain, writer=w)['body']

    all.append(ReStructuredText)
except:
    logging.info('reStructuredText not enabled.')

if len(all) <= 2:
    print('You probably want to install either Markdown or docutils '
        '(reStructuredText). Otherwise only plain text input will be '
        'supported.')
Something went wrong with that request. Please try again.