Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
Make markdown, pygments, and docutils dependencies optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
mythmon committed Jun 4, 2011
1 parent 7d88b6f commit c925814
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -7,7 +7,7 @@
setup(name='wok',
version=version.encode("utf8"),
description='Static site generator',
install_requires=['pyyaml', 'markdown', 'docutils', 'jinja2'],
install_requires=['pyyaml', 'jinja2'],
author='Mike Cooper',
author_email='mythmon@gmail.com',
url='https://www.github.com/mythmon/wok',
Expand Down
12 changes: 8 additions & 4 deletions wok/engine.py
Expand Up @@ -101,10 +101,14 @@ def load_pages(self):
for r in renderers.all:
if ext in r.extensions:
renderer = r

self.all_pages.append(
page.Page(os.path.join(root,f), self.options,
renderer))
break
else:
util.out.warn('Loading render', 'No parser found '
'for {0}. Using default renderer.'.format(f))
renderer = renderers.Renderer

self.all_pages.append(page.Page(os.path.join(root,f),
self.options, renderer))

def make_tree(self):
self.categories = {}
Expand Down
77 changes: 54 additions & 23 deletions wok/renderers.py
@@ -1,40 +1,71 @@
# Markdown
from markdown import markdown
# reStructuredText
import docutils.core
from docutils.writers.html4css1 import Writer as rst_html_writer
from docutils.parsers.rst import directives
from wok.rst_pygments import Pygments as RST_Pygments
# Check for pygments
try:
import pygments
have_pygments = True
except ImportError:
out.util.info('Loading renderers', '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 Markdown(Renderer):
extensions = ['markdown', 'mkd']
class Plain(Renderer):
"""Plain text renderer. Replaces new lines with html </br>s"""
extensions = ['txt']

@classmethod
def render(cls, plain):
return markdown(plain, ['def_list', 'footnotes', 'codehilite(css_class=highlight )'])
return plain.replace('\n', '<br>')
all.append(Plain)

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

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

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

class Plain(Renderer):
extensions = 'txt'
all.append(Markdown)

@classmethod
def render(cls, plain):
return plain.replace('\n', '<br>')
except ImportError:
util.out.info('Loading renderers', '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 = [Renderer, Plain, Markdown, ReStructuredText]
all.append(ReStructuredText)
except:
util.out.info('Loading renderers', 'reStructuredText not enabled.')

0 comments on commit c925814

Please sign in to comment.