def process_links(s, prefix, lang_ext, file_name, missing, target): page_flavors = {} if target == "online": # Strip .html, suffix for auto language selection (content # negotiation). The menu must keep the full extension, so do # this before adding the menu. page_flavors[file_name] = [lang_ext, online_links_re.sub('\\1="\\2\\4"', s)] elif target == "offline": # in LANG doc index: don't rewrite .html suffixes # as not all .LANG.html pages exist; # the doc index should be translated and contain links with the right suffixes # idem for NEWS if prefix in ("Documentation/out-www/index", "Documentation/topdocs/out-www/NEWS"): page_flavors[file_name] = [lang_ext, s] elif lang_ext == "": page_flavors[file_name] = [lang_ext, s] for e in missing: page_flavors[langdefs.lang_file_name(prefix, e, ".html")] = [ e, offline_links_re.sub('href="\\1.' + e + '\\2\\3"', s), ] else: # For saving bandwidth and disk space, we don't duplicate big pages # in English, so we must process translated big pages links differently. if "big-page" in prefix: page_flavors[file_name] = [ lang_ext, offline_links_re.sub(lambda match: process_i18n_big_page_links(match, prefix, lang_ext), s), ] else: page_flavors[file_name] = [lang_ext, offline_links_re.sub('href="\\1.' + lang_ext + '\\2\\3"', s)] return page_flavors
def process_links(s, prefix, lang_ext, file_name, missing, target): page_flavors = {} if target == 'online': # Strip .html, suffix for auto language selection (content # negotiation). The menu must keep the full extension, so do # this before adding the menu. page_flavors[file_name] = \ [lang_ext, online_links_re.sub ('\\1="\\2\\4"', s)] elif target == 'offline': # in LANG doc index: don't rewrite .html suffixes # as not all .LANG.html pages exist; # the doc index should be translated and contain links with the right suffixes # idem for NEWS if prefix in ('Documentation/out-www/index', 'Documentation/topdocs/out-www/NEWS'): page_flavors[file_name] = [lang_ext, s] elif lang_ext == '': page_flavors[file_name] = [lang_ext, s] for e in missing: page_flavors[langdefs.lang_file_name (prefix, e, '.html')] = \ [e, offline_links_re.sub ('href="\\1.' + e + '\\2\\3"', s)] else: # For saving bandwidth and disk space, we don't duplicate big pages # in English, so we must process translated big pages links differently. if 'big-page' in prefix: page_flavors[file_name] = \ [lang_ext, offline_links_re.sub \ (lambda match: process_i18n_big_page_links (match, prefix, lang_ext), s)] else: page_flavors[file_name] = \ [lang_ext, offline_links_re.sub ('href="\\1.' + lang_ext + '\\2\\3"', s)] return page_flavors
def process_html_files(pages_dict, package_name='', package_version='', target='offline'): """Add header, footer and tweak links to a number of HTML files Arguments: pages_dict: dict of filename => translations package_name=NAME set package_name to NAME package_version=VERSION set package version to VERSION targets=offline|online set page processing depending on the target offline is for reading HTML pages locally online is for hosting the HTML pages on a website with content negotiation """ versiontup = package_version.split('.') branch_str = _doc('stable-branch') if int(versiontup[1]) % 2: branch_str = _doc('development-branch') en_dict = { 'package_name': package_name, 'package_version': package_version, 'branch_str': branch_str, 'help_us_url': 'https://lilypond.org/help-us.html', 'bug_lilypond_url': 'https://lists.gnu.org/mailman/listinfo/bug-lilypond', 'footer_name_version': _doc('This page is for %(package_name)s-' '%(package_version)s (%(branch_str)s).'), 'footer_report_links': _doc('We welcome your aid; please ' '<a href="%(help_us_url)s">help us</a> by ' 'reporting errors to our ' '<a href="%(bug_lilypond_url)s">bug list</a>.'), } # language => (dict of str => str) subst = { '': en_dict, } for l in langdefs.translation: e = langdefs.LANGDICT[l].webext if e: subst[e] = { name: langdefs.translation[l](en_dict[name]) for name in en_dict } # Do deeper string formatting as early as possible, # so only one '%' formatting pass is needed later for e in subst: for k in ['footer_name_version', 'footer_report_links']: subst[e][k] = subst[e][k] % subst[e] for prefix, ext_list in list(pages_dict.items()): for lang_ext in ext_list: file_name = langdefs.lang_file_name(prefix, lang_ext, '.html') dest_time = 0 content = codecs.open(file_name, 'r', 'utf-8').read() content = content.replace('%', '%%') content = add_header(content, prefix) # add sidebar information content = content.replace('<!-- Sidebar Version Tag -->', sidebar_version) available = find_translations(pages_dict, prefix, lang_ext) page_flavors = process_links(pages_dict, content, prefix, lang_ext, file_name, target) # Add menu after stripping: must not have autoselection for language menu. page_flavors = add_menu(page_flavors, prefix, available, target, langdefs.translation) for k in page_flavors: page_flavors[k][1] = page_flavors[k][1] % subst[page_flavors[k] [0]] # Must write to tmp file to avoid touching hardlinked files. out_f = codecs.open(k + ".tmp", 'w', 'utf-8') out_f.write(page_flavors[k][1]) out_f.close() os.rename(k + ".tmp", k) # if the page is translated, a .en.html symlink is necessary for content negotiation if target == 'online' and ext_list != [ '' ] and not os.path.lexists(prefix + '.en.html'): os.symlink(os.path.basename(prefix) + '.html', prefix + '.en.html')
def process_html_files(package_name="", package_version="", target="offline", name_filter=lambda s: s): """Add header, footer and tweak links to a number of HTML files Arguments: package_name=NAME set package_name to NAME package_version=VERSION set package version to VERSION targets=offline|online set page processing depending on the target offline is for reading HTML pages locally online is for hosting the HTML pages on a website with content negotiation name_filter a HTML file name filter """ translation = langdefs.translation localtime = time.strftime("%c %Z", time.localtime(time.time())) if "http://" in mail_address: mail_address_url = mail_address else: mail_address_url = "mailto:" + mail_address versiontup = package_version.split(".") branch_str = _doc("stable-branch") if int(versiontup[1]) % 2: branch_str = _doc("development-branch") # Initialize dictionaries for string formatting subst = {} subst[""] = dict([i for i in globals().items() if type(i[1]) is str]) subst[""].update(dict([i for i in locals().items() if type(i[1]) is str])) for l in translation: e = langdefs.LANGDICT[l].webext if e: subst[e] = {} for name in subst[""]: subst[e][name] = translation[l](subst[""][name]) # Do deeper string formatting as early as possible, # so only one '%' formatting pass is needed later for e in subst: subst[e]["footer_name_version"] = subst[e]["footer_name_version"] % subst[e] subst[e]["footer_report_links"] = subst[e]["footer_report_links"] % subst[e] for prefix, ext_list in pages_dict.items(): for lang_ext in ext_list: file_name = langdefs.lang_file_name(prefix, lang_ext, ".html") in_f = open(file_name) s = in_f.read() in_f.close() s = s.replace("%", "%%") s = hack_urls(s, prefix) s = add_header(s, prefix) ### add footer if footer_tag_re.search(s) == None: if "web" in file_name: s = add_footer(s, footer_tag + web_footer) else: s = add_footer(s, footer_tag + footer) available, missing = find_translations(prefix, lang_ext) page_flavors = process_links(s, prefix, lang_ext, file_name, missing, target) # Add menu after stripping: must not have autoselection for language menu. page_flavors = add_menu(page_flavors, prefix, available, target, translation) for k in page_flavors: page_flavors[k][1] = page_flavors[k][1] % subst[page_flavors[k][0]] out_f = open(name_filter(k), "w") out_f.write(page_flavors[k][1]) out_f.close() # if the page is translated, a .en.html symlink is necessary for content negotiation if target == "online" and ext_list != [""]: os.symlink(os.path.basename(prefix) + ".html", name_filter(prefix + ".en.html"))
def process_html_files(package_name='', package_version='', target='offline', name_filter=lambda s: s): """Add header, footer and tweak links to a number of HTML files Arguments: package_name=NAME set package_name to NAME package_version=VERSION set package version to VERSION targets=offline|online set page processing depending on the target offline is for reading HTML pages locally online is for hosting the HTML pages on a website with content negotiation name_filter a HTML file name filter """ translation = langdefs.translation localtime = time.strftime('%c %Z', time.localtime(time.time())) if "http://" in mail_address: mail_address_url = mail_address else: mail_address_url = 'mailto:' + mail_address versiontup = package_version.split('.') branch_str = _doc('stable-branch') if int(versiontup[1]) % 2: branch_str = _doc('development-branch') # Initialize dictionaries for string formatting subst = {} subst[''] = dict([i for i in globals().items() if type(i[1]) is str]) subst[''].update(dict([i for i in locals().items() if type(i[1]) is str])) for l in translation: e = langdefs.LANGDICT[l].webext if e: subst[e] = {} for name in subst['']: subst[e][name] = translation[l](subst[''][name]) # Do deeper string formatting as early as possible, # so only one '%' formatting pass is needed later for e in subst: subst[e][ 'footer_name_version'] = subst[e]['footer_name_version'] % subst[e] subst[e][ 'footer_report_links'] = subst[e]['footer_report_links'] % subst[e] for prefix, ext_list in pages_dict.items(): for lang_ext in ext_list: file_name = langdefs.lang_file_name(prefix, lang_ext, '.html') source_time = os.path.getmtime(file_name) dest_time = 0 if os.path.exists(name_filter(file_name)): dest_time = os.path.getmtime(name_filter(file_name)) if dest_time < source_time: in_f = open(file_name) s = in_f.read() in_f.close() s = s.replace('%', '%%') s = hack_urls(s, prefix, target, bool(int(versiontup[1]) % 2)) s = add_header(s, prefix) ### add footer if footer_tag_re.search(s) == None: if 'web' in file_name: s = add_footer(s, footer_tag + web_footer) else: s = add_footer(s, footer_tag + footer) available, missing = find_translations(prefix, lang_ext) page_flavors = process_links(s, prefix, lang_ext, file_name, missing, target) # Add menu after stripping: must not have autoselection for language menu. page_flavors = add_menu(page_flavors, prefix, available, target, translation) for k in page_flavors: page_flavors[k][1] = page_flavors[k][1] % subst[ page_flavors[k][0]] out_f = open(name_filter(k), 'w') out_f.write(page_flavors[k][1]) out_f.close() # if the page is translated, a .en.html symlink is necessary for content negotiation if target == 'online' and ext_list != [''] and not os.path.lexists( name_filter(prefix + '.en.html')): os.symlink( os.path.basename(prefix) + '.html', name_filter(prefix + '.en.html'))
def process_html_files (package_name = '', package_version = '', target = 'offline', name_filter = lambda s: s): """Add header, footer and tweak links to a number of HTML files Arguments: package_name=NAME set package_name to NAME package_version=VERSION set package version to VERSION targets=offline|online set page processing depending on the target offline is for reading HTML pages locally online is for hosting the HTML pages on a website with content negotiation name_filter a HTML file name filter """ translation = langdefs.translation localtime = time.strftime ('%c %Z', time.localtime (time.time ())) if "http://" in mail_address: mail_address_url = mail_address else: mail_address_url= 'mailto:' + mail_address versiontup = package_version.split ('.') branch_str = _doc ('stable-branch') if int (versiontup[1]) % 2: branch_str = _doc ('development-branch') # Initialize dictionaries for string formatting subst = {} subst[''] = dict ([i for i in globals ().items() if type (i[1]) is str]) subst[''].update (dict ([i for i in locals ().items() if type (i[1]) is str])) for l in translation: e = langdefs.LANGDICT[l].webext if e: subst[e] = {} for name in subst['']: subst[e][name] = translation[l] (subst[''][name]) # Do deeper string formatting as early as possible, # so only one '%' formatting pass is needed later for e in subst: subst[e]['footer_name_version'] = subst[e]['footer_name_version'] % subst[e] subst[e]['footer_report_links'] = subst[e]['footer_report_links'] % subst[e] for prefix, ext_list in pages_dict.items (): for lang_ext in ext_list: file_name = langdefs.lang_file_name (prefix, lang_ext, '.html') source_time = os.path.getmtime(file_name) dest_time = 0 if os.path.exists(name_filter(file_name)): dest_time = os.path.getmtime(name_filter(file_name)) if dest_time < source_time: in_f = open (file_name) s = in_f.read() in_f.close() s = s.replace ('%', '%%') s = hack_urls (s, prefix, target, bool (int (versiontup[1]) % 2)) s = add_header (s, prefix) ### add sidebar information s = s.replace ('<!-- Sidebar Version Tag -->', sidebar_version) ### add footer if footer_tag_re.search (s) == None: if 'web' in file_name: s = add_footer (s, footer_tag + web_footer) else: s = add_footer (s, footer_tag + footer) available, missing = find_translations (prefix, lang_ext) page_flavors = process_links (s, prefix, lang_ext, file_name, missing, target) # Add menu after stripping: must not have autoselection for language menu. page_flavors = add_menu (page_flavors, prefix, available, target, translation) for k in page_flavors: page_flavors[k][1] = page_flavors[k][1] % subst[page_flavors[k][0]] out_f = open (name_filter (k), 'w') out_f.write (page_flavors[k][1]) out_f.close() # if the page is translated, a .en.html symlink is necessary for content negotiation if target == 'online' and ext_list != [''] and not os.path.lexists (name_filter (prefix + '.en.html')): os.symlink (os.path.basename (prefix) + '.html', name_filter (prefix + '.en.html'))