Mercurial > hgbook
annotate en/fixhtml.py @ 153:cf24633b8de7
Set up simple distribution rules.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Mon, 12 Mar 2007 21:10:19 -0700 |
parents | 16f02802f448 |
children | 2e73abddad21 |
rev | line source |
---|---|
149
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 import os |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 import sys |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 import re |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 unicode_re = re.compile(r'�([0-7][0-9a-f]);', re.I) |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 fancyvrb_re = re.compile(r'id="fancyvrb\d+"', re.I) |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 tmpsuffix = '.tmp.' + str(os.getpid()) |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 def fix_ascii(m): |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 return chr(int(m.group(1), 16)) |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 for name in sys.argv[1:]: |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 tmpname = name + tmpsuffix |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 ofp = file(tmpname, 'w') |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 for line in file(name): |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 line = unicode_re.sub(fix_ascii, line) |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 line = fancyvrb_re.sub('id="fancyvrb"', line) |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 ofp.write(line) |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 ofp.close() |
16f02802f448
Start to produce saner HTML and CSS.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 os.rename(tmpname, name) |