2006-08-27 21:59:47 +08:00
|
|
|
# Quick tests for the markup templatetags (django.contrib.markup)
|
|
|
|
import re
|
|
|
|
|
2011-10-25 05:30:55 +08:00
|
|
|
from django.template import Template, Context
|
2010-10-11 20:55:17 +08:00
|
|
|
from django.utils import unittest
|
2007-11-14 20:58:53 +08:00
|
|
|
from django.utils.html import escape
|
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
try:
|
|
|
|
import textile
|
|
|
|
except ImportError:
|
|
|
|
textile = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
import markdown
|
2012-03-16 08:36:52 +08:00
|
|
|
markdown_version = getattr(markdown, "version_info", 0)
|
2010-10-11 20:55:17 +08:00
|
|
|
except ImportError:
|
|
|
|
markdown = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
import docutils
|
|
|
|
except ImportError:
|
|
|
|
docutils = None
|
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
class Templates(unittest.TestCase):
|
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
textile_content = """Paragraph 1
|
2006-08-27 21:59:47 +08:00
|
|
|
|
|
|
|
Paragraph 2 with "quotes" and @code@"""
|
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
markdown_content = """Paragraph 1
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
## An h2"""
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
rest_content = """Paragraph 1
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
Paragraph 2 with a link_
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
.. _link: http://www.example.com/"""
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2012-04-14 20:35:31 +08:00
|
|
|
@unittest.skipUnless(textile, 'textile not installed')
|
2010-10-11 20:55:17 +08:00
|
|
|
def test_textile(self):
|
2011-10-25 05:30:55 +08:00
|
|
|
t = Template("{% load markup %}{{ textile_content|textile }}")
|
2010-10-11 20:55:17 +08:00
|
|
|
rendered = t.render(Context({'textile_content':self.textile_content})).strip()
|
|
|
|
self.assertEqual(rendered.replace('\t', ''), """<p>Paragraph 1</p>
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
<p>Paragraph 2 with “quotes” and <code>code</code></p>""")
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2012-04-14 20:35:31 +08:00
|
|
|
@unittest.skipIf(textile, 'textile is installed')
|
2010-10-11 20:55:17 +08:00
|
|
|
def test_no_textile(self):
|
2011-10-25 05:30:55 +08:00
|
|
|
t = Template("{% load markup %}{{ textile_content|textile }}")
|
2010-10-11 20:55:17 +08:00
|
|
|
rendered = t.render(Context({'textile_content':self.textile_content})).strip()
|
|
|
|
self.assertEqual(rendered, escape(self.textile_content))
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2012-04-14 20:35:31 +08:00
|
|
|
@unittest.skipUnless(markdown and markdown_version >= (2,1), 'markdown >= 2.1 not installed')
|
2010-10-11 20:55:17 +08:00
|
|
|
def test_markdown(self):
|
2011-10-25 05:30:55 +08:00
|
|
|
t = Template("{% load markup %}{{ markdown_content|markdown }}")
|
2010-10-11 20:55:17 +08:00
|
|
|
rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
|
|
|
|
pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""")
|
|
|
|
self.assertTrue(pattern.match(rendered))
|
|
|
|
|
2012-03-16 08:36:52 +08:00
|
|
|
@unittest.skipUnless(markdown and markdown_version >= (2,1), 'markdown >= 2.1 not installed')
|
2012-03-15 03:06:23 +08:00
|
|
|
def test_markdown_attribute_disable(self):
|
|
|
|
t = Template("{% load markup %}{{ markdown_content|markdown:'safe' }}")
|
|
|
|
markdown_content = "{@onclick=alert('hi')}some paragraph"
|
|
|
|
rendered = t.render(Context({'markdown_content':markdown_content})).strip()
|
|
|
|
self.assertTrue('@' in rendered)
|
|
|
|
|
2012-03-16 08:36:52 +08:00
|
|
|
@unittest.skipUnless(markdown and markdown_version >= (2,1), 'markdown >= 2.1 not installed')
|
2012-03-15 03:06:23 +08:00
|
|
|
def test_markdown_attribute_enable(self):
|
|
|
|
t = Template("{% load markup %}{{ markdown_content|markdown }}")
|
|
|
|
markdown_content = "{@onclick=alert('hi')}some paragraph"
|
|
|
|
rendered = t.render(Context({'markdown_content':markdown_content})).strip()
|
|
|
|
self.assertFalse('@' in rendered)
|
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
@unittest.skipIf(markdown, 'markdown is installed')
|
|
|
|
def test_no_markdown(self):
|
2011-10-25 05:30:55 +08:00
|
|
|
t = Template("{% load markup %}{{ markdown_content|markdown }}")
|
2010-10-11 20:55:17 +08:00
|
|
|
rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
|
|
|
|
self.assertEqual(rendered, self.markdown_content)
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
@unittest.skipUnless(docutils, 'docutils not installed')
|
|
|
|
def test_docutils(self):
|
2011-10-25 05:30:55 +08:00
|
|
|
t = Template("{% load markup %}{{ rest_content|restructuredtext }}")
|
2010-10-11 20:55:17 +08:00
|
|
|
rendered = t.render(Context({'rest_content':self.rest_content})).strip()
|
|
|
|
# Different versions of docutils return slightly different HTML
|
|
|
|
try:
|
|
|
|
# Docutils v0.4 and earlier
|
|
|
|
self.assertEqual(rendered, """<p>Paragraph 1</p>
|
2006-08-27 21:59:47 +08:00
|
|
|
<p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""")
|
2010-10-11 20:55:17 +08:00
|
|
|
except AssertionError, e:
|
|
|
|
# Docutils from SVN (which will become 0.5)
|
|
|
|
self.assertEqual(rendered, """<p>Paragraph 1</p>
|
2007-12-03 05:01:17 +08:00
|
|
|
<p>Paragraph 2 with a <a class="reference external" href="http://www.example.com/">link</a></p>""")
|
2010-10-11 20:55:17 +08:00
|
|
|
|
|
|
|
@unittest.skipIf(docutils, 'docutils is installed')
|
|
|
|
def test_no_docutils(self):
|
2011-10-25 05:30:55 +08:00
|
|
|
t = Template("{% load markup %}{{ rest_content|restructuredtext }}")
|
2010-10-11 20:55:17 +08:00
|
|
|
rendered = t.render(Context({'rest_content':self.rest_content})).strip()
|
|
|
|
self.assertEqual(rendered, self.rest_content)
|