summaryrefslogtreecommitdiff
path: root/sphinx/builders/htmlhelp.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2018-12-24 12:59:38 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2018-12-24 13:54:53 +0900
commit6ffe549f492dd8ed9cfcc286449a200a77bff235 (patch)
treeeb0f46c92c6f37704e2451c4e4799755076991d0 /sphinx/builders/htmlhelp.py
parent503cf9c39e829024160145d03bc1e9bc12d65ec9 (diff)
downloadsphinx-git-6ffe549f492dd8ed9cfcc286449a200a77bff235.tar.gz
refactor chm_htmlescape()
Diffstat (limited to 'sphinx/builders/htmlhelp.py')
-rw-r--r--sphinx/builders/htmlhelp.py34
1 files changed, 16 insertions, 18 deletions
diff --git a/sphinx/builders/htmlhelp.py b/sphinx/builders/htmlhelp.py
index 42ee05c4a..dfb58ede6 100644
--- a/sphinx/builders/htmlhelp.py
+++ b/sphinx/builders/htmlhelp.py
@@ -13,10 +13,10 @@ from __future__ import print_function
import codecs
import os
-import re
from os import path
from docutils import nodes
+from six import PY3
from sphinx import addnodes
from sphinx.builders.html import StandaloneHTMLBuilder
@@ -170,22 +170,21 @@ chm_locales = {
}
-def chm_htmlescape(*args, **kwargs):
- # type: (*Any, **Any) -> unicode
+def chm_htmlescape(s, quote=None):
+ # type: (unicode, bool) -> unicode
"""
- chm_htmlescape() is a wrapper of htmlescape().
+ chm_htmlescape() is a wrapper of html.escape().
.hhc/.hhk files don't recognize hex escaping, we need convert
- hex escaping to decimal escaping. for example: `&#x27;` -> `&#39;`
- htmlescape() may generates a hex escaping `&#x27;` for single
- quote `'`, this wrapper fixes this.
+ hex escaping to decimal escaping. for example: ``&#x27;`` -> ``&#39;``
+ html.escape() may generates a hex escaping ``&#x27;`` for single
+ quote ``'``, this wrapper fixes this.
"""
- def convert(matchobj):
- # type: (Match[unicode]) -> unicode
- codepoint = int(matchobj.group(1), 16)
- return '&#%d;' % codepoint
- return re.sub(r'&#[xX]([0-9a-fA-F]+);',
- convert,
- htmlescape(*args, **kwargs))
+ if quote is None:
+ quote = PY3 # True for py3, False for py2 (for compatibility)
+
+ s = htmlescape(s, quote)
+ s = s.replace('&#x27;', '&#39;') # re-escape as decimal
+ return s
class HTMLHelpBuilder(StandaloneHTMLBuilder):
@@ -297,7 +296,7 @@ class HTMLHelpBuilder(StandaloneHTMLBuilder):
write_toc(subnode, ullevel)
elif isinstance(node, nodes.reference):
link = node['refuri']
- title = chm_htmlescape(node.astext()).replace('"', '&quot;')
+ title = chm_htmlescape(node.astext(), True)
f.write(object_sitemap % (title, link))
elif isinstance(node, nodes.bullet_list):
if ullevel != 0:
@@ -327,10 +326,9 @@ class HTMLHelpBuilder(StandaloneHTMLBuilder):
# type: (unicode, List[Tuple[unicode, unicode]], List[Tuple[unicode, List[Tuple[unicode, unicode]]]]) -> None # NOQA
def write_param(name, value):
# type: (unicode, unicode) -> None
- item = ' <param name="%s" value="%s">\n' % \
- (name, value)
+ item = ' <param name="%s" value="%s">\n' % (name, value)
f.write(item)
- title = chm_htmlescape(title)
+ title = chm_htmlescape(title, True)
f.write('<LI> <OBJECT type="text/sitemap">\n')
write_param('Keyword', title)
if len(refs) == 0: