Browse Source

further tweaks in parser: handle shva=1 flag on attachment previews

Rafał Pitoń 8 years ago
parent
commit
a5c579c879
2 changed files with 37 additions and 10 deletions
  1. 20 6
      misago/markup/parser.py
  2. 17 4
      misago/markup/tests/test_parser.py

+ 20 - 6
misago/markup/parser.py

@@ -17,6 +17,9 @@ from .pipeline import pipeline
 
 
 __all__ = ['parse']
 __all__ = ['parse']
 
 
+MISAGO_ATTACHMENT_VIEWS = ('misago:attachment', 'misago:attachment-thumbnail')
+
+
 
 
 def parse(text, request, poster, allow_mentions=True, allow_links=True,
 def parse(text, request, poster, allow_mentions=True, allow_links=True,
           allow_images=True, allow_blocks=True, force_shva=False, minify=True):
           allow_images=True, allow_blocks=True, force_shva=False, minify=True):
@@ -127,13 +130,8 @@ def clean_links(request, result, force_shva=False):
     for link in soup.find_all('a'):
     for link in soup.find_all('a'):
         if is_internal_link(link['href'], host):
         if is_internal_link(link['href'], host):
             link['href'] = clean_internal_link(link['href'], host)
             link['href'] = clean_internal_link(link['href'], host)
-            if force_shva:
-                try:
-                    resolution = resolve(link['href'])
-                    print resolution
-                except (Http404, ValueError):
-                    pass
             result['inside_links'].append(link['href'])
             result['inside_links'].append(link['href'])
+            link['href'] = clean_attachment_link(link['href'], force_shva)
         else:
         else:
             result['outgoing_links'].append(link['href'])
             result['outgoing_links'].append(link['href'])
 
 
@@ -145,6 +143,7 @@ def clean_links(request, result, force_shva=False):
         if is_internal_link(img['src'], host):
         if is_internal_link(img['src'], host):
             img['src'] = clean_internal_link(img['src'], host)
             img['src'] = clean_internal_link(img['src'], host)
             result['images'].append(img['src'])
             result['images'].append(img['src'])
+            img['src'] = clean_attachment_link(img['src'], force_shva)
         else:
         else:
             result['images'].append(img['src'])
             result['images'].append(img['src'])
 
 
@@ -184,6 +183,21 @@ def clean_internal_link(link, host):
     return link or '/'
     return link or '/'
 
 
 
 
+def clean_attachment_link(link, force_shva=False):
+    try:
+        resolution = resolve(link)
+        url_name = ':'.join(resolution.namespaces + [resolution.url_name])
+    except (Http404, ValueError):
+        return link
+
+    if url_name in MISAGO_ATTACHMENT_VIEWS:
+        if force_shva:
+            link = '{}?shva=1'.format(link)
+        elif link.endswith('?shva=1'):
+            link = link[:-7]
+    return link
+
+
 def minify_result(result):
 def minify_result(result):
     # [25:-14] trims <html><head></head><body> and </body></html>
     # [25:-14] trims <html><head></head><body> and </body></html>
     result['parsed_text'] = html_minify(result['parsed_text'].encode('utf-8'))
     result['parsed_text'] = html_minify(result['parsed_text'].encode('utf-8'))

+ 17 - 4
misago/markup/tests/test_parser.py

@@ -218,7 +218,7 @@ Lorem ipsum: http://somewhere.com/somewhere-something/
         """parser handles image element nested in link"""
         """parser handles image element nested in link"""
         test_text = """
         test_text = """
 [![3.png](http://test.com/attachment/thumb/test-43/)](http://test.com/attachment/test-43/)
 [![3.png](http://test.com/attachment/thumb/test-43/)](http://test.com/attachment/test-43/)
-        """
+        """.strip()
 
 
         expected_result = """
         expected_result = """
 <p><a href="/attachment/test-43/" rel="nofollow"><img alt="3.png" src="/attachment/thumb/test-43/"/></a></p>
 <p><a href="/attachment/test-43/" rel="nofollow"><img alt="3.png" src="/attachment/thumb/test-43/"/></a></p>
@@ -228,14 +228,27 @@ Lorem ipsum: http://somewhere.com/somewhere-something/
         self.assertEqual(expected_result, result['parsed_text'])
         self.assertEqual(expected_result, result['parsed_text'])
 
 
     def test_force_shva(self):
     def test_force_shva(self):
-        """parser appends ?shva=1 bit to attachment links"""
+        """parser appends ?shva=1 bit to attachment links if flag is present"""
         test_text = """
         test_text = """
-            ![3.png](http://test.com/attachment/thumb/test-43/)
-        """
+![3.png](http://test.com/attachment/thumb/test-43/)
+        """.strip()
 
 
         expected_result = """
         expected_result = """
 <p><img alt="3.png" src="/attachment/thumb/test-43/?shva=1"/></p>
 <p><img alt="3.png" src="/attachment/thumb/test-43/?shva=1"/></p>
 """.strip()
 """.strip()
 
 
+        result = parse(test_text, MockRequest(), MockPoster(), minify=True, force_shva=True)
+        self.assertEqual(expected_result, result['parsed_text'])
+
+    def test_remove_shva(self):
+        """parser removes ?shva=1 bit from attachment links if flag is absent"""
+        test_text = """
+![3.png](http://test.com/attachment/thumb/test-43/?shva=1)
+        """.strip()
+
+        expected_result = """
+<p><img alt="3.png" src="/attachment/thumb/test-43/"/></p>
+""".strip()
+
         result = parse(test_text, MockRequest(), MockPoster(), minify=True)
         result = parse(test_text, MockRequest(), MockPoster(), minify=True)
         self.assertEqual(expected_result, result['parsed_text'])
         self.assertEqual(expected_result, result['parsed_text'])