Skip to content

Commit 936df02

Browse files
committed
Restore Python 2.6 compatibility
1 parent 50cebec commit 936df02

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

coloredlogs/converter.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
'white',
3030
)
3131

32-
# Regular expression that matches strings we want to convert. Used to separate
33-
# all special strings and literal output in a single pass (this allows us to
34-
# properly encode the output without resorting to nasty hacks).
35-
token_pattern = re.compile('(https?://\\S+|www\\.\\S+|\x1b\\[.*?m)', re.UNICODE)
32+
# Compiled regular expression that matches leading spaces (indentation).
33+
INDENT_PATTERN = re.compile('^ +', re.MULTILINE)
34+
35+
# Compiled regular expression that matches strings we want to convert. Used to
36+
# separate all special strings and literal output in a single pass (this allows
37+
# us to properly encode the output without resorting to nasty hacks).
38+
TOKEN_PATTERN = re.compile('(https?://\\S+|www\\.\\S+|\x1b\\[.*?m)', re.UNICODE)
3639

3740

3841
def capture(command, encoding='UTF-8'):
@@ -93,7 +96,7 @@ def convert(text, code=True, tabsize=4):
9396
:returns: The text converted to HTML (a string).
9497
"""
9598
output = []
96-
for token in token_pattern.split(text):
99+
for token in TOKEN_PATTERN.split(text):
97100
if token.startswith(('http://', 'https://', 'www.')):
98101
url = token
99102
if '://' not in token:
@@ -151,7 +154,7 @@ def encode_whitespace(text, tabsize=4):
151154
# Convert leading spaces (that is to say spaces at the start of the string
152155
# and/or directly after a line ending) into non-breaking spaces, otherwise
153156
# HTML rendering engines will simply ignore these spaces.
154-
text = re.sub('^ +', encode_whitespace_cb, text, 0, re.MULTILINE)
157+
text = re.sub(INDENT_PATTERN, encode_whitespace_cb, text)
155158
# Convert runs of multiple spaces into non-breaking spaces to avoid HTML
156159
# rendering engines from visually collapsing runs of spaces into a single
157160
# space. We specifically don't replace single spaces for several reasons:

0 commit comments

Comments
 (0)