Fetching contributors…
Cannot retrieve contributors at this time
138 lines (109 sloc) 4.07 KB
import logging
from wok import util
# Check for pygments
import pygments
have_pygments = True
except ImportError:
logging.warn('Pygments not enabled.')
have_pygments = False
# List of available renderers
all = []
class Renderer(object):
extensions = []
def render(cls, plain, page_meta): # the page_meta might contain renderer options...
return plain
class Plain(Renderer):
"""Plain text renderer. Replaces new lines with html </br>s"""
extensions = ['txt']
def render(cls, plain, page_meta):
return plain.replace('\n', '<br>')
# Include markdown, if it is available.
from markdown import markdown
class Markdown(Renderer):
"""Markdown renderer."""
extensions = ['markdown', 'mkd', 'md']
plugins = [
if have_pygments:
plugins.extend(['codehilite(css_class=codehilite)', 'fenced_code'])
def render(cls, plain, page_meta):
return markdown(plain, extensions=cls.plugins)
except ImportError:
logging.warn("markdown isn't available, trying markdown2")
markdown = None
# Try Markdown2
if markdown is None:
import markdown2
class Markdown2(Renderer):
"""Markdown2 renderer."""
extensions = ['markdown', 'mkd', 'md']
extras = ['def_list', 'footnotes']
if have_pygments:
def render(cls, plain, page_meta):
return markdown2.markdown(plain, extras=cls.extras)
except ImportError:
logging.warn('Markdown not enabled.')
# Include ReStructuredText Parser, if we have docutils
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']
options = {}
def render(cls, plain, page_meta):
w = rst_html_writer()
#return docutils.core.publish_parts(plain, writer=w)['body']
# Problem: missing heading and/or title if it's a lone heading
# Solution:
# Disable the promotion of a lone top-level section title to document title
# (and subsequent section title to document subtitle promotion)
overrides = { 'doctitle_xform': page_meta.get('rst_doctitle', cls.options['doctitle']), }
return docutils.core.publish_parts(plain, writer=w, settings_overrides=overrides, source_path=page_meta['source_path'])['body']
except ImportError:
logging.warn('reStructuredText not enabled.')
# Try Textile
import textile
class Textile(Renderer):
"""Textile renderer."""
extensions = ['textile']
def render(cls, plain, page_meta):
return textile.textile(plain)
except ImportError:
logging.warn('Textile not enabled.')
if len(all) <= 2:
logging.error("You probably want to install either a Markdown library (one of "
"'Markdown', or 'markdown2'), 'docutils' (for reStructuredText), or "
"'textile'. Otherwise only plain text input will be supported. You "
"can install any of these with 'sudo pip install PACKAGE'.")