Mercurial > hgbook
annotate web/genindex.py @ 760:65b82a891cf7
Add epub format
author | Dongsheng Song <dongsheng.song@gmail.com> |
---|---|
date | Tue, 31 Mar 2009 11:04:18 +0800 |
parents | 9e8e5292acaa |
children | 0ffae4ee4c47 |
rev | line source |
---|---|
698
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
2 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
3 import glob, os, re |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
4 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
5 chapter_re = re.compile(r'<(chapter|appendix|preface)\s+id="([^"]+)">') |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
6 filename_re = re.compile(r'<\?dbhtml filename="([^"]+)"\?>') |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
7 title_re = re.compile(r'<title>(.*)</title>') |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
8 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
9 chapters = glob.glob('../en/ch*.xml') + glob.glob('../en/app*.xml') |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
10 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
11 fp = open('index-read.html.in', 'w') |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
12 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
13 print >> fp, '''<!-- -*- html -*- --> |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
14 {% extends "boilerplate.html" %} |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
15 {% block bodycontent %} |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
16 <div class="navheader"><h1 class="booktitle">Mercurial: The Definitive Guide<div class="authors">by Bryan O'Sullivan</div></h1></div> |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
17 <div class="book"><ul class="booktoc">''' |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
18 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
19 ch = 0 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
20 app = 0 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
21 ab = 0 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
22 for c in chapters: |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
23 filename = None |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
24 title = None |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
25 chapid = None |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
26 chaptype = None |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
27 for line in open(c): |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
28 m = chapter_re.search(line) |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
29 if m: |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
30 chaptype, chapid = m.groups() |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
31 m = filename_re.search(line) |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
32 if m: |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
33 filename = m.group(1) |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
34 m = title_re.search(line) |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
35 if m: |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
36 title = m.group(1) |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
37 if filename and title and chapid: |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
38 if chaptype == 'appendix': |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
39 num = chr(ord('A') + app) |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
40 app += 1 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
41 else: |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
42 num = ch |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
43 ch += 1 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
44 ab += 1 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
45 date = os.popen('hg log -l1 --template "{date|isodate}" ' + c).read().split(None, 1)[0] |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
46 args = { |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
47 'ab': "ab"[ab % 2], |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
48 'date': date, |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
49 'chapid': chapid, |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
50 'num': num, |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
51 'filename': filename, |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
52 'title': title, |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
53 } |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
54 print >> fp, '<li class="zebra_%(ab)s"><span class="chapinfo">%(date)s<a href="/feeds/comments/%(chapid)s/"><img src="/support/figs/rss.png"/></a></span>%(num)s. <a href="%(filename)s">%(title)s</a></li>' % args |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
55 break |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
56 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
57 print >> fp, '''</ul></div> |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
58 {% endblock %}''' |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
59 |
9e8e5292acaa
Generate a nice readable index.
Bryan O'Sullivan <bos@serpentine.com>
parents:
diff
changeset
|
60 fp.close() |