~netlandish/django-wiki

e041a77cc9cf60fe7bce435cd19d7e2b1c662263 — Branko Majic 6 years ago 9199757
Enforce the internal urlize markdown extensions works only with links stand as "separate words":

- Introduced additional tests to ensure links are rendered correctly
  when in middle of text.
- Introduced additional tests to ensure links are not rendered in case
  of invalid patterns.
- Ensure the link pattern is matched only for standalone words (with
  "spacing" around the links).
2 files changed, 28 insertions(+), 1 deletions(-)

M src/wiki/plugins/links/mdx/urlize.py
M tests/plugins/links/test_urlize.py
M src/wiki/plugins/links/mdx/urlize.py => src/wiki/plugins/links/mdx/urlize.py +2 -1
@@ 91,7 91,8 @@ class UrlizePattern(markdown.inlinepatterns.Pattern):
        to standard matching flags added by parent class.
        """

        return re.compile(r'^(.*?)%s(.*?)$' % URLIZE_RE, re.DOTALL | re.UNICODE | re.IGNORECASE)
        # Ensure links are matched only if they stand on their own to avoid bad matches etc.
        return re.compile(r'^(|.*?\s)%s(\s.*?|)$' % URLIZE_RE, re.DOTALL | re.UNICODE | re.IGNORECASE)

    def handleMatch(self, m):
        """

M tests/plugins/links/test_urlize.py => tests/plugins/links/test_urlize.py +26 -0
@@ 120,6 120,24 @@ FIXTURE_POSITIVE_MATCHES = [
        'http://example.com/my/path?param1=value1&param2=value2',
        EXPECTED_PARAGRAPH_TEMPLATE % ('http://example.com/my/path?param1=value1&param2=value2', 'http://example.com/my/path?param1=value1&param2=value2')
    ),

    # Link positioned somewhere within the text, but around whitespace boundary.
    (
        'This is link myhost.example.com',
        "<p>This is link " + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + "</p>"
    ),
    (
        'myhost.example.com is the link',
        "<p>" + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + " is the link</p>"
    ),
    (
        'I have best myhost.example.com link ever',
        "<p>I have best " + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + " link ever</p>"
    ),
    (
        'I have best\nmyhost.example.com link ever',
        "<p>I have best\n" + EXPECTED_LINK_TEMPLATE % ('http://myhost.example.com', 'myhost.example.com') + " link ever</p>"
    ),
]




@@ 139,6 157,14 @@ FIXTURE_NEGATIVE_MATCHES = [
        'example-.com',
        '<p>example-.com</p>'
    ),
    (
        '-example.com',
        '<p>-example.com</p>'
    ),
    (
        'my.-example.com',
        '<p>my.-example.com</p>'
    ),
]