Skip to content
Browse files

Make markdown, pygments, and docutils dependencies optional.

  • Loading branch information...
1 parent 7d88b6f commit c925814443334be91dbf938dc083145f2b8f1f7f @mythmon committed Jun 4, 2011
Showing with 63 additions and 28 deletions.
  1. +1 −1 setup.py
  2. +8 −4 wok/engine.py
  3. +54 −23 wok/renderers.py
View
2 setup.py
@@ -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',
View
12 wok/engine.py
@@ -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 = {}
View
77 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.
Something went wrong with that request. Please try again.