hgbook

view it/web/genindex.py @ 1064:3a2260cf556f

1.8 6,7 para zh translated
author Zhaoping Sun <zhaopingsun@gmail.com>
date Wed Nov 11 16:50:06 2009 -0500 (2009-11-11)
parents 600e7ab15c67
children 719b03ea27c8
line source
1 # This script works with Python 3.0 or above
3 import glob, os, re
5 chapter_re = re.compile(r'<(chapter|appendix|preface|bibliography)\s+id="([^"]+)">')
6 filename_re = re.compile(r'<\?dbhtml filename="([^"]+)"\?>')
7 title_re = re.compile(r'<title>(.*)</title>')
9 chapters = (sorted(glob.glob('../ch*.xml')) +
10 sorted(glob.glob('../app*.xml')) +
11 sorted(glob.glob('../biblio*.xml')))
13 fp = open('index-read.html.in', 'w', encoding='utf-8')
15 print('''
16 <div class="navheader"><h1 class="booktitle">Mercurial: la guida definitiva<div class="authors">di Bryan O\&#8217;Sullivan</div></h1></div>
17 <div class="book"><ul class="booktoc">''', file=fp)
19 ch = 0
20 app = 0
21 ab = 0
22 for c in chapters:
23 filename = None
24 title = None
25 chapid = None
26 chaptype = None
27 for line in open(c, encoding='utf-8'):
28 m = chapter_re.search(line)
29 if m:
30 chaptype, chapid = m.groups()
31 m = filename_re.search(line)
32 if m:
33 filename = m.group(1)
34 m = title_re.search(line)
35 if m:
36 title = m.group(1)
37 if filename and title and chapid:
38 if chaptype == 'bibliography':
39 num = ''
40 elif chaptype == 'appendix':
41 num = str(chr(ord('A') + app)) + '. '
42 app += 1
43 else:
44 num = str(ch) + '. '
45 ch += 1
46 if '&' in title:
47 title = title.replace('&', '\&')
48 ab += 1
49 date = os.popen('hg log -l1 --template "{date|isodate}" ' + c).read().split(None, 1)[0]
50 args = {
51 'ab': "ab"[ab % 2],
52 'date': date,
53 # 'chapid': chapid,
54 'num': num,
55 'filename': filename,
56 'title': title,
57 }
58 print('<li class="zebra_%(ab)s"><span class="chapinfo">%(date)s</span>%(num)s<a href="%(filename)s">%(title)s</a></li>' % args, file=fp)
59 break
61 print('</ul></div>', file=fp)
63 fp.close()