bpo-35482: Fixes HTML escaping in CHM index and build location of NEWS file (GH-11224)

This commit is contained in:
Steve Dower 2018-12-19 18:20:06 -08:00 committed by GitHub
parent b2f642ccd2
commit afe17a7bee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 10 deletions

View file

@ -8,6 +8,8 @@ https://bugs.python.org/issue32174
import re
from html.entities import codepoint2name
from sphinx.util.logging import getLogger
# escape the characters which codepoint > 0x7F
def _process(string):
def escape(matchobj):
@ -23,7 +25,7 @@ def _process(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':
if getattr(app.builder, 'name', '') != 'htmlhelp':
return
# escape the `body` part to 7-bit ASCII
@ -31,9 +33,25 @@ def escape_for_chm(app, pagename, templatename, context, doctree):
if body is not None:
context['body'] = _process(body)
def fixup_keywords(app, exception):
# only works for .chm output
if getattr(app.builder, 'name', '') != 'htmlhelp' or exception:
return
getLogger(__name__).info('fixing HTML escapes in keywords file...')
outdir = app.builder.outdir
outname = app.builder.config.htmlhelp_basename
with app.builder.open_file(outdir, outname + '.hhk', 'r') as f:
index = f.read()
with app.builder.open_file(outdir, outname + '.hhk', 'w') as f:
f.write(index.replace(''', '''))
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)
# `build-finished` event emitted when all the files have been
# output.
app.connect('build-finished', fixup_keywords)
return {'version': '1.0', 'parallel_read_safe': True}

View file

@ -11,7 +11,7 @@
import re
import io
from os import path
from os import getenv, path
from time import asctime
from pprint import pformat
from docutils.io import StringOutput
@ -292,7 +292,9 @@ class MiscNews(Directive):
fname = self.arguments[0]
source = self.state_machine.input_lines.source(
self.lineno - self.state_machine.input_offset - 1)
source_dir = path.dirname(path.abspath(source))
source_dir = getenv('PY_MISC_NEWS_DIR')
if not source_dir:
source_dir = path.dirname(path.abspath(source))
fpath = path.join(source_dir, fname)
self.state.document.settings.record_dependencies.add(fpath)
try: