From 524d1f31145e30bbc097b98ba628fb02a1b51b44 Mon Sep 17 00:00:00 2001 From: "Rob G. Healey" Date: Thu, 17 Feb 2011 16:43:31 +0000 Subject: [PATCH] Returned html_escape() back to NarrativeWeb after discussions with Benny. svn: r16649 --- src/plugins/webreport/NarrativeWeb.py | 40 +++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/plugins/webreport/NarrativeWeb.py b/src/plugins/webreport/NarrativeWeb.py index 67f0ae4f0..765c9e401 100644 --- a/src/plugins/webreport/NarrativeWeb.py +++ b/src/plugins/webreport/NarrativeWeb.py @@ -103,9 +103,6 @@ from libhtmlconst import _CHARACTER_SETS, _CC, _COPY_OPTIONS # src/plugins/lib/libhtml.py from libhtml import Html -# ability to escape characters from html output -from xml.sax.saxutils import escape as html_escape - # import styled notes from # src/plugins/lib/libhtmlbackend.py from libhtmlbackend import HtmlBackend, process_spaces @@ -203,6 +200,43 @@ wrapper.width = 20 PLUGMAN = GuiPluginManager.get_instance() CSS = PLUGMAN.process_plugin_data('WEBSTUFF') +_html_dbl_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 +# special characters for presentation in HTML based on the above list. +def html_escape(text): + """Convert the text and replace some characters with a &# variant.""" + + # First single characters, no quotes + text = ''.join([_html_replacement.get(c, c) for c in text]) + + # Deal with double quotes. + while 1: + m = _html_dbl_quotes.match(text) + if not m: + break + text = m.group(1) + '“' + m.group(2) + '”' + m.group(3) + # Replace remaining double quotes. + text = text.replace('"', '"') + + # Deal with single quotes. + text = text.replace("'s ", '’s ') + while 1: + m = _html_sng_quotes.match(text) + if not m: + break + text = m.group(1) + '‘' + m.group(2) + '’' + m.group(3) + # Replace remaining single quotes. + text = text.replace("'", ''') + + return text + def name_to_md5(text): """This creates an MD5 hex string to be used as filename.""" return md5(text).hexdigest()