Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #241 -- added django.contrib.markup app with markup templatetag…

…s for Textile, ReST and Markdown

git-svn-id: http://code.djangoproject.com/svn/django/trunk@467 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 8e2d2753903dd0c9e2c5dd5b8fc61e0c613161a5 1 parent f65350b
Jacob Kaplan-Moss authored August 10, 2005
46  django/contrib/markup/templatetags/markup.py
... ...
@@ -0,0 +1,46 @@
  1
+"""
  2
+Set of "markup" template filters for Django.  These filters transform plain text
  3
+markup syntaxes to HTML; currently there is support for:
  4
+
  5
+    * Textile, which requires the PyTextile library available at
  6
+      http://dealmeida.net/projects/textile/
  7
+      
  8
+    * Markdown, which requires the Python-markdown library from
  9
+      http://www.freewisdom.org/projects/python-markdown
  10
+      
  11
+    * ReStructuredText, which requires docutils from http://docutils.sf.net/
  12
+    
  13
+In each case, if the required library is not installed, the filter will
  14
+silently fail and return the un-marked-up text.
  15
+"""
  16
+
  17
+from django.core import template
  18
+
  19
+def textile(value, _):
  20
+    try:
  21
+        import textile
  22
+    except ImportError:
  23
+        return value
  24
+    else:
  25
+        return textile.textile(value)
  26
+        
  27
+def markdown(value, _):
  28
+    try:
  29
+        import markdown
  30
+    except ImportError:
  31
+        return value
  32
+    else:
  33
+        return markdown.markdown(value)
  34
+        
  35
+def restructuredtext(value, _):
  36
+    try:
  37
+        from docutils.core import publish_parts
  38
+    except ImportError:
  39
+        return value
  40
+    else:
  41
+        parts = publish_parts(source=value, writer_name="html4css1")
  42
+        return parts["fragment"]
  43
+        
  44
+template.register_filter("textile", textile, False)
  45
+template.register_filter("markdown", markdown, False)
  46
+template.register_filter("restructuredtext", restructuredtext, False)
0  markup/__init__.py b/django/contrib/markup/__init__.py
No changes.
0  markup/templatetags/__init__.py b/django/contrib/markup/templatetags/__init__.py
No changes.
45  tests/othertests/markup.py
... ...
@@ -0,0 +1,45 @@
  1
+# Quick tests for the markup templatetags (django.contrib.markup)
  2
+#
  3
+# Requires that all supported markup modules be installed
  4
+# (http://dealmeida.net/projects/textile/, 
  5
+# http://www.freewisdom.org/projects/python-markdown,  and 
  6
+# http://docutils.sf.net/)
  7
+
  8
+
  9
+from django.core.template import Template, Context
  10
+import django.contrib.markup.templatetags.markup # this registers the filters
  11
+
  12
+# simple examples 'cause this isn't actually testing the markup, just
  13
+# that the filters work as advertised
  14
+
  15
+textile_content = """Paragraph 1
  16
+
  17
+Paragraph 2 with "quotes" and @code@"""
  18
+
  19
+markdown_content = """Paragraph 1
  20
+
  21
+## An h2 with *italics*"""
  22
+
  23
+rest_content = """Paragraph 1
  24
+
  25
+Paragraph 2 with a link_
  26
+
  27
+.. _link: http://www.example.com/"""
  28
+
  29
+t = Template("""{{ textile_content|textile }}
  30
+----
  31
+{{ markdown_content|markdown }}
  32
+----
  33
+{{ rest_content|restructuredtext }}""")
  34
+
  35
+rendered = t.render(Context(locals()))
  36
+
  37
+assert rendered.strip() == """<p>Paragraph 1</p>
  38
+
  39
+<p>Paragraph 2 with &#8220;quotes&#8221; and <code>code</code></p>
  40
+----
  41
+<p>Paragraph 1</p><h2>An h2 with *italics*</h2>
  42
+
  43
+----
  44
+<p>Paragraph 1</p>
  45
+<p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>"""

0 notes on commit 8e2d275

Please sign in to comment.
Something went wrong with that request. Please try again.