]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/add-html-footer.py
* lily/tab-note-heads-engraver.cc: listen to string number events
[lilypond.git] / stepmake / bin / add-html-footer.py
1 #!@PYTHON@
2
3 """
4 Print a nice footer.  add the top of the ChangeLog file (up to the ********)
5 """
6 import re
7 import sys
8 import os
9 import time
10 import string 
11 import getopt
12
13 gcos = "unknown"
14 index_url=''
15 top_url=''
16 changelog_file=''
17 package_name = ''
18 package_version = ''
19
20 mail_address = '(address unknown)'
21 try:
22         mail_address= os.environ['MAILADDRESS']
23 except KeyError:
24         pass
25
26 webmaster= mail_address
27 try:
28         webmaster= os.environ['WEBMASTER']
29 except KeyError:
30         pass
31
32 header_file = ''
33 footer_file = ''
34 default_header = r"""
35 """
36
37
38 wiki_base = 'http://afavant.elte.hu/lywiki/'
39
40
41 default_footer = r"""<hr>Please take me <a href=@INDEX@>back to the index</a>
42 of @PACKAGE_NAME@
43 """
44
45 built = r"""
46 <p>
47 <table align="center" width="100%%" cellspacing="2" BGCOLOR="#e8ffe8">
48  <tr>
49           <td align=left>
50
51 <a href="%(wiki_base)s%(wiki_page)s">Read </a> comments on this page, or
52 <a href="%(wiki_base)s%(wiki_page)s?action=edit">add</a> one.
53 <p>
54 <font size="-1">
55 This page is for %(package_name)s-%(package_version)s (%(branch_str)s). <br>
56 </font>
57 <address><font size="-1">
58 Report errors to &lt;<a href="mailto:%(mail_address)s">%(mail_address)s</a>&gt;.</font></address>
59         </tr>
60         </table>
61
62
63 """
64
65
66 def gulp_file (f):
67         try:
68                 i = open(f)
69                 i.seek (0, 2)
70                 n = i.tell ()
71                 i.seek (0,0)
72         except:
73                 sys.stderr.write ("can't open file: %s\n" % f)
74                 return ''
75         s = i.read (n)
76         if len (s) <= 0:
77                 sys.stderr.write ("gulped empty file: %s\n" % f)
78         i.close ()
79         return s
80
81 def help ():
82         sys.stdout.write (r"""Usage: add-html-footer [OPTIONS]... HTML-FILE
83 Add header, footer and top of ChangLog file (up to the ********) to HTML-FILE
84
85 Options:
86   --changelog=FILE          use FILE as ChangeLog [ChangeLog]
87   --footer=FILE             use FILE as footer
88   --header=FILE             use FILE as header
89   -h, --help                print this help
90   --index=URL               set homepage to URL
91   --name=NAME               set package_name to NAME
92   --version=VERSION         set package version to VERSION
93
94 """)
95         sys.exit (0)
96
97 (options, files) = getopt.getopt(sys.argv[1:], 'h', [
98         'changelog=', 'footer=', 'header=', 'help', 'index=',
99         'name=', 'version=']) 
100
101 for opt in options:
102         o = opt[0]
103         a = opt[1]
104         if o == '--changelog':
105                 changelog_file = a
106         elif o == '--footer':
107                 footer_file = a
108         elif o == '--header':
109                 header_file = a
110         elif o == '-h' or o == '--help':
111                 help ()
112         # urg, this is top!
113         elif o == '--index':
114                 index_url = a
115         elif o == '--name':
116                 package_name = a
117         elif o == '--version':
118                 package_version = a
119         else:
120                 raise 'unknown opt ', o
121
122 #burp?
123 def set_gcos ():
124         global gcos
125         os.environ["CONFIGSUFFIX"] = 'www';
126         if os.name == 'nt':
127                 import ntpwd
128                 pw = ntpwd.getpwname(os.environ['USERNAME'])
129         else:
130                 import pwd
131                 if os.environ.has_key('FAKEROOTKEY') and os.environ.has_key('LOGNAME'):
132                         pw = pwd.getpwnam (os.environ['LOGNAME'])
133                 else:
134                         pw = pwd.getpwuid (os.getuid())
135
136         f = pw[4]
137         f = string.split (f, ',')[0]
138         gcos = f 
139
140 def compose (default, file):
141         s = default
142         if file:
143                 s = gulp_file (file)
144         return s
145
146 set_gcos ()
147 localtime = time.strftime ('%c %Z', time.localtime (time.time ()))
148
149 if os.path.basename (index_url) != "index.html":
150         index_url = os.path.join (index_url , "index.html")
151 top_url = os.path.dirname (index_url) + "/"
152
153 header = compose (default_header, header_file)
154
155 # compose (default_footer, footer_file)
156 footer =  built
157 header_tag = '<!-- header_tag -->'
158 footer_tag = '<!-- footer_tag -->'
159
160 # Python < 1.5.2 compatibility
161 #
162 # On most platforms, this is equivalent to
163 #`normpath(join(os.getcwd()), PATH)'.  *Added in Python version 1.5.2*
164 if os.path.__dict__.has_key ('abspath'):
165         abspath = os.path.abspath
166 else:
167         def abspath (path):
168                 return os.path.normpath (os.path.join (os.getcwd (), path))
169
170
171 def remove_self_ref (s):        
172         self_url = abspath (os.getcwd () + '/' + f)
173         #sys.stderr.write ('url0: %s\n' % self_url)
174
175         # self_url = re.sub ('.*?' + string.lower (package_name) + '[^/]*/',
176         #                '', self_url)
177         # URG - this only works when source tree is unpacked in `src/' dir
178         # For some reason, .*? still eats away
179         #     /home/fred/usr/src/lilypond-1.5.14/Documentation/user/out-www/lilypond/
180         # instead of just
181         #
182         #     /home/fred/usr/src/lilypond-1.5.14/
183         #
184         #     Tutorial.html
185         self_url = re.sub ('.*?src/' + string.lower (package_name) + '[^/]*/',
186                          '', self_url)
187
188         #sys.stderr.write ('url1: %s\n' % self_url)
189         
190         #urg, ugly lily-specific toplevel index hack
191         self_url = re.sub ('.*topdocs/out-www/index.html', 'index.html', self_url)
192         #sys.stderr.write ('url2: %s\n' % self_url)
193
194         # ugh, python2.[12] re is broken.
195         ## pat = re.compile ('.*?(<a href="[\./]*' + self_url + '#?[^"]*">)([^<]*)(</a>)', re.DOTALL)
196         pat = re.compile ('[.\n]*?(<a href="[\./]*' + self_url + '#?[^"]*">)([^<]*)(</a>)')
197         m = pat.search (s)
198         while m:
199                 #sys.stderr.write ('self: %s\n' % m.group (2))
200                 s = s[:m.start (1)] + m.group (2) + s[m.end (3):]
201                 m = pat.search (s)
202         return s
203
204 def do_file (f):
205         s = gulp_file (f)
206         s = re.sub ('%', '%%', s)
207
208         if changelog_file:
209                 changes = gulp_file (changelog_file)
210                 # urg?
211                 #m = re.search ('^\\\\*\\\\*', changes)
212                 m = re.search (r'\*\*\*', changes)
213                 if m:
214                         changes = changes[:m.start (0)]
215                 s = re.sub ('top_of_ChangeLog', '<pre>\n'+ changes  + '\n</pre>\n', s)
216
217         if re.search (header_tag, s) == None:
218                 body = '<BODY BGCOLOR=WHITE TEXT=BLACK>'
219                 s = re.sub ('(?i)<body>', body, s)
220                 if re.search ('(?i)<BODY', s):
221                         s = re.sub ('(?i)<body[^>]*>', body + header, s, 1)
222                 elif re.search ('(?i)<html', s):                
223                         s = re.sub ('(?i)<html>', '<HTML>' + header, s, 1)
224                 else:
225                         s = header + s
226
227                 s = header_tag + '\n' + s
228
229                 if re.search ('(?i)<!DOCTYPE', s) == None:
230                         doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
231                         s = doctype + s
232
233         if re.search (footer_tag, s) == None:
234                 s = s + footer_tag + '\n'
235
236                 if re.search ('(?i)</body', s):
237                         s = re.sub ('(?i)</body>', footer + '</BODY>', s, 1)
238                 elif re.search ('(?i)</html', s):               
239                         s = re.sub ('(?i)</html>', footer + '</HTML>', s, 1)
240                 else:
241                         s = s + footer
242
243
244         #URUGRGOUSNGUOUNRIU
245         index = index_url
246         top = top_url
247         if os.path.basename (f) == "index.html":
248                 cwd = os.getcwd ()
249                 if os.path.basename (cwd) == "topdocs":
250                         index = "index.html"
251                         top = ""
252
253                 # don't cause ///////index.html entries in log files.
254                 #       index = "./index.html"
255                 #       top = "./"
256
257
258         versiontup = string.split(package_version, '.')
259         branch_str = 'stable-branch'
260         if string.atoi ( versiontup[1]) %  2:
261                 branch_str = 'development-branch'
262
263         wiki_page = ('v%s.%s-' % (versiontup[0], versiontup[1]) +  f)
264         wiki_page = re.sub ('out-www/', '', wiki_page)
265         wiki_page = re.sub ('/', '-', wiki_page) 
266         wiki_page = re.sub (r'\.-', '', wiki_page) 
267         wiki_page = re.sub ('.html', '', wiki_page)
268
269         subst = globals ()
270         subst.update (locals())
271         
272         
273         s = s % subst
274         
275         # urg
276         # maybe find first node?
277         fallback_web_title = '-- --'
278
279         # ugh, python2.[12] re is broken.
280         #m = re.match ('.*?<title>\(.*?\)</title>', s, re.DOTALL)
281         m = re.match ('[.\n]*?<title>([.\n]*?)</title>', s)
282         if m:
283                 fallback_web_title = m.group (1)
284         s = re.sub ('@WEB-TITLE@', fallback_web_title, s)
285         
286         s = remove_self_ref (s)
287
288         open (f, 'w').write (s)
289
290
291 for f in files:
292         do_file (f)
293