2005-08-11 02:31:33 +08:00
|
|
|
# Quick tests for the markup templatetags (django.contrib.markup)
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.template import Template, Context, add_to_builtins
|
2006-07-22 10:47:17 +08:00
|
|
|
import re
|
2005-11-27 06:46:31 +08:00
|
|
|
|
|
|
|
add_to_builtins('django.contrib.markup.templatetags.markup')
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
# find out if markup modules are installed and tailor the test appropriately
|
|
|
|
try:
|
|
|
|
import textile
|
|
|
|
except ImportError:
|
|
|
|
textile = None
|
2005-11-27 06:46:31 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
try:
|
|
|
|
import markdown
|
|
|
|
except ImportError:
|
|
|
|
markdown = None
|
2005-11-27 06:46:31 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
try:
|
|
|
|
import docutils
|
|
|
|
except ImportError:
|
|
|
|
docutils = None
|
|
|
|
|
2005-08-11 02:31:33 +08:00
|
|
|
# simple examples 'cause this isn't actually testing the markup, just
|
|
|
|
# that the filters work as advertised
|
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
### test textile
|
|
|
|
|
2005-08-11 02:31:33 +08:00
|
|
|
textile_content = """Paragraph 1
|
|
|
|
|
|
|
|
Paragraph 2 with "quotes" and @code@"""
|
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
t = Template("{{ textile_content|textile }}")
|
|
|
|
rendered = t.render(Context(locals())).strip()
|
|
|
|
if textile:
|
|
|
|
assert rendered == """<p>Paragraph 1</p>
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
<p>Paragraph 2 with “quotes” and <code>code</code></p>"""
|
|
|
|
else:
|
|
|
|
assert rendered == textile_content
|
2005-11-27 06:46:31 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
### test markdown
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
markdown_content = """Paragraph 1
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
## An h2"""
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
t = Template("{{ markdown_content|markdown }}")
|
|
|
|
rendered = t.render(Context(locals())).strip()
|
2006-04-13 21:16:05 +08:00
|
|
|
if markdown:
|
2006-07-22 10:47:17 +08:00
|
|
|
pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""")
|
|
|
|
assert pattern.match(rendered)
|
2005-08-12 02:32:19 +08:00
|
|
|
else:
|
|
|
|
assert rendered == markdown_content
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
### test rest
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
rest_content = """Paragraph 1
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
Paragraph 2 with a link_
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
.. _link: http://www.example.com/"""
|
2005-08-11 02:31:33 +08:00
|
|
|
|
2005-08-12 02:32:19 +08:00
|
|
|
t = Template("{{ rest_content|restructuredtext }}")
|
|
|
|
rendered = t.render(Context(locals())).strip()
|
|
|
|
if docutils:
|
|
|
|
assert rendered =="""<p>Paragraph 1</p>
|
|
|
|
<p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>"""
|
|
|
|
else:
|
2005-11-27 06:46:31 +08:00
|
|
|
assert rendered == rest_content
|