diff options
Diffstat (limited to 'paste')
| -rw-r--r-- | paste/exceptions/formatter.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/paste/exceptions/formatter.py b/paste/exceptions/formatter.py index 06ba8a0..e47d73d 100644 --- a/paste/exceptions/formatter.py +++ b/paste/exceptions/formatter.py @@ -475,13 +475,27 @@ pre_re = re.compile(r'</?pre.*?>') error_re = re.compile(r'<h3>ERROR: .*?</h3>') def str2html(src, strip=False, indent_subsequent=0): + """ + Convert a string to HTML. Try to be really safe about it, + returning a quoted version of the string if nothing else works. + """ + try: + return _str2html(src, strip=strip, indent_subsequent=indent_subsequent) + except: + return html_quote(src) + +def _str2html(src, strip=False, indent_subsequent=0): if strip: src = src.strip() - src = PySourceColor.str2html(src, form='snip') - src = error_re.sub('', src) - src = pre_re.sub('', src) - src = re.sub(r'^[\n\r]{0,1}', '', src) - src = re.sub(r'[\n\r]{0,1}$', '', src) + orig_src = src + try: + src = PySourceColor.str2html(src, form='snip') + src = error_re.sub('', src) + src = pre_re.sub('', src) + src = re.sub(r'^[\n\r]{0,1}', '', src) + src = re.sub(r'[\n\r]{0,1}$', '', src) + except: + src = html_quote(orig_src) lines = src.splitlines() if len(lines) == 1: return lines[0] @@ -494,3 +508,5 @@ def str2html(src, strip=False, indent_subsequent=0): src = whitespace_re.sub( lambda m: ' '*(len(m.group(0))-1) + ' ', src) return src + + |
