]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/add_html_footer.py
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / buildscripts / add_html_footer.py
1 #!@PYTHON@
2
3 """
4 Print a nice footer.
5 """
6 import re
7 import os
8 import time
9
10 import langdefs
11
12 # This is to try to make the docball not too big with almost duplicate files
13 # see process_links()
14 non_copied_pages = ['Documentation/user/out-www/lilypond-big-page',
15                     'Documentation/user/out-www/lilypond-internals-big-page',
16                     'Documentation/user/out-www/music-glossary-big-page',
17                     'out-www/examples',
18                     'Documentation/topdocs/out-www/NEWS',
19                     'Documentation/topdocs/out-www/INSTALL',
20                     'Documentation/bibliography/out-www/index',
21                     'Documentation/bibliography/out-www/engraving',
22                     'Documentation/bibliography/out-www/colorado',
23                     'Documentation/bibliography/out-www/computer-notation'
24                     'Documentation/out-www/THANKS',
25                     'Documentation/out-www/DEDICATION',
26                     'Documentation/topdocs/out-www/AUTHORS']
27
28 header = r"""
29 """
30
31 footer = r'''
32 <div style="background-color: #e8ffe8; padding: 2; border: #c0ffc0 1px solid;">
33 <p>
34 <font size="-1">
35 This page is for %(package_name)s-%(package_version)s (%(branch_str)s). <br>
36 </font>
37 <address><font size="-1">
38 Report errors to <a href="%(mail_address_url)s">%(mail_address)s</a>.</font></address>
39 </p>
40 </div>
41 '''
42
43 mail_address = 'http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs'
44
45 header_tag = '<!-- header_tag -->'
46 footer_tag = '<!-- footer_tag -->'
47
48 def _ (s):
49     return s
50
51 language_available = _ ("Other languages: %s.") % "%(language_menu)s"
52 browser_language = _ ("Using <A HREF='%s'>automatic language selection</A>.") \
53            % "/web/about/browser-language"
54
55 LANGUAGES_TEMPLATE = '''\
56 <P>
57  %(language_available)s
58  <BR>
59  %(browser_language)s
60 </P>
61 ''' % vars ()
62
63
64 html_re = re.compile ('(.*?)(?:[.]([^/.]*))?[.]html$')
65 pages_dict = {}
66
67 def build_pages_dict (filelist):
68     """Build dictionnary of available translations of each page"""
69     global pages_dict
70     for f in filelist:
71         m = html_re.match (f)
72         if m:
73             g = m.groups()
74             if len (g) <= 1 or g[1] == None:
75                 e = ''
76             else:
77                 e = g[1]
78             if not g[0] in pages_dict.keys():
79                 pages_dict[g[0]] = [e]
80             else:
81                 pages_dict[g[0]].append (e)
82
83 def add_header (s):
84     """Add header (<BODY> and doctype)"""
85     if re.search (header_tag, s) == None:
86         body = '<BODY BGCOLOR=WHITE TEXT=BLACK>'
87         s = re.sub ('(?i)<body>', body, s)
88         if re.search ('(?i)<BODY', s):
89             s = re.sub ('(?i)<body[^>]*>', body + header, s, 1)
90         elif re.search ('(?i)<html', s):                
91             s = re.sub ('(?i)<html>', '<HTML>' + header, s, 1)
92         else:
93             s = header + s
94
95         s = header_tag + '\n' + s
96
97         if re.search ('(?i)<!DOCTYPE', s) == None:
98             doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
99             s = doctype + s
100         return s
101
102 def info_external_ref_remove (s):
103     """Remove info's annoying's indication of referencing external document"""
104     return re.sub (' \((lilypond|lilypond-internals|music-glossary)\)</a>', '</a>', s)
105
106 def add_title (s):
107     # urg
108     # maybe find first node?
109     fallback_web_title = '-- --'
110     m = re.match ('.*?<title>(.*?)</title>', s, re.DOTALL)
111     if m:
112         fallback_web_title = m.group (1)
113     s = re.sub ('@WEB-TITLE@', fallback_web_title, s)
114     return s
115
116 info_nav_bar = re.compile (r'<div class="node">\s*<p>\s*<a name=".+?"></a>(.+?)<hr>\s*</div>', re.M | re.S)
117
118 def add_footer (s):
119     """add footer
120
121 also add navigation bar to bottom of Info HTML pages"""
122     m = info_nav_bar.search (s)
123     if m:
124         custom_footer = '<br><hr>\n<div class="node">\n<p>' + m.group (1) + '</div>\n' + footer
125     else:
126         custom_footer = footer
127     if re.search ('(?i)</body', s):
128         s = re.sub ('(?i)</body>', footer_tag + custom_footer + '\n' + '</BODY>', s, 1)
129     elif re.search ('(?i)</html', s):                
130         s = re.sub ('(?i)</html>', footer_tag + custom_footer + '\n' + '</HTML>', s, 1)
131     else:
132         s += footer_tag + custom_footer + '\n'
133     return s
134
135 def find_translations (prefix, lang_ext):
136     """find available translations of a page"""
137     available = []
138     missing = []
139     for l in langdefs.LANGUAGES:
140         e = l.webext
141         if lang_ext != e:
142             if e in pages_dict[prefix]:
143                 available.append (l)
144             elif lang_ext == '' and l.enabled and not prefix in non_copied_pages:
145                 # English version of missing translated pages will be written
146                 missing.append (e)
147     return available, missing
148
149 def process_links (s, prefix, lang_ext, file_name, missing, target):
150     page_flavors = {}
151     if target == 'online':
152         # Strip .html, .png suffix for auto language selection (content
153         # negotiation).  The menu must keep the full extension, so do
154         # this before adding the menu.
155         page_flavors[file_name] = re.sub (
156             '''(href|src)=[\'"]([^/][.]*[^.:\'"]*)(.html|.png)(#[^"\']*|)[\'"]''',
157             '\\1="\\2\\4"', s)
158     elif target == 'offline':
159         # in LANG doc index: don't rewrite .html suffixes as not all .LANG.html pages exist
160         # the doc index should be translated and contain the right links
161         if prefix == 'Documentation/out-www/index':
162             page_flavors[file_name] = s
163         elif lang_ext == '':
164             page_flavors[file_name] = s
165             for e in missing:
166                 page_flavors[langdefs.lang_file_name (prefix, e, '.html')] = re.sub (
167                     '''href=[\'"]([^/][.]*[^.:\'"]*)(.html)(#[^"\']*|)[\'"]''',
168                     'href="\\1.' + e + '\\2\\3"', s)
169         else:
170             page_flavors[file_name] = re.sub (
171                 '''href=[\'"]([^/][.]*[^.:\'"]*)(.html)(#[^"\']*|)[\'"]''',
172                 'href="\\1.' + lang_ext + '\\2\\3"', s)
173     return page_flavors
174
175 def add_menu (page_flavors, prefix, available):
176     language_menu = ''
177     for lang in available:
178         lang_file = lang.file_name (os.path.basename (prefix), '.html')
179         if language_menu != '':
180             language_menu += ', '
181         language_menu += '<a href="%s">%s</a>' % (lang_file, lang.name)
182
183     languages = ''
184     if language_menu:
185         languages = LANGUAGES_TEMPLATE % vars ()
186
187     # put language menu before '</body>' and '</html>' tags
188     for k in page_flavors.keys():
189         if re.search ('(?i)</body', page_flavors[k]):
190             page_flavors[k] = re.sub ('(?i)</body>', languages + '</BODY>', page_flavors[k], 1)
191         elif re.search ('(?i)</html', page_flavors[k]):                
192             page_flavors[k] = re.sub ('(?i)</html>', languages + '</HTML>', page_flavors[k], 1)
193         else:
194             page_flavors[k] += languages
195     return page_flavors
196
197
198 def add_html_footer (package_name = '',
199                      package_version = '',
200                      target = 'offline',
201                      name_filter = lambda s: s):
202     """Add header, footer to a number of HTML files
203
204     Arguments:
205      package_name=NAME         set package_name to NAME
206      package_version=VERSION   set package version to VERSION
207      targets=offline|online    set page processing depending on the target
208           offline is for reading HTML pages locally
209           online is for hosting the HTML pages on a website with content
210             negotiation
211      name_filter               a HTML file name filter
212     """
213     localtime = time.strftime ('%c %Z', time.localtime (time.time ()))
214
215     if re.search ("http://", mail_address):
216         mail_address_url = mail_address
217     else:
218         mail_address_url= 'mailto:' + mail_address
219
220     versiontup = package_version.split ('.')
221     branch_str = 'stable-branch'
222     if int ( versiontup[1]) %  2:
223         branch_str = 'development-branch'
224
225     for prefix, ext_list in pages_dict.items ():
226         for lang_ext in ext_list:
227             file_name = langdefs.lang_file_name (prefix, lang_ext, '.html')
228             in_f = open (file_name)
229             s = in_f.read()
230             in_f.close()
231
232             s = re.sub ('%', '%%', s)
233             s = add_header (s)
234             # seems to be no more needed
235             # s = info_external_ref_remove (s)
236
237             ### add footer
238             if re.search (footer_tag, s) == None:
239                 s = add_footer (s)
240                 available, missing = find_translations (prefix, lang_ext)
241                 page_flavors = process_links (s, prefix, lang_ext, file_name, missing, target)
242                 # Add menu after stripping: must not have autoselection for language menu.
243                 page_flavors = add_menu (page_flavors, prefix, available)
244             # urg, this stuff is oudated and seems useless, let's disable it
245             #else:
246             #    for e in [l.webext for l in langdefs.LANGUAGES]:
247             #        if not e in pages_dict[prefix]:
248             #            page_flavors[langdefs.lang_file_name (prefix, e, '.html')] = s
249
250             subst = globals ()
251             subst.update (locals())
252             for k in page_flavors.keys():
253                 page_flavors[k] = page_flavors[k] % subst
254
255                 out_f = open (name_filter (k), 'w')
256                 out_f.write (page_flavors[k])
257                 out_f.close()
258         # if the page is translated, a .en.html symlink is necessary for content negotiation
259         if target == 'online' and ext_list != ['']:
260             os.symlink (os.path.basename (prefix) + '.html', name_filter (prefix + '.en.html'))