Ticket #17714: 17714_markdown_extensions_multiple_options_with_tests.diff

File 17714_markdown_extensions_multiple_options_with_tests.diff, 3.5 KB (added by scottie_000, 4 years ago)

Updated original patch with Markdown extension tests

  • django/contrib/markup/tests.py

     
    3030
    3131## An h2"""
    3232
     33    markdown_content_with_def_list = """Paragraph 1
     34
     35## An h2
     36
     37Apple
     38:   Pomaceous fruit of plants of the genus Malus in the family Rosaceae.
     39
     40Orange
     41:   The fruit of an evergreen tree of the genus Citrus."""
     42
    3343    rest_content = """Paragraph 1
    3444
    3545Paragraph 2 with a link_
     
    6474        rendered = t.render(Context({'markdown_content':self.markdown_content})).strip()
    6575        self.assertEqual(rendered, self.markdown_content)
    6676
     77    @unittest.skipUnless(markdown, 'markdown not installed')
     78    def test_markdown_extensions(self):
     79        # Define extensions to use as argument to markdown filter. Using
     80        # def_list and headerid here because they are included in the standard
     81        # Markdown library.
     82        extensions = "def_list,headerid(forceid=False, level=3)"
     83        t = Template(
     84            """{%% load markup %%}"""
     85            """{{ markdown_content|markdown:"%s" }}""" % extensions
     86        )
     87        c = Context({'markdown_content': self.markdown_content_with_def_list})
     88        rendered = t.render(c)
     89       
     90        # Note that the output should have <h4> and <dl> now
     91        pattern = re.compile(
     92            r"""\s*<p>Paragraph 1</p>"""
     93            """\s*<h4>An h2</h4>"""
     94            """\s*<dl>"""
     95            """\s*<dt>Apple</dt>"""
     96            """\s*<dd>.*?Malus.*?</dd>"""
     97            """\s*<dt>Orange</dt>"""
     98            """\s*<dd>.*?Citrus.</dd>"""
     99            """\s*</dl>""")
     100        self.assertTrue(pattern.match(rendered))
     101
    67102    @unittest.skipUnless(docutils, 'docutils not installed')
    68103    def test_docutils(self):
    69104        t = Template("{% load markup %}{{ rest_content|restructuredtext }}")
  • django/contrib/markup/templatetags/markup.py

     
    4343    generated by actual Markdown syntax, pass "safe" as the first
    4444    extension in the list.
    4545
     46    Extensions may be given configuration options in the form of key=value
     47    pairs, separated by commas and enclosed in parenthesis immediately after
     48    the extension name. For example:
     49
     50        {{ value|markdown:"ext_name(key1=val1, key2=val2),ext2,ext3,..." }}
     51
    4652    If the version of Markdown in use does not support extensions,
    4753    they will be silently ignored.
    4854
     
    5763        # markdown.version was first added in 1.6b. The only version of markdown
    5864        # to fully support extensions before 1.6b was the shortlived 1.6a.
    5965        if hasattr(markdown, 'version'):
    60             extensions = [e for e in arg.split(",") if e]
     66            extensions = []
     67            # Ensure that splitting into extensions by commas ignores those
     68            # commas contained within parenthesis, so that extensions are
     69            # allowed multiple configuration options.
     70            for part in arg.split("("):
     71                if ")" in part and extensions:
     72                    config, part = part.split(")")
     73                    extensions[-1] += "(%s)" % config
     74                extensions.extend(e for e in part.split(",") if e)
     75
    6176            if len(extensions) > 0 and extensions[0] == "safe":
    6277                extensions = extensions[1:]
    6378                safe_mode = True
Back to Top