88import re
99from html .entities import codepoint2name
1010
11+ from sphinx .util .logging import getLogger
12+
1113# escape the characters which codepoint > 0x7F
1214def _process (string ):
1315 def escape (matchobj ):
@@ -23,17 +25,33 @@ def escape(matchobj):
2325
2426def 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+
3449def 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 }
0 commit comments