Browse Source

fix #696: de-linkfy links in code blocks

Rafał Pitoń 8 years ago
parent
commit
0dcf6e1167
2 changed files with 30 additions and 1 deletions
  1. 15 1
      misago/markup/parser.py
  2. 15 0
      misago/markup/tests/test_parser.py

+ 15 - 1
misago/markup/parser.py

@@ -1,5 +1,7 @@
 from __future__ import unicode_literals
 from __future__ import unicode_literals
 
 
+import warnings
+
 import bleach
 import bleach
 import markdown
 import markdown
 from bs4 import BeautifulSoup
 from bs4 import BeautifulSoup
@@ -134,7 +136,19 @@ def md_factory(allow_links=True, allow_images=True, allow_blocks=True):
 
 
 
 
 def linkify_paragraphs(result):
 def linkify_paragraphs(result):
-    result['parsed_text'] = bleach.linkify(result['parsed_text'], skip_pre=True, parse_email=True)
+    result['parsed_text'] = bleach.linkify(
+        result['parsed_text'], skip_pre=True, parse_email=True)
+
+    # dirty fix for
+    if '<code>' in result['parsed_text'] and '<a' in result['parsed_text']:
+        with warnings.catch_warnings():
+            warnings.simplefilter("ignore")
+
+            soup = BeautifulSoup(result['parsed_text'], 'html5lib')
+            for link in soup.select('code > a'):
+                link.replace_with(BeautifulSoup(link.string, 'html.parser'))
+            # [6:-7] trims <body></body> wrap
+            result['parsed_text'] = six.text_type(soup.body)[6:-7]
 
 
 
 
 def clean_links(request, result, force_shva=False):
 def clean_links(request, result, force_shva=False):

+ 15 - 0
misago/markup/tests/test_parser.py

@@ -255,6 +255,21 @@ Lorem ipsum: http://somewhere.com/somewhere-something/
         self.assertEqual(expected_result, result['parsed_text'])
         self.assertEqual(expected_result, result['parsed_text'])
 
 
 
 
+class LinkifyTests(TestCase):
+    def test_clean_current_link(self):
+        """clean_links step cleans http://test.com"""
+        test_text = """
+Lorem ipsum: `<http://test.com>`
+""".strip()
+
+        expected_result = """
+<p>Lorem ipsum: <code>&lt;http://test.com&gt;</code></p>
+""".strip()
+
+        result = parse(test_text, MockRequest(), MockPoster(), minify=True)
+        self.assertEqual(expected_result, result['parsed_text'])
+
+
 class StriketroughTests(TestCase):
 class StriketroughTests(TestCase):
     def test_striketrough(self):
     def test_striketrough(self):
         """striketrough markdown deletes test"""
         """striketrough markdown deletes test"""