102 lines
3.1 KiB
Python
102 lines
3.1 KiB
Python
|
# Society Self-Service
|
||
|
# Copyright © 2018-2020 Yingtong Li (RunasSudo)
|
||
|
#
|
||
|
# This program is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU Affero General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU Affero General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU Affero General Public License
|
||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
#
|
||
|
# Adapted from:
|
||
|
# Copyright (c) 2014 Rowan Nairn. All rights reserved. Licensed under the 2-clause BSD licence
|
||
|
# https://github.com/r0wb0t/markdown-urlize
|
||
|
|
||
|
"""A more liberal autolinker
|
||
|
|
||
|
Inspired by Django's urlize function.
|
||
|
|
||
|
Positive examples:
|
||
|
|
||
|
>>> import markdown
|
||
|
>>> md = markdown.Markdown(extensions=['urlize'])
|
||
|
|
||
|
>>> md.convert('http://example.com/')
|
||
|
u'<p><a href="http://example.com/">http://example.com/</a></p>'
|
||
|
|
||
|
>>> md.convert('go to http://example.com')
|
||
|
u'<p>go to <a href="http://example.com">http://example.com</a></p>'
|
||
|
|
||
|
>>> md.convert('example.com')
|
||
|
u'<p><a href="http://example.com">example.com</a></p>'
|
||
|
|
||
|
>>> md.convert('example.net')
|
||
|
u'<p><a href="http://example.net">example.net</a></p>'
|
||
|
|
||
|
>>> md.convert('www.example.us')
|
||
|
u'<p><a href="http://www.example.us">www.example.us</a></p>'
|
||
|
|
||
|
>>> md.convert('(www.example.us/path/?name=val)')
|
||
|
u'<p>(<a href="http://www.example.us/path/?name=val">www.example.us/path/?name=val</a>)</p>'
|
||
|
|
||
|
>>> md.convert('go to <http://example.com> now!')
|
||
|
u'<p>go to <a href="http://example.com">http://example.com</a> now!</p>'
|
||
|
|
||
|
Negative examples:
|
||
|
|
||
|
>>> md.convert('del.icio.us')
|
||
|
u'<p>del.icio.us</p>'
|
||
|
|
||
|
"""
|
||
|
|
||
|
import markdown
|
||
|
|
||
|
# Global Vars
|
||
|
URLIZE_RE = '(%s)' % '|'.join([
|
||
|
r'<(?:f|ht)tps?://[^>]*>',
|
||
|
r'\b(?:f|ht)tps?://[^)<>\s]+[^.,)<>\s]',
|
||
|
r'\bwww\.[^)<>\s]+[^.,)<>\s]',
|
||
|
r'[^(<\s]+\.(?:(?:com|net|org)(?:\.au)?)\b',
|
||
|
])
|
||
|
|
||
|
class UrlizePattern(markdown.inlinepatterns.Pattern):
|
||
|
""" Return a link Element given an autolink (`http://example/com`). """
|
||
|
def handleMatch(self, m):
|
||
|
url = m.group(2)
|
||
|
|
||
|
if url.startswith('<'):
|
||
|
url = url[1:-1]
|
||
|
|
||
|
text = url
|
||
|
|
||
|
if not url.split('://')[0] in ('http','https','ftp'):
|
||
|
if '@' in url and not '/' in url:
|
||
|
url = 'mailto:' + url
|
||
|
else:
|
||
|
url = 'http://' + url
|
||
|
|
||
|
el = markdown.util.etree.Element("a")
|
||
|
el.set('href', url)
|
||
|
el.text = markdown.util.AtomicString(text)
|
||
|
return el
|
||
|
|
||
|
class UrlizeExtension(markdown.Extension):
|
||
|
""" Urlize Extension for Python-Markdown. """
|
||
|
|
||
|
def extendMarkdown(self, md, md_globals):
|
||
|
""" Replace autolink with UrlizePattern """
|
||
|
md.inlinePatterns['autolink'] = UrlizePattern(URLIZE_RE, md)
|
||
|
|
||
|
def makeExtension(*args, **kwargs):
|
||
|
return UrlizeExtension(*args, **kwargs)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
import doctest
|
||
|
doctest.testmod()
|