Skip to content

Commit 7ba0dca

Browse files
committed
Why did I create a separate branch for 2.6 again?
1 parent d4cbf44 commit 7ba0dca

1 file changed

Lines changed: 48 additions & 22 deletions

File tree

textile/functions.py

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,31 @@
2020

2121
import re
2222
import uuid
23-
from sys import maxunicode
24-
import urlparse
25-
import urllib
26-
import HTMLParser
23+
from sys import maxunicode, version_info
2724

2825
from textile.tools import sanitizer, imagesize
2926

30-
# We're going to use the Python 2.7+ OrderedDict data type. This has been
31-
# included in textile.tools since it won't be available on Python 2.6
32-
from textile.tools import OrderedDict
27+
28+
# We're going to use the Python 2.7+ OrderedDict data type. Import it if it's
29+
# available, otherwise, use the included tool.
30+
try:
31+
from collections import OrderedDict
32+
except ImportError:
33+
from textile.tools import OrderedDict
34+
35+
36+
try:
37+
# Python 3
38+
from urllib.parse import urlparse, urlsplit, urlunsplit, quote, unquote
39+
from html.parser import HTMLParser
40+
xrange = range
41+
unichr = chr
42+
unicode = str
43+
except (ImportError):
44+
# Python 2
45+
from urllib import quote, unquote
46+
from urlparse import urlparse, urlsplit, urlunsplit
47+
from HTMLParser import HTMLParser
3348

3449

3550
def _normalize_newlines(string):
@@ -218,8 +233,10 @@ def textile(self, text, rel=None, head_offset=0, html_type='xhtml',
218233
# characters.
219234
# we need to know if there are unicode charcters in the text.
220235
# return True as soon as a unicode character is found, else, False
236+
# Python 3 is better equipped to handle unicode data, so this hackery
237+
# is version-specific.
221238
self.text_has_unicode = next((True for c in text if ord(c) > 128),
222-
False)
239+
False) and version_info < (3,)
223240

224241
if self.text_has_unicode:
225242
uppers = []
@@ -463,7 +480,10 @@ def fTable(self, match):
463480
colgrp, last_rgrp = '', ''
464481
c_row = 1
465482
rows = []
466-
split = re.compile(r'\|\s*?$', re.M).split(match.group(3))
483+
try:
484+
split = re.split(r'\|\s*?$', match.group(3), flags=re.M)
485+
except TypeError:
486+
split = re.compile(r'\|\s*?$', re.M).split(match.group(3))
467487
for row in [x for x in split if x]:
468488
row = row.lstrip()
469489

@@ -598,7 +618,10 @@ def lists(self, text):
598618
return pattern.sub(self.fList, bullet_pattern.sub('*', text))
599619

600620
def fList(self, match):
601-
text = re.compile(r'\n(?=[*#;:])', re.M).split(match.group())
621+
try:
622+
text = re.split(r'\n(?=[*#;:])', match.group(), flags=re.M)
623+
except TypeError:
624+
text = re.compile(r'\n(?=[*#;:])', re.M).split(match.group())
602625
pt = ''
603626
result = []
604627
ls = OrderedDict()
@@ -679,7 +702,7 @@ def fList(self, match):
679702
if len(nl) <= len(tl):
680703
line = line + ("</%s>" % litem if showitem else '')
681704
# work backward through the list closing nested lists/items
682-
for k, v in reversed(ls.items()):
705+
for k, v in reversed(list(ls.items())):
683706
if len(k) > len(nl):
684707
if v != 2:
685708
line = line + "\n\t</%sl>" % self.listType(k)
@@ -1032,7 +1055,7 @@ def isRelURL(self, url):
10321055
True
10331056
10341057
"""
1035-
(scheme, netloc) = urlparse.urlparse(url)[0:2]
1058+
(scheme, netloc) = urlparse(url)[0:2]
10361059
return not scheme and not netloc
10371060

10381061
def relURL(self, url):
@@ -1045,7 +1068,7 @@ def relURL(self, url):
10451068
'#'
10461069
10471070
"""
1048-
scheme = urlparse.urlparse(url)[0]
1071+
scheme = urlparse(url)[0]
10491072
if self.restricted and scheme and scheme not in self.url_schemes:
10501073
return '#'
10511074
return url
@@ -1214,7 +1237,7 @@ def encode_url(self, url):
12141237
url = url.decode('utf8')
12151238

12161239
# parse it
1217-
parsed = urlparse.urlsplit(url)
1240+
parsed = urlsplit(url)
12181241

12191242
# divide the netloc further
12201243
netloc_pattern = re.compile(r"""
@@ -1226,17 +1249,17 @@ def encode_url(self, url):
12261249

12271250
# encode each component
12281251
scheme = parsed.scheme
1229-
user = netloc_parsed['user'] and urllib.quote(netloc_parsed['user'])
1252+
user = netloc_parsed['user'] and quote(netloc_parsed['user'])
12301253
password = (netloc_parsed['password'] and
1231-
urllib.quote(netloc_parsed['password']))
1254+
quote(netloc_parsed['password']))
12321255
host = netloc_parsed['host']
12331256
port = netloc_parsed['port'] and netloc_parsed['port']
12341257
path = '/'.join( # could be encoded slashes!
1235-
urllib.quote(urllib.unquote(pce).encode('utf8'), '')
1258+
quote(unquote(pce).encode('utf8'), '')
12361259
for pce in parsed.path.split('/')
12371260
)
1238-
query = urllib.quote(urllib.unquote(parsed.query).encode('utf8'), '=&?/')
1239-
fragment = urllib.quote(urllib.unquote(parsed.fragment).encode('utf8'))
1261+
query = quote(unquote(parsed.query), '=&?/')
1262+
fragment = quote(unquote(parsed.fragment))
12401263

12411264
# put it back together
12421265
netloc = ''
@@ -1248,7 +1271,7 @@ def encode_url(self, url):
12481271
netloc += host
12491272
if port:
12501273
netloc += ':'+port
1251-
return urlparse.urlunsplit((scheme, netloc, path, query, fragment))
1274+
return urlunsplit((scheme, netloc, path, query, fragment))
12521275

12531276
def span(self, text):
12541277
"""
@@ -1435,7 +1458,10 @@ def redcloth_list(self, text):
14351458
def fRCList(self, match):
14361459
"""Format a definition list."""
14371460
out = []
1438-
text = re.compile(r'\n(?=[-])', re.M).split(match.group())
1461+
try:
1462+
text = re.split(r'\n(?=[-])', match.group(), flags=re.M)
1463+
except TypeError:
1464+
text = re.compile(r'\n(?=[-])', re.M).split(match.group())
14391465
for line in text:
14401466
# parse the attributes and content
14411467
m = re.match(r'^[-]+(%s)[ .](.*)$' % self.lc, line, re.M | re.S)
@@ -1634,7 +1660,7 @@ def encode_high(self, text):
16341660

16351661
def decode_high(self, text):
16361662
"""Decode encoded HTML entities."""
1637-
h = HTMLParser.HTMLParser()
1663+
h = HTMLParser()
16381664
text = '&#%s;' % text
16391665
return h.unescape(text)
16401666

0 commit comments

Comments
 (0)