Mercurial > p > roundup > code
comparison test/test_templating.py @ 7561:91725f12b239
Support markdown2 2.4.10, 2.4.8- and exclude 2.4.9
Handle these changes to markdown2
version 2.4.9 broke links like (issue1)[issue1]: raise error if used
Version 2.4.10 changed how filtering of schemes is done: adapt to new
method
Mail url's in markdown are formatted
[label](mailto:user@something.com). The markdown format wrapper uses
the plain text formatter to turn issue1 and user@something.com into
markdown formatted strings to be htmlized by the markdown formatters.
However when the plain text formatter saw (mailto:user@something.com)
it made it (mailto:<user@something.com>). This is broken as the enamil
address shouldn't have the angle brackets. By modifying the email
pattern to include an optional mailto:, all three markdown formatters
do the right thing and I don't end up with href="<user@something.com>"
in the link.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 23 Jul 2023 16:50:35 -0400 |
| parents | f529cca242dc |
| children | 7bdf9c0f74b9 |
comparison
equal
deleted
inserted
replaced
| 7560:5cadcaa13bed | 7561:91725f12b239 |
|---|---|
| 8 from .html_norm import NormalizingHtmlParser | 8 from .html_norm import NormalizingHtmlParser |
| 9 | 9 |
| 10 | 10 |
| 11 import pytest | 11 import pytest |
| 12 from .pytest_patcher import mark_class | 12 from .pytest_patcher import mark_class |
| 13 | |
| 14 from markdown2 import __version_info__ as md2__version_info__ | |
| 13 | 15 |
| 14 if ReStructuredText: | 16 if ReStructuredText: |
| 15 skip_rst = lambda func, *args, **kwargs: func | 17 skip_rst = lambda func, *args, **kwargs: func |
| 16 else: | 18 else: |
| 17 skip_rst = mark_class(pytest.mark.skip( | 19 skip_rst = mark_class(pytest.mark.skip( |
| 772 def test_string_markdown(self): | 774 def test_string_markdown(self): |
| 773 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'A string with <br> *embedded* \u00df')) | 775 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'A string with <br> *embedded* \u00df')) |
| 774 self.assertEqual(p.markdown().strip(), u2s(u'<p>A string with <br> <em>embedded</em> \u00df</p>')) | 776 self.assertEqual(p.markdown().strip(), u2s(u'<p>A string with <br> <em>embedded</em> \u00df</p>')) |
| 775 | 777 |
| 776 def test_string_markdown_link(self): | 778 def test_string_markdown_link(self): |
| 777 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'A link <http://localhost>')) | 779 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', |
| 778 self.assertEqual(p.markdown().strip(), u2s(u'<p>A link <a href="http://localhost">http://localhost</a></p>')) | 780 u2s(u'A link <http://localhost>')) |
| 781 m = p.markdown().strip() | |
| 782 m = self.mangleMarkdown2(m) | |
| 783 | |
| 784 self.assertEqual( u2s(u'<p>A link <a href="http://localhost" rel="nofollow noopener">http://localhost</a></p>'), m) | |
| 779 | 785 |
| 780 def test_string_markdown_link_item(self): | 786 def test_string_markdown_link_item(self): |
| 781 """ The link formats for the different markdown engines changes. | 787 """ The link formats for the different markdown engines changes. |
| 782 Order of attributes, value for rel (noopener, nofollow etc) | 788 Order of attributes, value for rel (noopener, nofollow etc) |
| 783 is different. So most tests check for a substring that indicates | 789 is different. So most tests check for a substring that indicates |
| 784 success rather than the entire returned string. | 790 success rather than the entire returned string. |
| 785 """ | 791 """ |
| 786 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'An issue1 link')) | 792 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', |
| 793 u2s(u'An issue1 link')) | |
| 787 self.assertIn( u2s(u'href="issue1"'), p.markdown().strip()) | 794 self.assertIn( u2s(u'href="issue1"'), p.markdown().strip()) |
| 788 # just verify that plain linking is working | 795 # just verify that plain linking is working |
| 789 self.assertIn( u2s(u'href="issue1"'), p.plain(hyperlink=1)) | 796 self.assertIn( u2s(u'href="issue1"'), p.plain(hyperlink=1)) |
| 790 | 797 |
| 791 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'An [issue1](issue1) link')) | 798 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', |
| 799 u2s(u'An [issue1](issue1) link')) | |
| 792 self.assertIn( u2s(u'href="issue1"'), p.markdown().strip()) | 800 self.assertIn( u2s(u'href="issue1"'), p.markdown().strip()) |
| 793 # just verify that plain linking is working | 801 # just verify that plain linking is working |
| 794 self.assertIn( u2s(u'href="issue1"'), p.plain(hyperlink=1)) | 802 self.assertIn( u2s(u'href="issue1"'), p.plain(hyperlink=1)) |
| 795 | 803 |
| 796 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'An [issue1](https://example.com/issue1) link')) | 804 p = StringHTMLProperty( |
| 797 self.assertIn( u2s(u'href="https://example.com/issue1"'), p.markdown().strip()) | 805 self.client, 'test', '1', None, 'test', |
| 798 | 806 u2s(u'An [issue1](https://example.com/issue1) link')) |
| 799 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'An [issue1] (https://example.com/issue1) link')) | 807 self.assertIn( u2s(u'href="https://example.com/issue1"'), |
| 808 p.markdown().strip()) | |
| 809 | |
| 810 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', | |
| 811 u2s(u'An [issu1](#example) link')) | |
| 812 self.assertIn( u2s(u'href="#example"'), p.markdown().strip()) | |
| 813 | |
| 814 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', | |
| 815 u2s(u'An [issu1](/example) link')) | |
| 816 self.assertIn( u2s(u'href="/example"'), p.markdown().strip()) | |
| 817 | |
| 818 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', | |
| 819 u2s(u'An [issu1](./example) link')) | |
| 820 self.assertIn( u2s(u'href="./example"'), p.markdown().strip()) | |
| 821 | |
| 822 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', | |
| 823 u2s(u'An [issu1](../example) link')) | |
| 824 self.assertIn( u2s(u'href="../example"'), p.markdown().strip()) | |
| 825 | |
| 826 p = StringHTMLProperty( | |
| 827 self.client, 'test', '1', None, 'test', | |
| 828 u2s(u'A [wuarchive_ftp](ftp://www.wustl.gov/file) link')) | |
| 829 self.assertIn( u2s(u'href="ftp://www.wustl.gov/file"'), | |
| 830 p.markdown().strip()) | |
| 831 | |
| 832 p = StringHTMLProperty( | |
| 833 self.client, 'test', '1', None, 'test', | |
| 834 u2s(u'An [issue1] (https://example.com/issue1) link')) | |
| 800 self.assertIn( u2s(u'href="issue1"'), p.markdown().strip()) | 835 self.assertIn( u2s(u'href="issue1"'), p.markdown().strip()) |
| 801 if type(self) == MistuneTestCase: | 836 if type(self) == MistuneTestCase: |
| 802 # mistune makes the https url into a real link | 837 # mistune makes the https url into a real link |
| 803 self.assertIn( u2s(u'href="https://example.com/issue1"'), p.markdown().strip()) | 838 self.assertIn( u2s(u'href="https://example.com/issue1"'), |
| 839 p.markdown().strip()) | |
| 804 else: | 840 else: |
| 805 # the other two engines leave the parenthesized url as is. | 841 # the other two engines leave the parenthesized url as is. |
| 806 self.assertIn( u2s(u' (https://example.com/issue1) link'), p.markdown().strip()) | 842 self.assertIn( u2s(u' (https://example.com/issue1) link'), |
| 807 | 843 p.markdown().strip()) |
| 808 def test_string_markdown_link(self): | 844 |
| 845 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', | |
| 846 u2s(u'An [issu1](.../example) link')) | |
| 847 if (isinstance(self, Markdown2TestCase) and | |
| 848 md2__version_info__ > (2, 4, 9)): | |
| 849 # markdown2 > 2.4.9 handles this differently | |
| 850 self.assertIn( u2s(u'href="#"'), p.markdown().strip()) | |
| 851 else: | |
| 852 self.assertIn( u2s(u'href=".../example"'), p.markdown().strip()) | |
| 853 | |
| 854 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', | |
| 855 u2s(u'A [phone](tel:0016175555555) link')) | |
| 856 if (isinstance(self, Markdown2TestCase) and | |
| 857 md2__version_info__ > (2, 4, 9)): | |
| 858 self.assertIn(u2s(u'href="#"'), p.markdown().strip()) | |
| 859 else: | |
| 860 self.assertIn( u2s(u'href="tel:0016175555555"'), | |
| 861 p.markdown().strip()) | |
| 862 | |
| 863 def test_string_email_markdown_link(self): | |
| 809 # markdown2 and markdown escape the email address | 864 # markdown2 and markdown escape the email address |
| 810 try: | 865 try: |
| 811 from html import unescape as html_unescape | 866 from html import unescape as html_unescape |
| 812 except ImportError: | 867 except ImportError: |
| 813 from HTMLParser import HTMLParser | 868 from HTMLParser import HTMLParser |
| 814 html_unescape = HTMLParser().unescape | 869 html_unescape = HTMLParser().unescape |
| 815 | 870 |
| 816 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'A link <cmeerw@example.com>')) | 871 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', |
| 872 u2s(u'A link <cmeerw@example.com>')) | |
| 817 m = html_unescape(p.markdown().strip()) | 873 m = html_unescape(p.markdown().strip()) |
| 818 m = self.mangleMarkdown2(m) | 874 m = self.mangleMarkdown2(m) |
| 819 | 875 |
| 820 self.assertEqual(m, u2s(u'<p>A link <a href="mailto:cmeerw@example.com">cmeerw@example.com</a></p>')) | 876 self.assertEqual(m, u2s(u'<p>A link <a href="mailto:cmeerw@example.com">cmeerw@example.com</a></p>')) |
| 877 | |
| 878 p = StringHTMLProperty( | |
| 879 self.client, 'test', '1', None, 'test', | |
| 880 u2s(u'An bare email baduser@daemons.com link')) | |
| 881 m = self.mangleMarkdown2(html_unescape(p.markdown().strip())) | |
| 882 self.assertIn( u2s(u'href="mailto:baduser@daemons.com"'), | |
| 883 m) | |
| 884 | |
| 885 p = StringHTMLProperty( | |
| 886 self.client, 'test', '1', None, 'test', | |
| 887 u2s(u'An [email_url](mailto:baduser@daemons.com) link')) | |
| 888 m = self.mangleMarkdown2(html_unescape(p.markdown().strip())) | |
| 889 | |
| 890 if isinstance(self, MistuneTestCase): | |
| 891 self.assertIn('<a href="mailto:baduser@daemons.com" rel="nofollow noopener">email_url</a>', m) | |
| 892 else: | |
| 893 self.assertIn('<a href="mailto:baduser@daemons.com">email_url</a>', m) | |
| 821 | 894 |
| 822 def test_string_markdown_javascript_link(self): | 895 def test_string_markdown_javascript_link(self): |
| 823 # make sure we don't get a "javascript:" link | 896 # make sure we don't get a "javascript:" link |
| 824 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'<javascript:alert(1)>')) | 897 p = StringHTMLProperty(self.client, 'test', '1', None, 'test', u2s(u'<javascript:alert(1)>')) |
| 825 self.assertTrue(p.markdown().find('href="javascript:') == -1) | 898 self.assertTrue(p.markdown().find('href="javascript:') == -1) |
| 864 parser.reset() | 937 parser.reset() |
| 865 print(m) | 938 print(m) |
| 866 if type(self) == MistuneTestCase: | 939 if type(self) == MistuneTestCase: |
| 867 self.assertEqual(m, parser.normalize('<p>embedded code block <pre></p>\n<pre><code class="lang-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>')) | 940 self.assertEqual(m, parser.normalize('<p>embedded code block <pre></p>\n<pre><code class="lang-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>')) |
| 868 elif type(self) == MarkdownTestCase: | 941 elif type(self) == MarkdownTestCase: |
| 869 self.assertEqual(m, parser.normalize('<p>embedded code block <pre></p>\n<pre><code class="language-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>')) | 942 self.assertEqual(m.replace('class="python"','class="language-python"'), parser.normalize('<p>embedded code block <pre></p>\n<pre><code class="language-python">line 1\nline 2\n</code></pre>\n<p>new </pre> paragraph</p>')) |
| 870 else: | 943 else: |
| 871 expected_result = parser.normalize('<p>embedded code block <pre></p>\n<div class="codehilite"><pre><span></span><code><span class="n">line</span> <span class="mi">1</span>\n<span class="n">line</span> <span class="mi">2</span>\n</code></pre></div>\n<p>new </pre> paragraph</p>') | 944 expected_result = parser.normalize('<p>embedded code block <pre></p>\n<div class="codehilite"><pre><span></span><code><span class="n">line</span> <span class="mi">1</span>\n<span class="n">line</span> <span class="mi">2</span>\n</code></pre></div>\n<p>new </pre> paragraph</p>') |
| 872 self.assertEqual(m, expected_result) | 945 self.assertEqual(m, expected_result) |
| 873 | 946 |
| 874 def test_markdown_return_text_on_exception(self): | 947 def test_markdown_return_text_on_exception(self): |
