diff options
author | luxagraf <sng@luxagraf.net> | 2023-07-28 13:39:02 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2023-07-28 13:39:02 -0500 |
commit | 9a620cf42bf1fe6977e378bd834b41ff4a593dde (patch) | |
tree | cf41a0582681cecaf88a30bfe409f9c2be57972a /bak/unused_apps/ccg_notes/mdx_urlize.py | |
parent | 6e5897117124cd60ae81efb1574c6347f48e60e5 (diff) |
main: removed some apps I wasn't using and added bak to git to preserve
a copy of old apps
Diffstat (limited to 'bak/unused_apps/ccg_notes/mdx_urlize.py')
-rw-r--r-- | bak/unused_apps/ccg_notes/mdx_urlize.py | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/bak/unused_apps/ccg_notes/mdx_urlize.py b/bak/unused_apps/ccg_notes/mdx_urlize.py new file mode 100644 index 0000000..dc8d1d7 --- /dev/null +++ b/bak/unused_apps/ccg_notes/mdx_urlize.py @@ -0,0 +1,81 @@ +"""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)\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(configs=None): + return UrlizeExtension(configs=configs) + +if __name__ == "__main__": + import doctest + doctest.testmod() |