From 8e2d2753903dd0c9e2c5dd5b8fc61e0c613161a5 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Wed, 10 Aug 2005 18:31:33 +0000 Subject: [PATCH] Fixed #241 -- added django.contrib.markup app with markup templatetags for Textile, ReST and Markdown git-svn-id: http://code.djangoproject.com/svn/django/trunk@467 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/markup/__init__.py | 0 .../contrib/markup/templatetags/__init__.py | 0 django/contrib/markup/templatetags/markup.py | 46 +++++++++++++++++++ tests/othertests/markup.py | 45 ++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 django/contrib/markup/__init__.py create mode 100644 django/contrib/markup/templatetags/__init__.py create mode 100644 django/contrib/markup/templatetags/markup.py create mode 100644 tests/othertests/markup.py diff --git a/django/contrib/markup/__init__.py b/django/contrib/markup/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/markup/templatetags/__init__.py b/django/contrib/markup/templatetags/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/django/contrib/markup/templatetags/markup.py b/django/contrib/markup/templatetags/markup.py new file mode 100644 index 0000000000..2ee6f06af7 --- /dev/null +++ b/django/contrib/markup/templatetags/markup.py @@ -0,0 +1,46 @@ +""" +Set of "markup" template filters for Django. These filters transform plain text +markup syntaxes to HTML; currently there is support for: + + * Textile, which requires the PyTextile library available at + http://dealmeida.net/projects/textile/ + + * Markdown, which requires the Python-markdown library from + http://www.freewisdom.org/projects/python-markdown + + * ReStructuredText, which requires docutils from http://docutils.sf.net/ + +In each case, if the required library is not installed, the filter will +silently fail and return the un-marked-up text. +""" + +from django.core import template + +def textile(value, _): + try: + import textile + except ImportError: + return value + else: + return textile.textile(value) + +def markdown(value, _): + try: + import markdown + except ImportError: + return value + else: + return markdown.markdown(value) + +def restructuredtext(value, _): + try: + from docutils.core import publish_parts + except ImportError: + return value + else: + parts = publish_parts(source=value, writer_name="html4css1") + return parts["fragment"] + +template.register_filter("textile", textile, False) +template.register_filter("markdown", markdown, False) +template.register_filter("restructuredtext", restructuredtext, False) \ No newline at end of file diff --git a/tests/othertests/markup.py b/tests/othertests/markup.py new file mode 100644 index 0000000000..0489b16b30 --- /dev/null +++ b/tests/othertests/markup.py @@ -0,0 +1,45 @@ +# Quick tests for the markup templatetags (django.contrib.markup) +# +# Requires that all supported markup modules be installed +# (http://dealmeida.net/projects/textile/, +# http://www.freewisdom.org/projects/python-markdown, and +# http://docutils.sf.net/) + + +from django.core.template import Template, Context +import django.contrib.markup.templatetags.markup # this registers the filters + +# simple examples 'cause this isn't actually testing the markup, just +# that the filters work as advertised + +textile_content = """Paragraph 1 + +Paragraph 2 with "quotes" and @code@""" + +markdown_content = """Paragraph 1 + +## An h2 with *italics*""" + +rest_content = """Paragraph 1 + +Paragraph 2 with a link_ + +.. _link: http://www.example.com/""" + +t = Template("""{{ textile_content|textile }} +---- +{{ markdown_content|markdown }} +---- +{{ rest_content|restructuredtext }}""") + +rendered = t.render(Context(locals())) + +assert rendered.strip() == """

Paragraph 1

+ +

Paragraph 2 with “quotes” and code

+---- +

Paragraph 1

An h2 with *italics*

+ +---- +

Paragraph 1

+

Paragraph 2 with a link

""" \ No newline at end of file