Skip to content

Commit 7039b39

Browse files
committed
Make regex dependency optional. Fixes #13.
1 parent ffd5c18 commit 7039b39

4 files changed

Lines changed: 25 additions & 7 deletions

File tree

README.textile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ python-textile is a Python port of Textile, Dean Allen's humane web text generat
66

77
h2. Installation
88

9-
Install the 'textile' folder on your python path, or @pip install textile@
9+
Install the 'textile' folder on your python path, or @pip install textile@.
10+
Optional dependencies require PIL/Pillow (for checking images size)
11+
and regex (for faster unicode-aware string matching).
1012

1113
h2. Usage
1214

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import sys
44

5-
install_requires = ['regex']
5+
install_requires = []
66

77
try:
88
from collections import OrderedDict

textile/core.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
2020
"""
2121

22-
import re
23-
import regex
2422
import uuid
2523

2624
from textile.tools import sanitizer, imagesize
@@ -48,6 +46,18 @@
4846
from HTMLParser import HTMLParser
4947

5048

49+
try:
50+
# Use regex module for matching uppercase characters if installed,
51+
# otherwise fall back to finding all the uppercase chars in a loop.
52+
import regex as re
53+
upper_re_s = r'\p{Lu}'
54+
except ImportError:
55+
import re
56+
from sys import maxunicode
57+
upper_re_s = "".join([unichr(c) for c in
58+
xrange(maxunicode) if unichr(c).isupper()])
59+
60+
5161
def _normalize_newlines(string):
5262
out = string.strip()
5363
out = re.sub(r'\r\n', '\n', out)
@@ -181,10 +191,10 @@ def __init__(self, restricted=False, lite=False, noimage=False,
181191
# plus/minus
182192
re.compile(r'[([]\+\/-[])]', re.I | re.U),
183193
# 3+ uppercase acronym
184-
regex.compile(r'\b([\p{Lu}][\p{Lu}0-9]{2,})\b(?:[(]([^)]*)[)])'),
194+
re.compile(r'\b([%s][%s0-9]{2,})\b(?:[(]([^)]*)[)])' % (upper_re_s, upper_re_s)),
185195
# 3+ uppercase
186-
regex.compile(r"""(?:(?<=^)|(?<=\s)|(?<=[>\(;-]))([\p{Lu}]{3,})(\w*)(?=\s|%s|$)(?=[^">]*?(<|$))""" %
187-
self.pnct_re_s),
196+
re.compile(r"""(?:(?<=^)|(?<=\s)|(?<=[>\(;-]))([%s]{3,})(\w*)(?=\s|%s|$)(?=[^">]*?(<|$))""" %
197+
(upper_re_s, self.pnct_re_s)),
188198
]
189199

190200
# These are the changes that need to be made for characters that occur

tox.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ deps = nose
88
regex
99
Pillow
1010
commands = nosetests --id-file=.noseids.{envname}
11+
12+
[testenv:pypy]
13+
deps = nose
14+
coverage
15+
html5lib
16+
Pillow

0 commit comments

Comments
 (0)