Skip to content
This repository
Browse code

Make markdown, pygments, and docutils dependencies optional.

  • Loading branch information...
commit c925814443334be91dbf938dc083145f2b8f1f7f 1 parent 7d88b6f
Mike Cooper authored

Showing 3 changed files with 63 additions and 28 deletions. Show diff stats Hide diff stats

  1. +1 1  setup.py
  2. +8 4 wok/engine.py
  3. +54 23 wok/renderers.py
2  setup.py
@@ -7,7 +7,7 @@
7 7 setup(name='wok',
8 8 version=version.encode("utf8"),
9 9 description='Static site generator',
10   - install_requires=['pyyaml', 'markdown', 'docutils', 'jinja2'],
  10 + install_requires=['pyyaml', 'jinja2'],
11 11 author='Mike Cooper',
12 12 author_email='mythmon@gmail.com',
13 13 url='https://www.github.com/mythmon/wok',
12 wok/engine.py
@@ -101,10 +101,14 @@ def load_pages(self):
101 101 for r in renderers.all:
102 102 if ext in r.extensions:
103 103 renderer = r
104   -
105   - self.all_pages.append(
106   - page.Page(os.path.join(root,f), self.options,
107   - renderer))
  104 + break
  105 + else:
  106 + util.out.warn('Loading render', 'No parser found '
  107 + 'for {0}. Using default renderer.'.format(f))
  108 + renderer = renderers.Renderer
  109 +
  110 + self.all_pages.append(page.Page(os.path.join(root,f),
  111 + self.options, renderer))
108 112
109 113 def make_tree(self):
110 114 self.categories = {}
77 wok/renderers.py
... ... @@ -1,10 +1,13 @@
1   -# Markdown
2   -from markdown import markdown
3   -# reStructuredText
4   -import docutils.core
5   -from docutils.writers.html4css1 import Writer as rst_html_writer
6   -from docutils.parsers.rst import directives
7   -from wok.rst_pygments import Pygments as RST_Pygments
  1 +# Check for pygments
  2 +try:
  3 + import pygments
  4 + have_pygments = True
  5 +except ImportError:
  6 + out.util.info('Loading renderers', 'Pygments not enabled.')
  7 + have_pygments = False
  8 +
  9 +# List of available renderers
  10 +all = []
8 11
9 12 class Renderer(object):
10 13 extensions = []
@@ -12,29 +15,57 @@ class Renderer(object):
12 15 @classmethod
13 16 def render(cls, plain):
14 17 return plain
  18 +all.append(Renderer)
15 19
16   -class Markdown(Renderer):
17   - extensions = ['markdown', 'mkd']
  20 +class Plain(Renderer):
  21 + """Plain text renderer. Replaces new lines with html </br>s"""
  22 + extensions = ['txt']
18 23
19 24 @classmethod
20 25 def render(cls, plain):
21   - return markdown(plain, ['def_list', 'footnotes', 'codehilite(css_class=highlight )'])
  26 + return plain.replace('\n', '<br>')
  27 +all.append(Plain)
22 28
23   -class ReStructuredText(Renderer):
24   - directives.register_directive('Pygments', RST_Pygments)
  29 +# Include markdown, if it is available.
  30 +try:
  31 + from markdown import markdown
  32 + class Markdown(Renderer):
  33 + """Markdown renderer."""
  34 + extensions = ['markdown', 'mkd']
25 35
26   - extensions = ['rst']
  36 + plugins = ['def_list', 'footnotes']
  37 + if have_pygments:
  38 + plugins.append('codehilite(css_class=highlight)')
27 39
28   - @classmethod
29   - def render(cls, plain):
30   - w = rst_html_writer()
31   - return docutils.core.publish_parts(plain, writer=w)['body']
  40 + @classmethod
  41 + def render(cls, plain):
  42 + return markdown(plain, Markdown.plugins)
32 43
33   -class Plain(Renderer):
34   - extensions = 'txt'
  44 + all.append(Markdown)
35 45
36   - @classmethod
37   - def render(cls, plain):
38   - return plain.replace('\n', '<br>')
  46 +except ImportError:
  47 + util.out.info('Loading renderers', 'Markdown not enabled.')
  48 +
  49 +# Include ReStructuredText Parser, if we have docutils
  50 +try:
  51 +
  52 + import docutils.core
  53 + from docutils.writers.html4css1 import Writer as rst_html_writer
  54 + from docutils.parsers.rst import directives
  55 +
  56 + if have_pygments:
  57 + from wok.rst_pygments import Pygments as RST_Pygments
  58 + directives.register_directive('Pygments', RST_Pygments)
  59 +
  60 + class ReStructuredText(Renderer):
  61 + """reStructuredText renderer."""
  62 + extensions = ['rst']
  63 +
  64 + @classmethod
  65 + def render(cls, plain):
  66 + w = rst_html_writer()
  67 + return docutils.core.publish_parts(plain, writer=w)['body']
39 68
40   -all = [Renderer, Plain, Markdown, ReStructuredText]
  69 + all.append(ReStructuredText)
  70 +except:
  71 + 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.