Browse Source

#122: url bbcode

Rafał Pitoń 8 years ago
parent
commit
8752fe985e
3 changed files with 52 additions and 5 deletions
  1. 29 4
      misago/markup/bbcode/inline.py
  2. 4 1
      misago/markup/parser.py
  3. 19 0
      misago/markup/tests/test_parser.py

+ 29 - 4
misago/markup/bbcode/inline.py

@@ -3,7 +3,8 @@ Supported inline BBCodes: b, u, i
 """
 import re
 
-from markdown.inlinepatterns import IMAGE_LINK_RE, ImagePattern, SimpleTagPattern, util
+from markdown.inlinepatterns import (
+    IMAGE_LINK_RE, ImagePattern, LinkPattern, SimpleTagPattern, dequote, util)
 
 
 class SimpleBBCodePattern(SimpleTagPattern):
@@ -70,6 +71,30 @@ def image(md):
     return BBCodeImagePattern(IMAGE_PATTERN, md)
 
 
-# todo: URL
-# note: can't just replace url's bbcode with md cos:
-# [url=http://onet.pl][1][/url] => [[1]](http://onet.pl)
+class BBCodeUrlPattern(BBcodePattern, LinkPattern):
+    def handleMatch(self, m):
+        el = util.etree.Element("a")
+
+        if m.group(8):
+            el.text = m.group(8).strip()
+            title = m.group(8).strip()
+            href = m.group(5)
+        else:
+            el.text = m.group(3)
+            title = m.group(3)
+            href = m.group(3)
+
+        if href:
+            if href[0] == "<":
+                href = href[1:-1]
+            el.set("href", self.sanitize_url(self.unescape(href.strip())))
+        else:
+            el.set("href", "")
+        return el
+
+
+URL_PATTERN = r'((\[url=("?)(.*?)("?)\])|(\[url\]))(.*?)\[/url\]'
+
+
+def url(md):
+    return BBCodeUrlPattern(URL_PATTERN, md)

+ 4 - 1
misago/markup/parser.py

@@ -100,7 +100,10 @@ def md_factory(allow_links=True, allow_images=True, allow_blocks=True):
     striketrough_md = StriketroughExtension()
     striketrough_md.extendMarkdown(md)
 
-    if not allow_links:
+    if allow_links:
+        # Add [url]
+        md.inlinePatterns.add('bb_url', inline.url(md), '<link')
+    else:
         # Remove links
         del md.inlinePatterns['link']
         del md.inlinePatterns['autolink']

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

@@ -96,6 +96,25 @@ Lorem ipsum !(https://placekitten.com/g/1200/500)
         result = parse(test_text, MockRequest(), MockPoster(), minify=False)
         self.assertEqual(expected_result, result['parsed_text'])
 
+    def test_url(self):
+        """url bbcode is correctly parsed"""
+        test_text = """
+Lorem ipsum [url]https://placekitten.com/g/600/600[/url]
+
+Lorem ipsum [uRL=https://placekitten.com/g/400/400"]Label text![/UrL]
+
+Lorem ipsum [Lorem ipsum](https://placekitten.com/g/1200/500)
+""".strip()
+
+        expected_result = """
+<p>Lorem ipsum <a href="" rel="nofollow">placekitten.com/g/600/600</a></p>
+<p>Lorem ipsum <a href="https://placekitten.com/g/400/400" rel="nofollow">Label text!</a></p>
+<p>Lorem ipsum <a href="https://placekitten.com/g/1200/500" rel="nofollow">Lorem ipsum</a></p>
+""".strip()
+
+        result = parse(test_text, MockRequest(), MockPoster(), minify=False)
+        self.assertEqual(expected_result, result['parsed_text'])
+
 
 class MinifyTests(TestCase):
     def test_minified_text(self):