staff-symbol-referencer.cc: Junk redundant functions.
[lilypond.git] / buildscripts / add_html_footer.py
blob34ada97aac97d2cb7a5cfc8af0c0c481aab23aec
1 #!@PYTHON@
3 """
4 Print a nice footer.
5 """
6 import re
7 import os
8 import time
10 import langdefs
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',
19 'Documentation/bibliography',
20 'Documentation/out-www/THANKS',
21 'Documentation/out-www/DEDICATION',
22 'input/']
24 def _doc (s):
25 return s
27 header = r"""
28 """
30 footer = '''
31 <div style="background-color: #e8ffe8; padding: 2; border: #c0ffc0 1px solid;">
32 <p>
33 <font size="-1">
34 %(footer_name_version)s
35 <br>
36 <address>
37 %(footer_report_errors)s </address>
38 <br>
39 %(footer_suggest_docs)s
40 </font>
41 </p>
42 </div>
43 '''
44 footer_name_version = _doc ('This page is for %(package_name)s-%(package_version)s (%(branch_str)s).')
45 footer_report_errors = _doc ('Report errors to <a href="%(mail_address_url)s">%(mail_address)s</a>.')
46 # ugh, must not have "_doc" in strings because it is naively replaced with "_" in hacked gettext process
47 footer_suggest_docs = _doc ('Your <a href="%(suggest_Docs_url)s">suggestions for the documentation</a> are welcome.')
49 mail_address = 'http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs'
50 suggest_Docs_url = 'http://lilypond.org/web/devel/participating/documentation-adding'
52 header_tag = '<!-- header_tag -->'
53 footer_tag = '<!-- footer_tag -->'
55 lang_available = _doc ("Other languages: %s.")
56 browser_lang = _doc ('About <A HREF="%s">automatic language selection</A>.')
57 browser_language_url = "/web/about/browser-language"
59 LANGUAGES_TEMPLATE = '''
60 <P>
61 %(language_available)s
62 <BR>
63 %(browser_language)s
64 </P>
65 '''
68 html_re = re.compile ('(.*?)(?:[.]([^/.]*))?[.]html$')
69 pages_dict = {}
71 def build_pages_dict (filelist):
72 """Build dictionary of available translations of each page"""
73 global pages_dict
74 for f in filelist:
75 m = html_re.match (f)
76 if m:
77 g = m.groups()
78 if len (g) <= 1 or g[1] == None:
79 e = ''
80 else:
81 e = g[1]
82 if not g[0] in pages_dict:
83 pages_dict[g[0]] = [e]
84 else:
85 pages_dict[g[0]].append (e)
87 def source_links_replace (m, source_val):
88 return 'href="' + os.path.join (source_val, m.group (1)) + '"'
90 splitted_docs_re = re.compile ('(input/lsr/out-www/lilypond-snippets|Documentation/user/out-www/(lilypond|music-glossary|lilypond-program|lilypond-learning))/')
92 snippets_ref_re = re.compile (r'href="(\.\./)?lilypond-snippets')
93 user_ref_re = re.compile (r'href="(?:\.\./)?lilypond(|-internals|-learning|-program)')
95 ## Windows does not support symlinks.
96 # This function avoids creating symlinks for splitted HTML manuals
97 # Get rid of symlinks in GNUmakefile.in (local-WWW-post)
98 # this also fixes missing PNGs only present in translated docs
99 def hack_urls (s, prefix):
100 if splitted_docs_re.match (prefix):
101 s = re.sub ('(href|src)="(../lily-.*?|.*?[.]png)"', '\\1="../\\2"', s)
103 # fix xrefs between documents in different directories ad hoc
104 if 'user/out-www/lilypond' in prefix:
105 s = snippets_ref_re.sub ('href="source/input/lsr/lilypond-snippets', s)
106 elif 'input/lsr' in prefix:
107 s = user_ref_re.sub ('href="source/Documentation/user/lilypond\\1', s)
109 source_path = os.path.join (os.path.dirname (prefix), 'source')
110 if not os.path.islink (source_path):
111 return s
112 source_val = os.readlink (source_path)
113 return re.sub ('href="source/(.*?)"', lambda m: source_links_replace (m, source_val), s)
115 def add_header (s):
116 """Add header (<BODY> and doctype)"""
117 if re.search (header_tag, s) == None:
118 body = '<BODY BGCOLOR=WHITE TEXT=BLACK>'
119 s = re.sub ('(?i)<body>', body, s)
120 if re.search ('(?i)<BODY', s):
121 s = re.sub ('(?i)<body[^>]*>', body + header, s, 1)
122 elif re.search ('(?i)<html', s):
123 s = re.sub ('(?i)<html>', '<HTML>' + header, s, 1)
124 else:
125 s = header + s
127 s = header_tag + '\n' + s
129 if re.search ('(?i)<!DOCTYPE', s) == None:
130 doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
131 s = doctype + s
132 return s
134 def add_title (s):
135 # urg
136 # maybe find first node?
137 fallback_web_title = '-- --'
138 m = re.match ('.*?<title>(.*?)</title>', s, re.DOTALL)
139 if m:
140 fallback_web_title = m.group (1)
141 s = re.sub ('@WEB-TITLE@', fallback_web_title, s)
142 return s
144 info_nav_bar = re.compile (r'<div class="node">\s*<p>\s*<a name=".+?"></a>(.+?)<hr>\s*</div>', re.M | re.S)
145 info_footnote_hr = re.compile (r'<hr>\s*(</div>)?\s*</body>', re.M | re.I)
147 def add_footer (s):
148 """add footer
150 also add navigation bar to bottom of Info HTML pages"""
151 m = info_nav_bar.search (s)
152 if m:
153 # avoid duplicate <hr> in case there are footnotes at the end of the Info HTML page
154 if info_footnote_hr.search (s):
155 custom_footer = '<div class="node">\n<p>' + m.group (1) + '</div>\n' + footer
156 else:
157 custom_footer = '<br><hr>\n<div class="node">\n<p>' + m.group (1) + '</div>\n' + footer
158 else:
159 custom_footer = footer
160 if re.search ('(?i)</body', s):
161 s = re.sub ('(?i)</body>', footer_tag + custom_footer + '\n' + '</BODY>', s, 1)
162 elif re.search ('(?i)</html', s):
163 s = re.sub ('(?i)</html>', footer_tag + custom_footer + '\n' + '</HTML>', s, 1)
164 else:
165 s += footer_tag + custom_footer + '\n'
166 return s
168 def find_translations (prefix, lang_ext):
169 """find available translations of a page"""
170 available = []
171 missing = []
172 for l in langdefs.LANGUAGES:
173 e = l.webext
174 if lang_ext != e:
175 if e in pages_dict[prefix]:
176 available.append (l)
177 elif lang_ext == '' and l.enabled and reduce (lambda x, y: x and y, [not prefix.startswith (s) for s in non_copied_pages]):
178 # English version of missing translated pages will be written
179 missing.append (e)
180 return available, missing
182 def process_links (s, prefix, lang_ext, file_name, missing, target):
183 page_flavors = {}
184 if target == 'online':
185 # Strip .html, .png suffix for auto language selection (content
186 # negotiation). The menu must keep the full extension, so do
187 # this before adding the menu.
188 page_flavors[file_name] = [lang_ext, re.sub (
189 '''(href|src)=[\'"]([^/][.]*[^.:\'"]*)(.html|.png)(#[^"\']*|)[\'"]''',
190 '\\1="\\2\\4"', s)]
191 elif target == 'offline':
192 # in LANG doc index: don't rewrite .html suffixes as not all .LANG.html pages exist
193 # the doc index should be translated and contain the right links
194 if prefix == 'Documentation/out-www/index':
195 page_flavors[file_name] = [lang_ext, s]
196 elif lang_ext == '':
197 page_flavors[file_name] = [lang_ext, s]
198 for e in missing:
199 page_flavors[langdefs.lang_file_name (prefix, e, '.html')] = [e, re.sub (
200 '''href=[\'"]([^/][.]*[^.:\'"]*)(.html)(#[^"\']*|)[\'"]''',
201 'href="\\1.' + e + '\\2\\3"', s)]
202 else:
203 page_flavors[file_name] = [lang_ext, re.sub (
204 '''href=[\'"]([^/][.]*[^.:\'"]*)(.html)(#[^"\']*|)[\'"]''',
205 'href="\\1.' + lang_ext + '\\2\\3"', s)]
206 return page_flavors
208 def add_menu (page_flavors, prefix, available, target, translation):
209 for k in page_flavors:
210 language_menu = ''
211 languages = ''
212 if page_flavors[k][0] != '':
213 t = translation[page_flavors[k][0]]
214 else:
215 t = _doc
216 for lang in available:
217 lang_file = lang.file_name (os.path.basename (prefix), '.html')
218 if language_menu != '':
219 language_menu += ', '
220 language_menu += '<a href="%s">%s</a>' % (lang_file, t (lang.name))
221 if target == 'offline':
222 browser_language = ''
223 elif target == 'online':
224 browser_language = t (browser_lang) % browser_language_url
225 if language_menu:
226 language_available = t (lang_available) % language_menu
227 languages = LANGUAGES_TEMPLATE % vars ()
228 # put language menu before '</body>' and '</html>' tags
229 if re.search ('(?i)</body', page_flavors[k][1]):
230 page_flavors[k][1] = re.sub ('(?i)</body>', languages + '</BODY>', page_flavors[k][1], 1)
231 elif re.search ('(?i)</html', page_flavors[k][1]):
232 page_flavors[k][1] = re.sub ('(?i)</html>', languages + '</HTML>', page_flavors[k][1], 1)
233 else:
234 page_flavors[k][1] += languages
235 return page_flavors
238 def add_html_footer (package_name = '',
239 package_version = '',
240 target = 'offline',
241 name_filter = lambda s: s):
242 """Add header, footer to a number of HTML files
244 Arguments:
245 package_name=NAME set package_name to NAME
246 package_version=VERSION set package version to VERSION
247 targets=offline|online set page processing depending on the target
248 offline is for reading HTML pages locally
249 online is for hosting the HTML pages on a website with content
250 negotiation
251 name_filter a HTML file name filter
253 translation = langdefs.translation
254 localtime = time.strftime ('%c %Z', time.localtime (time.time ()))
256 if re.search ("http://", mail_address):
257 mail_address_url = mail_address
258 else:
259 mail_address_url= 'mailto:' + mail_address
261 versiontup = package_version.split ('.')
262 branch_str = _doc ('stable-branch')
263 if int (versiontup[1]) % 2:
264 branch_str = _doc ('development-branch')
266 for prefix, ext_list in pages_dict.items ():
267 for lang_ext in ext_list:
268 file_name = langdefs.lang_file_name (prefix, lang_ext, '.html')
269 in_f = open (file_name)
270 s = in_f.read()
271 in_f.close()
273 s = re.sub ('%', '%%', s)
274 s = hack_urls (s, prefix)
275 s = add_header (s)
277 ### add footer
278 if re.search (footer_tag, s) == None:
279 s = add_footer (s)
281 available, missing = find_translations (prefix, lang_ext)
282 page_flavors = process_links (s, prefix, lang_ext, file_name, missing, target)
283 # Add menu after stripping: must not have autoselection for language menu.
284 page_flavors = add_menu (page_flavors, prefix, available, target, translation)
285 subst = dict ([i for i in globals().items() if type (i[1]) is str])
286 subst.update (dict ([i for i in locals().items() if type (i[1]) is str]))
287 for k in page_flavors:
288 if page_flavors[k][0] in translation:
289 for name in subst:
290 subst[name] = translation[page_flavors[k][0]] (subst[name])
291 subst['footer_name_version'] = subst['footer_name_version'] % subst
292 subst['footer_report_errors'] = subst['footer_report_errors'] % subst
293 subst['footer_suggest_docs'] = subst['footer_suggest_docs'] % subst
294 page_flavors[k][1] = page_flavors[k][1] % subst
295 out_f = open (name_filter (k), 'w')
296 out_f.write (page_flavors[k][1])
297 out_f.close()
298 # if the page is translated, a .en.html symlink is necessary for content negotiation
299 if target == 'online' and ext_list != ['']:
300 os.symlink (os.path.basename (prefix) + '.html', name_filter (prefix + '.en.html'))