1 # Quick tests for the markup templatetags (django.contrib.markup) |
|
2 |
|
3 from django.template import Template, Context, add_to_builtins |
|
4 import re |
|
5 import unittest |
|
6 |
|
7 add_to_builtins('django.contrib.markup.templatetags.markup') |
|
8 |
|
9 class Templates(unittest.TestCase): |
|
10 def test_textile(self): |
|
11 try: |
|
12 import textile |
|
13 except ImportError: |
|
14 textile = None |
|
15 |
|
16 textile_content = """Paragraph 1 |
|
17 |
|
18 Paragraph 2 with "quotes" and @code@""" |
|
19 |
|
20 t = Template("{{ textile_content|textile }}") |
|
21 rendered = t.render(Context(locals())).strip() |
|
22 if textile: |
|
23 self.assertEqual(rendered, """<p>Paragraph 1</p> |
|
24 |
|
25 <p>Paragraph 2 with “quotes” and <code>code</code></p>""") |
|
26 else: |
|
27 self.assertEqual(rendered, textile_content) |
|
28 |
|
29 def test_markdown(self): |
|
30 try: |
|
31 import markdown |
|
32 except ImportError: |
|
33 markdown = None |
|
34 |
|
35 markdown_content = """Paragraph 1 |
|
36 |
|
37 ## An h2""" |
|
38 |
|
39 t = Template("{{ markdown_content|markdown }}") |
|
40 rendered = t.render(Context(locals())).strip() |
|
41 if markdown: |
|
42 pattern = re.compile("""<p>Paragraph 1\s*</p>\s*<h2>\s*An h2</h2>""") |
|
43 self.assert_(pattern.match(rendered)) |
|
44 else: |
|
45 self.assertEqual(rendered, markdown_content) |
|
46 |
|
47 def test_docutils(self): |
|
48 try: |
|
49 import docutils |
|
50 except ImportError: |
|
51 docutils = None |
|
52 |
|
53 rest_content = """Paragraph 1 |
|
54 |
|
55 Paragraph 2 with a link_ |
|
56 |
|
57 .. _link: http://www.example.com/""" |
|
58 |
|
59 t = Template("{{ rest_content|restructuredtext }}") |
|
60 rendered = t.render(Context(locals())).strip() |
|
61 if docutils: |
|
62 self.assertEqual(rendered, """<p>Paragraph 1</p> |
|
63 <p>Paragraph 2 with a <a class="reference" href="http://www.example.com/">link</a></p>""") |
|
64 else: |
|
65 self.assertEqual(rendered, rest_content) |
|
66 |
|
67 |
|
68 if __name__ == '__main__': |
|
69 unittest.main() |
|