Added Jinja2-port of 'markup' contrib module.
This commit is contained in:
0
coffin/contrib/__init__.py
Normal file
0
coffin/contrib/__init__.py
Normal file
0
coffin/contrib/markup/__init__.py
Normal file
0
coffin/contrib/markup/__init__.py
Normal file
0
coffin/contrib/markup/models.py
Normal file
0
coffin/contrib/markup/models.py
Normal file
0
coffin/contrib/markup/templatetags/__init__.py
Normal file
0
coffin/contrib/markup/templatetags/__init__.py
Normal file
15
coffin/contrib/markup/templatetags/markup.py
Normal file
15
coffin/contrib/markup/templatetags/markup.py
Normal 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)
|
||||
@@ -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
16
tests/test_contrib.py
Normal 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())
|
||||
Reference in New Issue
Block a user