12 # This is to try to make the docball not too big with almost duplicate files
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',
18 'Documentation/topdocs',
19 'Documentation/bibliography',
20 'Documentation/out-www/THANKS',
21 'Documentation/out-www/DEDICATION',
31 <div style="background-color: #e8ffe8; padding: 2; border: #c0ffc0 1px solid;">
34 %(footer_name_version)s
37 %(footer_report_errors)s </address>
39 %(footer_suggest_docs)s
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 = '''
61 %(language_available)s
68 html_re = re.compile ('(.*?)(?:[.]([^/.]*))?[.]html$')
71 def build_pages_dict (filelist):
72 """Build dictionary of available translations of each page"""
78 if len (g) <= 1 or g[1] == None:
82 if not g[0] in pages_dict.keys():
83 pages_dict[g[0]] = [e]
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')
94 ## Windows does not support symlinks.
95 # This function avoids creating symlinks for splitted HTML manuals
96 # Get rid of symlinks in GNUmakefile.in (local-WWW-post)
97 # this also fixes missing PNGs only present in translated docs
98 def hack_urls (s, prefix):
99 if splitted_docs_re.match (prefix):
100 s = re.sub ('(href|src)="(../lily-.*?|.*?[.]png)"', '\\1="../\\2"', s)
102 # fix Snippets xrefs ad hoc
103 s = snippets_ref_re.sub ('href="source/input/lsr/lilypond-snippets', s)
105 source_path = os.path.join (os.path.dirname (prefix), 'source')
106 if not os.path.islink (source_path):
108 source_val = os.readlink (source_path)
109 return re.sub ('href="source/(.*?)"', lambda m: source_links_replace (m, source_val), s)
112 """Add header (<BODY> and doctype)"""
113 if re.search (header_tag, s) == None:
114 body = '<BODY BGCOLOR=WHITE TEXT=BLACK>'
115 s = re.sub ('(?i)<body>', body, s)
116 if re.search ('(?i)<BODY', s):
117 s = re.sub ('(?i)<body[^>]*>', body + header, s, 1)
118 elif re.search ('(?i)<html', s):
119 s = re.sub ('(?i)<html>', '<HTML>' + header, s, 1)
123 s = header_tag + '\n' + s
125 if re.search ('(?i)<!DOCTYPE', s) == None:
126 doctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">\n'
132 # maybe find first node?
133 fallback_web_title = '-- --'
134 m = re.match ('.*?<title>(.*?)</title>', s, re.DOTALL)
136 fallback_web_title = m.group (1)
137 s = re.sub ('@WEB-TITLE@', fallback_web_title, s)
140 info_nav_bar = re.compile (r'<div class="node">\s*<p>\s*<a name=".+?"></a>(.+?)<hr>\s*</div>', re.M | re.S)
141 info_footnote_hr = re.compile (r'<hr>\s*(</div>)?\s*</body>', re.M | re.I)
146 also add navigation bar to bottom of Info HTML pages"""
147 m = info_nav_bar.search (s)
149 # avoid duplicate <hr> in case there are footnotes at the end of the Info HTML page
150 if info_footnote_hr.search (s):
151 custom_footer = '<div class="node">\n<p>' + m.group (1) + '</div>\n' + footer
153 custom_footer = '<br><hr>\n<div class="node">\n<p>' + m.group (1) + '</div>\n' + footer
155 custom_footer = footer
156 if re.search ('(?i)</body', s):
157 s = re.sub ('(?i)</body>', footer_tag + custom_footer + '\n' + '</BODY>', s, 1)
158 elif re.search ('(?i)</html', s):
159 s = re.sub ('(?i)</html>', footer_tag + custom_footer + '\n' + '</HTML>', s, 1)
161 s += footer_tag + custom_footer + '\n'
164 def find_translations (prefix, lang_ext):
165 """find available translations of a page"""
168 for l in langdefs.LANGUAGES:
171 if e in pages_dict[prefix]:
173 elif lang_ext == '' and l.enabled and reduce (lambda x, y: x and y, [not prefix.startswith (s) for s in non_copied_pages]):
174 # English version of missing translated pages will be written
176 return available, missing
178 def process_links (s, prefix, lang_ext, file_name, missing, target):
180 if target == 'online':
181 # Strip .html, .png suffix for auto language selection (content
182 # negotiation). The menu must keep the full extension, so do
183 # this before adding the menu.
184 page_flavors[file_name] = [lang_ext, re.sub (
185 '''(href|src)=[\'"]([^/][.]*[^.:\'"]*)(.html|.png)(#[^"\']*|)[\'"]''',
187 elif target == 'offline':
188 # in LANG doc index: don't rewrite .html suffixes as not all .LANG.html pages exist
189 # the doc index should be translated and contain the right links
190 if prefix == 'Documentation/out-www/index':
191 page_flavors[file_name] = [lang_ext, s]
193 page_flavors[file_name] = [lang_ext, s]
195 page_flavors[langdefs.lang_file_name (prefix, e, '.html')] = [e, re.sub (
196 '''href=[\'"]([^/][.]*[^.:\'"]*)(.html)(#[^"\']*|)[\'"]''',
197 'href="\\1.' + e + '\\2\\3"', s)]
199 page_flavors[file_name] = [lang_ext, re.sub (
200 '''href=[\'"]([^/][.]*[^.:\'"]*)(.html)(#[^"\']*|)[\'"]''',
201 'href="\\1.' + lang_ext + '\\2\\3"', s)]
204 def add_menu (page_flavors, prefix, available, target, translation):
205 for k in page_flavors.keys():
208 if page_flavors[k][0] != '':
209 t = translation[page_flavors[k][0]]
212 for lang in available:
213 lang_file = lang.file_name (os.path.basename (prefix), '.html')
214 if language_menu != '':
215 language_menu += ', '
216 language_menu += '<a href="%s">%s</a>' % (lang_file, t (lang.name))
217 if target == 'offline':
218 browser_language = ''
219 elif target == 'online':
220 browser_language = t (browser_lang) % browser_language_url
222 language_available = t (lang_available) % language_menu
223 languages = LANGUAGES_TEMPLATE % vars ()
224 # put language menu before '</body>' and '</html>' tags
225 if re.search ('(?i)</body', page_flavors[k][1]):
226 page_flavors[k][1] = re.sub ('(?i)</body>', languages + '</BODY>', page_flavors[k][1], 1)
227 elif re.search ('(?i)</html', page_flavors[k][1]):
228 page_flavors[k][1] = re.sub ('(?i)</html>', languages + '</HTML>', page_flavors[k][1], 1)
230 page_flavors[k][1] += languages
234 def add_html_footer (translation,
236 package_version = '',
238 name_filter = lambda s: s):
239 """Add header, footer to a number of HTML files
242 translation gettext translations dictionary, with language codes as keys
243 package_name=NAME set package_name to NAME
244 package_version=VERSION set package version to VERSION
245 targets=offline|online set page processing depending on the target
246 offline is for reading HTML pages locally
247 online is for hosting the HTML pages on a website with content
249 name_filter a HTML file name filter
251 localtime = time.strftime ('%c %Z', time.localtime (time.time ()))
253 if re.search ("http://", mail_address):
254 mail_address_url = mail_address
256 mail_address_url= 'mailto:' + mail_address
258 versiontup = package_version.split ('.')
259 branch_str = _doc ('stable-branch')
260 if int (versiontup[1]) % 2:
261 branch_str = _doc ('development-branch')
263 for prefix, ext_list in pages_dict.items ():
264 for lang_ext in ext_list:
265 file_name = langdefs.lang_file_name (prefix, lang_ext, '.html')
266 in_f = open (file_name)
270 s = re.sub ('%', '%%', s)
271 s = hack_urls (s, prefix)
275 if re.search (footer_tag, s) == None:
278 available, missing = find_translations (prefix, lang_ext)
279 page_flavors = process_links (s, prefix, lang_ext, file_name, missing, target)
280 # Add menu after stripping: must not have autoselection for language menu.
281 page_flavors = add_menu (page_flavors, prefix, available, target, translation)
282 subst = dict ([i for i in globals().items() if type (i[1]) is str])
283 subst.update (dict ([i for i in locals().items() if type (i[1]) is str]))
284 for k in page_flavors.keys():
285 if page_flavors[k][0] in translation.keys():
286 for name in subst.keys():
287 subst[name] = translation[page_flavors[k][0]] (subst[name])
288 subst['footer_name_version'] = subst['footer_name_version'] % subst
289 subst['footer_report_errors'] = subst['footer_report_errors'] % subst
290 subst['footer_suggest_docs'] = subst['footer_suggest_docs'] % subst
291 page_flavors[k][1] = page_flavors[k][1] % subst
292 out_f = open (name_filter (k), 'w')
293 out_f.write (page_flavors[k][1])
295 # if the page is translated, a .en.html symlink is necessary for content negotiation
296 if target == 'online' and ext_list != ['']:
297 os.symlink (os.path.basename (prefix) + '.html', name_filter (prefix + '.en.html'))