Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…H-9758) Let .chm document display non-ASCII characters properly Escape the `body` part of .chm source file to 7-bit ASCII, to fix visual effect on some MBCS Windows systems.
- Loading branch information
Showing
3 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Escape the `body` part of .chm source file to 7-bit ASCII, to fix visual | ||
effect on some MBCS Windows systems. | ||
https://bugs.python.org/issue32174 | ||
""" | ||
|
||
import re | ||
from html.entities import codepoint2name | ||
|
||
# escape the characters which codepoint > 0x7F | ||
def _process(string): | ||
def escape(matchobj): | ||
codepoint = ord(matchobj.group(0)) | ||
|
||
name = codepoint2name.get(codepoint) | ||
if name is None: | ||
return '&#%d;' % codepoint | ||
else: | ||
return '&%s;' % name | ||
|
||
return re.sub(r'[^\x00-\x7F]', escape, string) | ||
|
||
def escape_for_chm(app, pagename, templatename, context, doctree): | ||
# only works for .chm output | ||
if not hasattr(app.builder, 'name') or app.builder.name != 'htmlhelp': | ||
return | ||
|
||
# escape the `body` part to 7-bit ASCII | ||
body = context.get('body') | ||
if body is not None: | ||
context['body'] = _process(body) | ||
|
||
def setup(app): | ||
# `html-page-context` event emitted when the HTML builder has | ||
# created a context dictionary to render a template with. | ||
app.connect('html-page-context', escape_for_chm) | ||
|
||
return {'version': '1.0', 'parallel_read_safe': True} |
2 changes: 2 additions & 0 deletions
2
Misc/NEWS.d/next/Documentation/2018-10-08-19-15-28.bpo-32174.YO9CYm.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
chm document displays non-ASCII charaters properly on some MBCS Windows | ||
systems. |