]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/wiki-slurp.py
90efea4d13dceee14238ec74794bd765abed47ff
[lilypond.git] / buildscripts / wiki-slurp.py
1 #!@PYTHON@
2
3 #
4 # translate an entire Wiki site into local html.
5 #
6
7 import re
8 import urllib
9 import sys
10 import getopt
11 import os
12 program_version = '@TOPLEVEL_VERSION@'
13 if program_version == '@' + 'TOPLEVEL_VERSION' + '@':
14         program_version = '1.5.69'
15
16
17 def help ():
18         print """Usage: wiki-slurp.py [OPTIONS]... ENTRY-PATTERN...
19
20 Download a WikiWiki site and convert to local html.
21
22 Example: wiki-slurp.py -d /tmp/output 'http://c2.com/cgi-bin/wiki?'
23
24 Options:
25   -h, --help         show this help
26   -m, --mangle       mangle file names to be shorter 
27   -d, --outdir=DIR   set output directory to DIR
28   -v, --version      show version information
29
30 Warning: downloading an entire Wiki site generates a huge amount of
31 traffic and server load.  Consider asking for a copy of the database.
32 See also http://c2.com/cgi-bin/wiki?WikiSlurp.
33
34 Report bugs to bug-lilypond@gnu.org.
35
36 Written by Han-Wen Nienhuys <hanwen@cs.uu.nl>.
37
38 """
39
40 def print_version ():
41         print r"""wiki-slurp.py %s
42
43 This is free software.  It is covered by the GNU General Public License,
44 and you are welcome to change it and/or distribute copies of it under
45 certain conditions.  Invoke as `midi2ly --warranty' for more information.
46
47 Copyright (c)  2000--2003 by Han-Wen Nienhuys <hanwen@cs.uu.nl>
48 """ % program_version
49
50 (options, files) = getopt.getopt (sys.argv[1:], 'vd:hm', ['help','mangle','version', 'outdir='])
51
52
53 def identity (name):
54         return name
55
56 def mangle (name):
57         return '%d' % hash (name)
58
59 mangler = identity
60
61 outdir = '/tmp/'
62
63 for opt in options:
64         o = opt[0]
65         a = opt[1]
66         if o== '--help' or o == '-h':
67                 help ()
68                 sys.exit (0)
69         elif o == '--version' or o == '-v':
70                 print_version ()
71                 sys.exit(0)
72         elif  o == '--mangle' or o == '-m':
73                 mangler = mangle
74         elif o == '--outdir' or o == '-d':
75                 outdir = a
76         else:
77                 print o
78                 raise getopt.error
79
80
81 patterns = files
82
83 if not patterns:
84         help ()
85         sys.stderr.write ("\n")
86         sys.exit (2)
87
88 re_patterns = []
89 for pattern in patterns:
90         re_patterns.append (re.sub ('([?.])', '\\\\\\1', pattern))
91
92 todo = ["FrontPage"]
93
94 print 'here!'
95 done = {
96         'EditText': 1,  
97         }
98
99 def unwiki (str, pat, mangler):
100         local = '<a href="%s([A-Za-z]+)">([A-Za-z]+)</a>' % pat
101
102         newurls = []
103         def do_replace (match, us = newurls, mangler = mangler):
104                 newurl = match.group (1)
105                 local = mangler (newurl)
106                 
107                 replacement = '<a href="%s.html">%s</a>' % (local,newurl)
108                 us.append (newurl)
109                 return replacement
110
111         str = re.sub (local, do_replace, str)
112         otherurl = '<a href="%s[^>]*">([?A-Za-z]+)</a>' % pat
113         str = re.sub (otherurl, '\\1', str)
114
115         imagesrc = '<a href="%s[^>]*">(<img[^>]*>)</a>' % pat
116         str = re.sub (imagesrc, '\\1', str)
117         
118         return (str, newurls)
119
120
121 while todo:
122         f = todo[-1]
123         todo = todo[:-1]
124
125         if done.has_key (f):
126                 continue
127         done [f] = 1
128
129         mangled = mangler (f)
130         
131         sys.stderr.write ("reading `%s' ... " % f)
132         sys.stderr.flush ()
133
134         page = urllib.urlopen (patterns[0] + f).read ()
135         sys.stderr.write ('done. ')
136         sys.stderr.flush ()
137
138         for re_pattern in re_patterns:
139                 (page, nus) = unwiki (page, re_pattern, mangler)
140                 todo.extend (nus)
141         
142         outname = os.path.join (outdir, mangled) + '.html'
143         fo = open (outname, 'w')
144
145         sys.stderr.write ("Writing `%s'\n" % outname)
146         fo.write (page)
147         fo.close ()
148
149
150 # test
151 if 0:
152         page = open ('/tmp/FrontPage.html').read()
153         (str, us)=unwiki (page, re_patterns)
154         print str
155         print us
156