hgbook

view en/fixhtml.py @ 255:9c49615e8dcb

Stop bibtex from getting shirty over lack of authorship.
author Bryan O'Sullivan <bos@serpentine.com>
date Wed May 30 22:19:56 2007 -0700 (2007-05-30)
parents 16f02802f448
children ec6a3bb10986
line source
1 #!/usr/bin/env python
2 #
3 # This script attempts to work around some of the more bizarre and
4 # quirky behaviours of htlatex.
5 #
6 # - We've persuaded htlatex to produce UTF-8, which unfortunately
7 # causes it to use huge character sequences to represent even the
8 # safe 7-bit ASCII subset of UTF-8. We fix that up.
9 #
10 # - BUT we have to treat angle brackets (for example, redirections in
11 # shell script snippets) specially, otherwise they'll break the
12 # generated HTML. (Reported by Johannes Hoff.)
13 #
14 # - For some reason, htlatex gives a unique ID to each fancyvrb
15 # environment, which makes writing a sane, small CSS stylesheet
16 # impossible. We squish all those IDs down to nothing.
18 import os
19 import sys
20 import re
22 angle_re = re.compile(r'(&#x003[CE];)')
23 unicode_re = re.compile(r'&#x00([0-7][0-9A-F]);')
24 fancyvrb_re = re.compile(r'id="fancyvrb\d+"', re.I)
26 tmpsuffix = '.tmp.' + str(os.getpid())
28 def hide_angle(m):
29 return m.group(1).lower()
31 def fix_ascii(m):
32 return chr(int(m.group(1), 16))
34 for name in sys.argv[1:]:
35 tmpname = name + tmpsuffix
36 ofp = file(tmpname, 'w')
37 for line in file(name):
38 line = angle_re.sub(hide_angle, line)
39 line = unicode_re.sub(fix_ascii, line)
40 line = fancyvrb_re.sub('id="fancyvrb"', line)
41 ofp.write(line)
42 ofp.close()
43 os.rename(tmpname, name)