diff --git a/coffin/contrib/__init__.py b/coffin/contrib/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/coffin/contrib/markup/__init__.py b/coffin/contrib/markup/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/coffin/contrib/markup/models.py b/coffin/contrib/markup/models.py new file mode 100644 index 0000000..e69de29 diff --git a/coffin/contrib/markup/templatetags/__init__.py b/coffin/contrib/markup/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/coffin/contrib/markup/templatetags/markup.py b/coffin/contrib/markup/templatetags/markup.py new file mode 100644 index 0000000..0d6b92f --- /dev/null +++ b/coffin/contrib/markup/templatetags/markup.py @@ -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) \ No newline at end of file diff --git a/coffin/template/library.py b/coffin/template/library.py index ff13ae5..5dda9af 100644 --- a/coffin/template/library.py +++ b/coffin/template/library.py @@ -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). diff --git a/tests/test_contrib.py b/tests/test_contrib.py new file mode 100644 index 0000000..703bbf6 --- /dev/null +++ b/tests/test_contrib.py @@ -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

Title\n

\n\n\n' + Template('{{ "**Title**"|markdown }}').render(Context()) \ No newline at end of file