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