]> git.donarmstrong.com Git - lilypond.git/blob - stepmake/bin/add-html-footer.py
patch::: 1.3.60.jcn1
[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 fullname = "unknown"
14 index_file=''
15 changelog_file=''
16 package_name = ''
17 package_version = ''
18
19 mail_address = '(address unknown)'
20 try:
21         mail_address= os.environ['MAILADDRESS']
22 except KeyError:
23         pass
24
25 webmaster= mail_address
26 try:
27         webmaster= os.environ['WEBMASTER']
28 except KeyError:
29         pass
30
31 header_file = ''
32 footer_file = ''
33 default_header = r"""
34 """
35
36 default_footer = r"""<hr>Please take me <a href=%s>back to the index</a>
37 of %s
38 <!-- package name %s>
39  <!-- webmaster fields. %s %s>
40 """
41
42 builtstr = r"""<hr><font size=-1>
43 This page was built from %s-%s by 
44 <address><br>%s &lt<a href="mailto:%s">%s</a>&gt,  %s.</address><p></font>"""
45
46
47 def gulp_file (f):
48         try:
49                 i = open(f)
50                 i.seek (0, 2)
51                 n = i.tell ()
52                 i.seek (0,0)
53         except:
54                 sys.stderr.write ("can't open file: %s\n" % f)
55                 return ''
56         s = i.read (n)
57         if len (s) <= 0:
58                 sys.stderr.write ("gulped empty file: %s\n" % f)
59         i.close ()
60         return s
61
62 def help ():
63         sys.stdout.write (r"""Usage: add-html-footer [OPTION]... HTML-FILE
64 Add header, footer and top of ChangLog file (up to the ********) to HTML-FILE
65
66 Options:
67   --changelog=FILE          use FILE as ChangeLog [ChangeLog]
68   --footer=FILE             use FILE as footer
69   --header=FILE             use FILE as header
70   -h, --help                print this help
71   --index=URL               set homepage to URL
72   --name=NAME               set package_name to NAME
73   --version=VERSION         set package version to VERSION
74 """)
75         sys.exit (0)
76
77 (options, files) = getopt.getopt(sys.argv[1:], 'h', [
78         'changelog=', 'footer=', 'header=', 'help', 'index=',
79         'name=', 'version=']) 
80
81 for opt in options:
82         o = opt[0]
83         a = opt[1]
84         if o == '--changelog':
85                 changelog_file = a
86         elif o == '--footer':
87                 footer_file = a
88         elif o == '--header':
89                 header_file = a
90         elif o == '-h' or o == '--help':
91                 help ()
92         elif o == '--index':
93                 index_file = a
94         elif o == '--name':
95                 package_name = a
96         elif o == '--version':
97                 package_version = a
98         else:
99                 raise 'unknown opt ', o
100
101 def set_vars ():
102         global fullname
103         os.environ["CONFIGSUFFIX"] = 'www';
104         if os.name == 'nt':
105                 import ntpwd
106                 pw = ntpwd.getpwname(os.environ['USERNAME'])
107         else:
108                 import pwd
109                 pw = pwd.getpwuid (os.getuid());
110
111         f = pw[4]
112         f = string.split (f, ',')[0]
113         fullname = f 
114
115 #burp
116 def compose_header ():
117         global default_header
118         head = default_header
119         if header_file:
120                 head = gulp_file (header_file)
121         return head
122
123 def compose_footer (index):
124         global default_footer
125         foot = default_footer
126
127         if footer_file:
128                 foot = gulp_file (footer_file)
129
130         s = foot % (index, package_name, package_name, webmaster, webmaster)
131         s = s + builtstr % (package_name, package_version, fullname,
132                             mail_address, mail_address, 
133                             time.strftime ('%c %Z', time.localtime (time.time ())))
134         return s
135
136 set_vars ()
137 header = compose_header ()
138 footer = compose_footer (index_file)
139 header_tag = '<! header_tag >'
140 footer_tag = '<! footer_tag >'
141
142 def do_file (s):
143         if changelog_file:
144                 changes = gulp_file (changelog_file)
145                 # urg?
146                 #m = re.search ('^\\\\*\\\\*', changes)
147                 m = re.search (r'\*\*\*', changes)
148                 if m:
149                         changes = changes[:m.start (0)]
150                 s = re.sub ('top_of_ChangeLog', '<XMP>\n'+ changes  + '\n</XMP>\n', s)
151
152         if re.search (header_tag, s) == None:
153                 body='<BODY BGCOLOR=WHITE><FONT COLOR=BLACK>'
154                 s = re.sub ('(?i)<body>', body, s)
155                 if re.search ('(?i)<BODY', s):
156                         s = re.sub ('(?i)<body[^>]*>', body + header, s)
157                 elif re.search ('(?i)<html', s):                
158                         s = re.sub ('(?i)<html>', '<HTML>' + header, s)
159                 else:
160                         s = header + s
161
162                 s = header_tag + s
163
164         if re.search (footer_tag, s) == None:
165                 s = s + footer_tag
166
167                 if re.search ('(?i)</body', s):
168                         s = re.sub ('(?i)</body>', footer + '</BODY>', s)
169                 elif re.search ('(?i)</html', s):               
170                         s = re.sub ('(?i)</html>', footer + '</HTML>', s)
171                 else:
172                         s = s + footer
173
174         return s
175
176
177 for f in files:
178         s = gulp_file (f)
179         s = do_file (s)
180         open (f, 'w').write (s)
181
182 if 0:
183         title = '<HEAD><TITLE>' \
184                 + package_name + ' -- ' + os.path.basename (os.path.splitext(f)[0]) \
185                 + '</TITLE></HEAD>'
186         s = check_tag ('<title>', title, s, 0)
187
188         s = check_tag ('<html', '', s, 0)
189         if regex.search ('<HTML', s) == -1:
190                 s = '<HTML>\n' + s
191         s = check_tag ('</html>', '</HTML>', s, 1)
192
193         dump_file (f, s)
194
195