#!@PYTHON@
"""
Postprocess HTML files:
add footer, tweak links, add language selection menu.
"""
import re
import os
import time
import operator
import langdefs
# This is to try to make the docball not too big with almost duplicate files
# see process_links()
non_copied_pages = ['Documentation/out-www/notation-big-page',
'Documentation/out-www/internals-big-page',
'Documentation/out-www/learning-big-page',
'Documentation/out-www/usage-big-page',
'Documentation/out-www/music-glossary-big-page',
'Documentation/out-www/contributor',
'Documentation/out-www/changes-big-page',
'Documentation/out-www/essay-big-page',
'Documentation/out-www/extending-big-page',
'Documentation/out-www/snippets',
'out-www/examples',
'Documentation/topdocs',
'Documentation/bibliography',
'Documentation/out-www/THANKS',
'Documentation/out-www/DEDICATION',
'input/']
def _doc (s):
return s
header = r"""
"""
footer = '''
'''
web_footer = '''
'''
footer_name_version = _doc ('This page is for %(package_name)s-%(package_version)s (%(branch_str)s).')
# ugh, must not have "_doc" in strings because it is naively replaced with "_" in hacked gettext process
footer_report_links = _doc ('We welcome your aid; please help us by reporting errors to our bug list.')
mail_address = 'http://post.gmane.org/post.php?group=gmane.comp.gnu.lilypond.bugs'
help_us_url = 'http://lilypond.org/help-us.html'
header_tag = ''
header_tag_re = re.compile (header_tag)
footer_tag = ''
footer_tag_re = re.compile (footer_tag)
lang_available = _doc ("Other languages: %s.")
browser_lang = _doc ('About automatic language selection.')
browser_language_url = "/web/about/browser-language"
LANGUAGES_TEMPLATE = '''
%(language_available)s
%(browser_language)s
'''
html_re = re.compile ('(.*?)(?:[.]([^/.]*))?[.]html$')
pages_dict = {}
def build_pages_dict (filelist):
"""Build dictionary of available translations of each page"""
global pages_dict
for f in filelist:
m = html_re.match (f)
if m:
g = m.groups()
if len (g) <= 1 or g[1] == None:
e = ''
else:
e = g[1]
if not g[0] in pages_dict:
pages_dict[g[0]] = [e]
else:
pages_dict[g[0]].append (e)
def source_links_replace (m, source_val):
return 'href="' + os.path.join (source_val, m.group (1)) + '"'
# More hardcoding, yay!
split_docs_re = re.compile('(Documentation/out-www/(automated-engraving|essay|notation|changes|extending|music-glossary|usage|web|learning|snippets|contributor))/')
lily_snippets_re = re.compile ('(href|src)="([0-9a-f]{2}/lily-.*?)"')
pictures_re = re.compile ('src="(pictures/.*?)"')
docindex_link_re = re.compile (r'href="index.html"')
manuals_page_link_re = re.compile (r'href="((?:\.\./)+)Documentation/web/manuals')
## Windows does not support symlinks.
# This function avoids creating symlinks for split HTML manuals
# Get rid of symlinks in GNUmakefile.in (local-WWW-post)
# this also fixes missing PNGs only present in translated docs
def hack_urls (s, prefix, target, is_development_branch):
if split_docs_re.match (prefix):
s = lily_snippets_re.sub ('\\1="../\\2"', s)
s = pictures_re.sub ('src="../\\1"', s)
# we also need to replace in the lsr, which is already processed above!
if 'input/' in prefix or 'Documentation/topdocs' in prefix or \
'Documentation/contributor' in prefix:
# fix the link from the regtest, lsr and topdoc pages to the doc index
# (rewrite prefix to obtain the relative path of the doc index page)
rel_link = re.sub (r'out-www/.*$', '', prefix)
rel_link = re.sub (r'[^/]*/', '../', rel_link)
if 'input/regression' in prefix or 'Documentation/contributor' in prefix:
indexfile = "Documentation/devel"
else:
indexfile = "index"
s = docindex_link_re.sub ('href="' + rel_link + indexfile + '.html\"', s)
# make the "return to doc index" work with the online website.
if target == 'online':
if (('Documentation/contributor' in prefix) or
is_development_branch):
manuals_page = 'development'
else:
manuals_page = 'manuals'
s = manuals_page_link_re.sub (r'href="../../\1website/%s'
% manuals_page, s)
source_path = os.path.join (os.path.dirname (prefix), 'source')
if not os.path.islink (source_path):
return s
source_val = os.readlink (source_path)
return re.sub ('href="source/(.*?)"', lambda m: source_links_replace (m, source_val), s)
body_tag_re = re.compile ('(?i)]*)>')
html_tag_re = re.compile ('(?i)')
doctype_re = re.compile ('(?i)\n'
css_re = re.compile ('(?i)]*)href="[^">]*?lilypond.*\.css"([^>]*)>')
end_head_tag_re = re.compile ('(?i)')
css_link = """
"""
def add_header (s, prefix):
"""Add header (, doctype and CSS)"""
if header_tag_re.search (s) == None:
body = ''
(s, n) = body_tag_re.subn (body + header, s, 1)
if not n:
(s, n) = html_tag_re.subn ('' + header, s, 1)
if not n:
s = header + s
if doctype_re.search (s) == None:
s = doctype + header_tag + '\n' + s
if css_re.search (s) == None:
depth = (prefix.count ('/') - 1) * '../'
s = end_head_tag_re.sub ((css_link % {'rel': depth}) + '', s)
return s
title_tag_re = re.compile ('.*?(.*?)', re.DOTALL)
AT_web_title_re = re.compile ('@WEB-TITLE@')
def add_title (s):
# urg
# maybe find first node?
fallback_web_title = '-- --'
m = title_tag_re.match (s)
if m:
fallback_web_title = m.group (1)
s = AT_web_title_re.sub (fallback_web_title, s)
return s
footer_insert_re = re.compile ('')
end_body_re = re.compile ('(?i)')
end_html_re = re.compile ('(?i)')
def add_footer (s, footer_text):
"""add footer"""
(s, n) = footer_insert_re.subn (footer_text + '\n' + '', s, 1)
if not n:
(s, n) = end_body_re.subn (footer_text + '\n' + '', s, 1)
if not n:
(s, n) = end_html_re.subn (footer_text + '\n' + '