Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-http schemas in url refs #75

Merged
merged 5 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_getRefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ def test_getRefs():
result = t.urlrefs
expect = {'Google': 'http://www.google.com'}
assert result == expect

t2 = Textile()

result = t2.getRefs("my ftp [ftp]ftp://example.com")
expect = 'my ftp '
assert result == expect

result = t2.urlrefs
expect = {'ftp': 'ftp://example.com'}
assert result == expect
14 changes: 10 additions & 4 deletions textile/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ def __init__(self, restricted=False, lite=False, noimage=False,
else:
self.url_schemes = self.unrestricted_url_schemes

all_schemes_re_s = '|'.join([
'(?:{0})'.format(scheme)
for scheme in self.url_schemes
])
self.url_ref_regex = re.compile(
r'(?:(?<=^)|(?<=\s))\[(.+)\]\s?((?:{0}:\/\/|\/)\S+)(?=\s|$)'.format(all_schemes_re_s),
re.U
)

def parse(self, text, rel=None, sanitize=False):
"""Parse the input text as textile and return html output."""
self.notes = OrderedDict()
Expand Down Expand Up @@ -612,10 +621,7 @@ def glyphs(self, text):

def getRefs(self, text):
"""Capture and store URL references in self.urlrefs."""
pattern = re.compile(r'(?:(?<=^)|(?<=\s))\[(.+)\]((?:http(?:s?):\/\/|\/)\S+)(?=\s|$)',
re.U)
text = pattern.sub(self.refs, text)
return text
return self.url_ref_regex.sub(self.refs, text)

def refs(self, match):
flag, url = match.groups()
Expand Down