Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Adding untested jinja2 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
shazow committed Oct 25, 2011
1 parent c872dff commit b3e30bd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
51 changes: 44 additions & 7 deletions composer/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@
except ImportError:
docutils = False

try:
import jinja2
except ImportError:
jinja2 = False


__all__ = ['Filter', 'Markdown', 'Mako', 'MakoContainer', 'RestructuredText']
__all__ = ['Filter',
'Mako', 'MakoContainer', 'Jinja2',
'RestructuredText','Markdown']


class Filter(object):
Expand Down Expand Up @@ -60,18 +67,14 @@ def __init__(self, index, **template_kw):

super(Mako, self).__init__(index)

from mako.lookup import TemplateLookup

kw = dict(input_encoding='utf-8', output_encoding='utf-8', encoding_errors='replace')
kw.update(template_kw)

self.template_kw = kw
self.lookup = TemplateLookup(**self.template_kw)

self.lookup = mako.lookup.TemplateLookup(**self.template_kw)

def __call__(self, content, route=None):
from mako.template import Template
t = Template(content, lookup=self.lookup, input_encoding='utf-8', output_encoding='utf-8', encoding_errors='replace')
t = mako.template.Template(content, lookup=self.lookup, **self.template_kw)

return str(t.render(index=self.index, route=route))

Expand All @@ -96,18 +99,52 @@ def __call__(self, content, route=None):


class RestructuredText(Filter):
# FIXME: This is untested and probably not Best Practices compliant. Someone
# who seriously uses RST should make this filter better.

def __init__(self, index, **rst_kw):
if not docutils:
raise ImportError("RestructuredText filter requires the 'docutils' package to be installed.")

super(RestructuredText, self).__init__(index)

self.rst_kw = rst_kw

def __call__(self, content, route=None):
return docutils.core.publish_string(content, **self.rst_kw)


class Jinja2(Filter):
# FIXME: This is untested and probably not Best Practices compliant. Someone
# who seriously uses Jinja2 should make this filter better.

# TODO: Make a Jinja2Container version of this Filter (similar to MakoContainer)

def __init__(self, index, searchpaths=None):
super(Jinja2, self).__init__(index)

if not jinja2:
raise ImportError("Jinja2 filter requires the 'Jinja2' package to be installed.")

loaders = []
if searchpaths:
loaders.append(jinja2.FileSystemLoader(searchpaths))

# TODO: Add support for more loaders?

self.jinja_env = jinja2.Environment(
loaders=jinja2.ChoiceLoaders(loaders)
)

def __call__(self, content, route=None):
t = self.jinja_env.from_string(content)
return t.render(index=self.index, route=route)



default_filters = {
'mako': Mako,
'markdown': Markdown,
'rst': RestructuredText,
'jinja2': Jinja2,
}
1 change: 1 addition & 0 deletions optional.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Dependency: Filters:

Mako # composer.filters.Mako, composer.filters.MakoContainer
Jinja2 # composer.filters.Jinja2
markdown2 # composer.filters.Markdown
docutils # composer.filters.RestructuredText

Expand Down

0 comments on commit b3e30bd

Please sign in to comment.