Skip to content

Commit afe17a7

Browse files
authored
bpo-35482: Fixes HTML escaping in CHM index and build location of NEWS file (GH-11224)
1 parent b2f642c commit afe17a7

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

Doc/make.bat

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if not defined SPHINXBUILD (
1616
%PYTHON% -m pip install sphinx
1717
if errorlevel 1 exit /B
1818
)
19-
set SPHINXBUILD=%PYTHON% -c "import sphinx, sys; sys.argv[0] = 'sphinx-build'; sys.exit(sphinx.main())"
19+
set SPHINXBUILD=%PYTHON% -c "import sphinx.cmd.build, sys; sys.exit(sphinx.cmd.build.main())"
2020
)
2121

2222
%PYTHON% -c "import python_docs_theme" > nul 2> nul
@@ -115,17 +115,16 @@ goto end
115115
:build
116116
if not exist "%BUILDDIR%" mkdir "%BUILDDIR%"
117117

118-
rem We ought to move NEWS to %BUILDDIR%\NEWS and point
119-
rem Sphinx at the right location.
118+
rem PY_MISC_NEWS_DIR is also used by our Sphinx extension in tools/extensions/pyspecific.py
119+
if not defined PY_MISC_NEWS_DIR set PY_MISC_NEWS_DIR=%BUILDDIR%\%1
120120
if exist ..\Misc\NEWS (
121-
echo.Copying Misc\NEWS to build\NEWS
122-
if not exist build mkdir build
123-
copy ..\Misc\NEWS build\NEWS > nul
121+
echo.Copying Misc\NEWS to %PY_MISC_NEWS_DIR%\NEWS
122+
copy ..\Misc\NEWS "%PY_MISC_NEWS_DIR%\NEWS" > nul
124123
) else if exist ..\Misc\NEWS.D (
125124
if defined BLURB (
126125
echo.Merging Misc/NEWS with %BLURB%
127126
if not exist build mkdir build
128-
%BLURB% merge -f build\NEWS
127+
%BLURB% merge -f "%PY_MISC_NEWS_DIR%\NEWS"
129128
) else (
130129
echo.No Misc/NEWS file and Blurb is not available.
131130
exit /B 1

Doc/tools/extensions/escape4chm.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import re
99
from html.entities import codepoint2name
1010

11+
from sphinx.util.logging import getLogger
12+
1113
# escape the characters which codepoint > 0x7F
1214
def _process(string):
1315
def escape(matchobj):
@@ -23,17 +25,33 @@ def escape(matchobj):
2325

2426
def escape_for_chm(app, pagename, templatename, context, doctree):
2527
# only works for .chm output
26-
if not hasattr(app.builder, 'name') or app.builder.name != 'htmlhelp':
28+
if getattr(app.builder, 'name', '') != 'htmlhelp':
2729
return
2830

2931
# escape the `body` part to 7-bit ASCII
3032
body = context.get('body')
3133
if body is not None:
3234
context['body'] = _process(body)
3335

36+
def fixup_keywords(app, exception):
37+
# only works for .chm output
38+
if getattr(app.builder, 'name', '') != 'htmlhelp' or exception:
39+
return
40+
41+
getLogger(__name__).info('fixing HTML escapes in keywords file...')
42+
outdir = app.builder.outdir
43+
outname = app.builder.config.htmlhelp_basename
44+
with app.builder.open_file(outdir, outname + '.hhk', 'r') as f:
45+
index = f.read()
46+
with app.builder.open_file(outdir, outname + '.hhk', 'w') as f:
47+
f.write(index.replace(''', '''))
48+
3449
def setup(app):
3550
# `html-page-context` event emitted when the HTML builder has
3651
# created a context dictionary to render a template with.
3752
app.connect('html-page-context', escape_for_chm)
53+
# `build-finished` event emitted when all the files have been
54+
# output.
55+
app.connect('build-finished', fixup_keywords)
3856

3957
return {'version': '1.0', 'parallel_read_safe': True}

Doc/tools/extensions/pyspecific.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import re
1313
import io
14-
from os import path
14+
from os import getenv, path
1515
from time import asctime
1616
from pprint import pformat
1717
from docutils.io import StringOutput
@@ -292,7 +292,9 @@ def run(self):
292292
fname = self.arguments[0]
293293
source = self.state_machine.input_lines.source(
294294
self.lineno - self.state_machine.input_offset - 1)
295-
source_dir = path.dirname(path.abspath(source))
295+
source_dir = getenv('PY_MISC_NEWS_DIR')
296+
if not source_dir:
297+
source_dir = path.dirname(path.abspath(source))
296298
fpath = path.join(source_dir, fname)
297299
self.state.document.settings.record_dependencies.add(fpath)
298300
try:

0 commit comments

Comments
 (0)