|
29 | 29 | 'white',
|
30 | 30 | )
|
31 | 31 |
|
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) |
36 | 39 |
|
37 | 40 |
|
38 | 41 | def capture(command, encoding='UTF-8'):
|
@@ -93,7 +96,7 @@ def convert(text, code=True, tabsize=4):
|
93 | 96 | :returns: The text converted to HTML (a string).
|
94 | 97 | """
|
95 | 98 | output = []
|
96 |
| - for token in token_pattern.split(text): |
| 99 | + for token in TOKEN_PATTERN.split(text): |
97 | 100 | if token.startswith(('http://', 'https://', 'www.')):
|
98 | 101 | url = token
|
99 | 102 | if '://' not in token:
|
@@ -151,7 +154,7 @@ def encode_whitespace(text, tabsize=4):
|
151 | 154 | # Convert leading spaces (that is to say spaces at the start of the string
|
152 | 155 | # and/or directly after a line ending) into non-breaking spaces, otherwise
|
153 | 156 | # 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) |
155 | 158 | # Convert runs of multiple spaces into non-breaking spaces to avoid HTML
|
156 | 159 | # rendering engines from visually collapsing runs of spaces into a single
|
157 | 160 | # space. We specifically don't replace single spaces for several reasons:
|
|
0 commit comments