hgbook

view it/web/genindex.py @ 776:c0794abb494e

Unicode fiddling.
author Giulio@puck
date Tue Aug 04 18:57:58 2009 +0200 (2009-08-04)
parents 23ebfe2bd091
children 52ed51334e01
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)\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')))
12 fp = open('index-read.html.in', 'w', encoding='utf-8')
14 print('''
15 <div class="navheader"><h1 class="booktitle">Mercurial: la guida definitiva<div class="authors">di Bryan O'Sullivan</div></h1></div>
16 <div class="book"><ul class="booktoc">''', file=fp)
18 ch = 0
19 app = 0
20 ab = 0
21 for c in chapters:
22 filename = None
23 title = None
24 chapid = None
25 chaptype = None
26 for line in open(c, encoding='utf-8'):
27 m = chapter_re.search(line)
28 if m:
29 chaptype, chapid = m.groups()
30 m = filename_re.search(line)
31 if m:
32 filename = m.group(1)
33 m = title_re.search(line)
34 if m:
35 title = m.group(1)
36 if filename and title and chapid:
37 if chaptype == 'appendix':
38 num = chr(ord('A') + app)
39 app += 1
40 else:
41 num = ch
42 ch += 1
43 ab += 1
44 date = os.popen('hg log -l1 --template "{date|isodate}" ' + c).read().split(None, 1)[0]
45 args = {
46 'ab': "ab"[ab % 2],
47 'date': date,
48 'chapid': chapid,
49 'num': num,
50 'filename': filename,
51 'title': title,
52 }
53 print('<li class="zebra_%(ab)s"><span class="chapinfo">%(date)s<a href="/feeds/comments/%(chapid)s/"><img src="figs/rss.png"/></a></span>%(num)s. <a href="%(filename)s">%(title)s</a></li>' % args, file=fp)
54 break
56 print('</ul></div>', file=fp)
58 fp.close()