Added Jinja2-port of 'markup' contrib module.

This commit is contained in:
Michael Elsdoerfer
2009-02-08 16:24:45 +01:00
parent e75ec65f29
commit 03b947bf80
7 changed files with 47 additions and 0 deletions

View File

View File

View File

View File

@@ -0,0 +1,15 @@
"""Makes the template filters from the ``django.contrib.markup`` app
available to both the Jinja2 and Django engines.
In other words, adding ``coffin.contrib.markup`` to your INSTALLED_APPS
setting will enable the markup filters not only through Coffin, but
also through the default Django template system.
"""
from coffin.template import Library as CoffinLibrary
from django.contrib.markup.templatetags.markup import register
# Convert Django's Library into a Coffin Library object, which will
# make sure the filters are correctly ported to Jinja2.
register = CoffinLibrary.from_django(register)

View File

@@ -73,6 +73,22 @@ class Library(DjangoLibrary):
self.jinja2_filters = {}
self.jinja2_extensions = []
@classmethod
def from_django(cls, django_library):
"""Create a Coffin library object from a Django library.
Specifically, this ensures that filters already registered
with the Django library are also made available to Jinja,
where applicable.
"""
from copy import copy
result = cls()
result.filters = copy(django_library.filters)
result.tags = copy(django_library.tags)
for name, func in result.filters.iteritems():
result._register_filter(name, func, jinja2_only=True)
return result
def tag(self, name_or_node=None, compile_function=None):
"""Register a Django template tag (1) or Jinja 2 extension (2).

16
tests/test_contrib.py Normal file
View File

@@ -0,0 +1,16 @@
from coffin.template import add_to_builtins as add_to_coffin_builtins
from django.template import add_to_builtins as add_to_django_builtins
from coffin.common import get_env
from django.template import Template, Context
def test_markup():
add_to_coffin_builtins('coffin.contrib.markup.templatetags.markup')
add_to_django_builtins('coffin.contrib.markup.templatetags.markup')
# Make sure filters will be available in both Django and Coffin.
# Note that we do not assert the result - if markdown is not installed,
# the filter will just return the input text. We don't care, we simple
# want to check the filter is available.
get_env().from_string('{{ "**Title**"|markdown }}').render() # '\n<p><strong>Title</strong>\n</p>\n\n\n'
Template('{{ "**Title**"|markdown }}').render(Context())