Skip to content

Commit

Permalink
remove auto_link and resolve some issues in #16
Browse files Browse the repository at this point in the history
auto_link doesn't exist in php-textile.  The php version is somewhat incorrect in its handling of links, so I'll report that as an issue to them.
  • Loading branch information
ikirudennis committed Dec 11, 2015
1 parent dde6d24 commit c585f40
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 27 deletions.
5 changes: 5 additions & 0 deletions tests/test_github_issues.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import textile

def test_github_issue_16():
result = textile.textile('"$":http://google.com "$":https://google.com "$":mailto:blackhole@sun.comet')
expect = '\t<p><a href="http://google.com">google.com</a> <a href="https://google.com">google.com</a> <a href="mailto:blackhole%40sun.comet">blackhole@sun.comet</a></p>'
assert result == expect

def test_github_issue_17():
result = textile.textile('!http://www.ox.ac.uk/favicon.ico!')
expect = '\t<p><img alt="" src="http://www.ox.ac.uk/favicon.ico" /></p>'
Expand Down
4 changes: 2 additions & 2 deletions tests/test_textile.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def test_unicode_footnote():

def test_autolinking():
test = """some text "test":http://www.google.com http://www.google.com "$":http://www.google.com"""
result = """\t<p>some text <a href="http://www.google.com">test</a> <a href="http://www.google.com">www.google.com</a> <a href="http://www.google.com">www.google.com</a></p>"""
expect = textile.textile(test, auto_link=True)
result = """\t<p>some text <a href="http://www.google.com">test</a> http://www.google.com <a href="http://www.google.com">www.google.com</a></p>"""
expect = textile.textile(test)

assert result == expect

Expand Down
2 changes: 0 additions & 2 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ def test_urls():

assert t.relURL("http://www.google.com/") == 'http://www.google.com/'

assert t.autoLink("http://www.ya.ru") == '"$":http://www.ya.ru'

result = t.links('fooobar "Google":http://google.com/foobar/ and hello world "flickr":http://flickr.com/photos/jsamsa/ ')
expect = re.compile(r'fooobar {0}2:shelve and hello world {0}4:shelve'.format(t.uid))
assert expect.search(result) is not None
30 changes: 10 additions & 20 deletions textile/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,14 @@ class Textile(object):
'nl_ref_pattern': '<sup%(atts)s>%(marker)s</sup>',
}

def __init__(
self, restricted=False, lite=False, noimage=False, auto_link=False, get_sizes=False, html_type='xhtml', rel='', block_tags=True):
def __init__(self, restricted=False, lite=False, noimage=False,
get_sizes=False, html_type='xhtml', rel='', block_tags=True):
"""Textile properties that are common to regular textile and
textile_restricted"""
self.restricted = restricted
self.lite = lite
self.noimage = noimage
self.get_sizes = get_sizes
self.auto_link = auto_link
self.fn = {}
self.urlrefs = {}
self.shelf = {}
Expand Down Expand Up @@ -1091,9 +1090,6 @@ def graf(self, text):

text = self.getRefs(text)
text = self.links(text)
if self.auto_link:
text = self.autoLink(text)
text = self.links(text)

if not self.noimage:
text = self.image(text)
Expand All @@ -1110,11 +1106,6 @@ def graf(self, text):

return text.rstrip('\n')

def autoLink(self, text):
pattern = re.compile(r"""\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))""",
re.U | re.I)
return pattern.sub(r'"$":\1', text)

def links(self, text):
"""For some reason, the part of the regex below that matches the url
does not match a trailing parenthesis. It gets caught by tail, and
Expand Down Expand Up @@ -1387,7 +1378,10 @@ def _casesdefault(c, pop, popped, url_chars, counts, pre):
if url in self.urlrefs:
url = self.urlrefs[url]
text = url
text = text.split("://")[1]
if "://" in text:
text = text.split("://")[1]
else:
text = text.split(":")[1]

text = text.strip()
title = self.encode_html(title)
Expand Down Expand Up @@ -1884,35 +1878,31 @@ def cleanUniqueTokens(self, text):
return text.replace(self.uid, '')


def textile(text, html_type='xhtml', auto_link=False,
encoding=None, output=None):
def textile(text, html_type='xhtml', encoding=None, output=None):
"""
Apply Textile to a block of text.
This function takes the following additional parameters:
auto_link - enable automatic linking of URLs (default: False)
html_type - 'xhtml' or 'html5' style tags (default: 'xhtml')
"""
return Textile(auto_link=auto_link, html_type=html_type).parse(text)
return Textile(html_type=html_type).parse(text)


def textile_restricted(text, lite=True, noimage=True, html_type='xhtml',
auto_link=False):
def textile_restricted(text, lite=True, noimage=True, html_type='xhtml'):
"""
Apply Textile to a block of text, with restrictions designed for weblog
comments and other untrusted input. Raw HTML is escaped, style attributes
are disabled, and rel='nofollow' is added to external links.
This function takes the following additional parameters:
auto_link - enable automatic linking of URLs (default: False)
html_type - 'xhtml' or 'html5' style tags (default: 'xhtml')
lite - restrict block tags to p, bq, and bc, disable tables (default: True)
noimage - disable image tags (default: True)
"""
return Textile(restricted=True, lite=lite, noimage=noimage,
auto_link=auto_link, html_type=html_type, rel='nofollow').parse(
html_type=html_type, rel='nofollow').parse(
text)
4 changes: 1 addition & 3 deletions textile/textilefactory.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ class TextileFactory(object):
process multiple strings with the same settings."""

def __init__(self, restricted=False, lite=False, sanitize=False,
noimage=None, auto_link=False, get_sizes=False,
html_type='xhtml'):
noimage=None, get_sizes=False, html_type='xhtml'):

self.class_parms = {}
self.method_parms = {}
Expand All @@ -29,7 +28,6 @@ def __init__(self, restricted=False, lite=False, sanitize=False,

self.class_parms['noimage'] = noimage
self.method_parms['sanitize'] = sanitize
self.class_parms['auto_link'] = auto_link
self.class_parms['get_sizes'] = get_sizes

if html_type not in ['xhtml', 'html5']:
Expand Down

0 comments on commit c585f40

Please sign in to comment.