Simplify html_escape using library escape function and string substitutions
svn: r16751
This commit is contained in:
@ -61,6 +61,7 @@ from textwrap import TextWrapper
|
|||||||
from unicodedata import normalize
|
from unicodedata import normalize
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
import re
|
import re
|
||||||
|
from xml.sax.saxutils import escape
|
||||||
|
|
||||||
import operator
|
import operator
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
@ -202,11 +203,6 @@ CSS = PLUGMAN.process_plugin_data('WEBSTUFF')
|
|||||||
|
|
||||||
_html_dbl_quotes = re.compile(r'([^"]*) " ([^"]*) " (.*)', re.VERBOSE)
|
_html_dbl_quotes = re.compile(r'([^"]*) " ([^"]*) " (.*)', re.VERBOSE)
|
||||||
_html_sng_quotes = re.compile(r"([^']*) ' ([^']*) ' (.*)", re.VERBOSE)
|
_html_sng_quotes = re.compile(r"([^']*) ' ([^']*) ' (.*)", re.VERBOSE)
|
||||||
_html_replacement = {
|
|
||||||
"&" : "&",
|
|
||||||
">" : ">",
|
|
||||||
"<" : "<",
|
|
||||||
}
|
|
||||||
|
|
||||||
# This command then defines the 'html_escape' option for escaping
|
# This command then defines the 'html_escape' option for escaping
|
||||||
# special characters for presentation in HTML based on the above list.
|
# special characters for presentation in HTML based on the above list.
|
||||||
@ -214,24 +210,22 @@ def html_escape(text):
|
|||||||
"""Convert the text and replace some characters with a &# variant."""
|
"""Convert the text and replace some characters with a &# variant."""
|
||||||
|
|
||||||
# First single characters, no quotes
|
# First single characters, no quotes
|
||||||
text = ''.join([_html_replacement.get(c, c) for c in text])
|
text = escape(text)
|
||||||
|
|
||||||
# Deal with double quotes.
|
# Deal with double quotes.
|
||||||
while 1:
|
|
||||||
m = _html_dbl_quotes.match(text)
|
m = _html_dbl_quotes.match(text)
|
||||||
if not m:
|
while m:
|
||||||
break
|
text = "%s" "“" "%s" "”" "%s" % m.groups()
|
||||||
text = m.group(1) + '“' + m.group(2) + '”' + m.group(3)
|
m = _html_dbl_quotes.match(text)
|
||||||
# Replace remaining double quotes.
|
# Replace remaining double quotes.
|
||||||
text = text.replace('"', '"')
|
text = text.replace('"', '"')
|
||||||
|
|
||||||
# Deal with single quotes.
|
# Deal with single quotes.
|
||||||
text = text.replace("'s ", '’s ')
|
text = text.replace("'s ", '’s ')
|
||||||
while 1:
|
|
||||||
m = _html_sng_quotes.match(text)
|
m = _html_sng_quotes.match(text)
|
||||||
if not m:
|
while m:
|
||||||
break
|
text = "%s" "‘" "%s" "’" "%s" % m.groups()
|
||||||
text = m.group(1) + '‘' + m.group(2) + '’' + m.group(3)
|
m = _html_sng_quotes.match(text)
|
||||||
# Replace remaining single quotes.
|
# Replace remaining single quotes.
|
||||||
text = text.replace("'", ''')
|
text = text.replace("'", ''')
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user