#!@PYTHON@
#-*- coding: utf-8 -*-
-## This is web_post.py. This script deals with translations
-## in the "make website" target.
+##### This is web_post.py. This script deals with translations
+##### in the "make website" target.
import sys
import os
import glob
-
-#### Translation data
-lang_lookup = {
- 'fr': 'français',
- 'es': 'español',
- '': 'english'
-}
-
-lang_other_langs = {
- 'es': 'Blargle flop: ',
- 'fr': 'lirer des autres langs: ',
- '': 'Other languages: '
-}
-
-
-#### Actual program
-
-#indir, outdir = sys.argv[1:]
-
-# FIXME: looks dangerous!
-indir = sys.argv[1]
-outdir=indir
-
-html_files = glob.glob( os.path.join(indir, '*.html') )
-
-# messy way to get all languages
+import re
+
+###### Translation data, move out, see create-weblinks-itexi.py
+translations = {
+ 'cs': {
+ 'English': 'Česky',
+ 'Other languages': 'Jiné jazyky',
+ },
+ 'de': {
+ 'English': 'Deutsch',
+ 'Other languages': 'Andere Sprachen',
+ },
+ 'es': {
+ 'English': 'Español',
+ 'Other languages': 'Otros idiomas',
+ },
+ 'fr': {
+ 'English': 'Français',
+ 'Other languages': 'Autres langues',
+ },
+ 'hu': {
+ 'English': 'Magyar',
+ 'Other languages': 'Más nyelvek',
+ },
+ 'it': {
+ 'English': 'Italiano',
+ 'Other languages': 'Altre lingue',
+ },
+ 'ja': {
+ 'English': '日本語',
+ 'Other languages': '他の言語',
+ },
+ 'nl': {
+ 'English': 'Nederlands',
+ 'Other languages': 'Andere talen',
+ },
+ 'zh': {
+ 'English': '中文',
+ 'Other languages': '其他语言',
+ },
+ }
+
+# needs at least: make -C po or make -C Documentation/po
+HAVE_GETTEXT = False
+
+#### this breaks on lilypond.org
+# Keep some freakin' gettext compatibility
+#if HAVE_GETTEXT:
+# import lilylib as ly;
+# global _;_=ly._
+#else: # poor mans translation
+# def _ (string, lang=os.environ['LANG']):
+# return translations.get (lang.split ('_')[0], {}).get (string, string)
+
+#### this works on lilypond.org
+def _ (string, lang):
+ return translations.get (lang.split ('_')[0], {}).get (string, string)
+
+
+exclude_manuals = [
+ '/music-glossary',
+ '/snippets',
+ '/internals',
+ '/contributor'
+]
+
+###### Actual program
+
+dir = sys.argv[1]
+
+os.chdir(dir)
+html_files = glob.glob( '*.html' )
+
+
+### messy way to get all languages
langs_set = set()
for file in html_files:
- file_split = file.split('.')
- if (len(file_split) == 2):
- # it's English
- lang = ''
- else:
- # it's a translation
- lang = file_split[1]
- langs_set.add(lang)
+ file_split = file.split('.')
+ if (len(file_split) == 2):
+ # it's English
+ lang = ''
+ elif (len(file_split) == 3):
+ # it's a translation
+ lang = file_split[1]
+ # make sure it's a translated language
+ if lang != "en":
+ langs_set.add(lang)
langs = list(langs_set)
langs.sort()
-def makeFooter(currentLang, currentPage):
- text = "<p id=\"languages\">\n"
- text += lang_other_langs[currentLang]
- for i in range(len(langs)):
- l = langs[i]
- if (l == currentLang):
- continue
- text += "<a href=\""
- text += currentPage
- if (not (l=="")):
- text += "." + l
- text += ".html\">"
- text += lang_lookup[l]
- text += "</a>"
- if (i < len(langs)-2):
- text += ", "
- else:
- text += ".\n"
- # TODO: add link to automatic language selection?
- # still need to include this page in the new webpages somewhere
- text += "</p>\n"
- return text
-
+### helper functions
+def addLangExt(filename, lang, ext):
+ text = filename
+ exclude = 0
+ for dir in exclude_manuals:
+ if (text.find(dir) >= 0):
+ exclude = 1
+ if (not (exclude or (lang==""))):
+ text += "." + lang
+ text += "." + ext
+ return text
+
+def makeFooter (filename, currentLang):
+ footer = '''<p id="languages">
+%(other)s: %(lst)s.
+<br>
+%(browser_language)s
+</p>
+'''
+ def link (lang):
+ str = '''<a href="%(file_name)s">%(language_name)s</a>'''
+ file_name = addLangExt (filename, lang, 'html')
+ language_name = _ ('English', lang)
+ return str % locals ()
+ lst = ', '.join ([link (lang) for lang in langs if lang != currentLang])
+ other = _ ('Other languages', currentLang)
+ browser_lang = _ ('About <a href="%s">automatic language selection</a>.', currentLang)
+ browser_language_url = "http://www.lilypond.org/website/misc/browser-language"
+ browser_language = browser_lang % browser_language_url
+ return footer % locals ()
+
+def getLocalHref(line):
+ match = re.search(r'href=[\'"]?([^\'" >]+)', line)
+ if match:
+ url = match.group(0)[6:]
+ if (url[0:7] == "http://"):
+ url = ''
+ # strip any '#'
+ omit = url.find('#')
+ if (omit >= 0):
+ url = url[0:omit]
+ else:
+ url = ''
+ return url
+
+
+
+
+### main loop
for file in html_files:
- file_split = file.split('.')
- # we want to strip the .html
- out_filename = os.path.basename(file_split[0])
- if (len(file_split) == 2):
- # it's English
- lang = ''
- else:
- # it's a translation
- lang = file_split[1]
- out_filename += '.'+lang
-
-# I can't get the previous name to work
- out_filename = os.path.basename(file)
-
- # translation links should point to translations
- lines = open(file).readlines()
- # ick
- os.remove(file)
-
- # ick
- lang_footer = makeFooter(lang, out_filename.split('.')[0])
-
- outfile = open( os.path.join(outdir, out_filename), 'w')
- for line in lines:
- # avoid external links
- if ((line.find("href") >= 0) and (line.find("http")==-1)):
-# eventually we want to do this, but I can't get it to work.
-# waiting for help with apache (?)
-# line = line.replace(".html", "."+lang)
- text = ""
- if (not (lang=="")):
- text += "." + lang
- text += ".html"
- line = line.replace(".html", text)
- if ((line.find("href") >= 0) and
- (line.find("http")==-1) and
- (line.find("pdf") >= 0)):
- text = ""
- if (not (lang=="")):
- text += "." + lang
- text += ".pdf"
- line = line.replace(".pdf", text)
-
-
- if (line.find("<!-- FOOTER -->") >= 0):
- outfile.write( lang_footer )
- outfile.write(line)
- outfile.close()
+ ### we want to strip the .html and get the lang
+ file_split = file.split('.')
+ file_base = os.path.basename( file_split[0] )
+ if (len(file_split) == 2):
+ # it's English
+ lang = ''
+ # possibly necessary for automatic language selection
+ file_symlink = file.replace(".html", ".en.html")
+ if not os.path.lexists (file_symlink):
+ os.symlink (file, file_symlink)
+ elif (len(file_split) == 3):
+ # it's a translation
+ lang = file_split[1]
+ if (lang == "en"):
+ # it's a symlink
+ continue
+ else:
+ # it's a mess
+ print "is a mess"
+ continue
+
+ ### we need to replace parts of the file
+ lines = open(file).readlines()
+ os.remove(file)
+ outfile = open(file, 'w')
+
+ lang_footer = makeFooter (file_base, lang)
+
+ ### alter file
+ for line in lines:
+ ### alter links as appropriate
+ link = getLocalHref(line)
+ if (link != ""):
+ # questionable
+ if (not link.startswith("../doc/")):
+ if (link.endswith(".html")):
+ langlink = addLangExt(link[:-5], lang, "html")
+ line = line.replace(link, langlink)
+ if (link.endswith(".pdf")):
+ langlink = addLangExt(link[:-4], lang, "pdf")
+ line = line.replace(link, langlink)
+ ### add google tracker header
+ if (line.find("</head>") >= 0):
+ outfile.write("""<!-- Google tracking !-->
+<script src="http://www.google-analytics.com/urchin.js"
+type="text/javascript">
+</script>
+<script type="text/javascript">
+_uacct = "UA-68969-1";
+urchinTracker();
+</script>
+""");
+ #### add google tracker goals
+ if (line.find("href=\"http://download.linuxaudio.org") >= 0):
+ # TODO: more ugly hardcoding to make releases hard. :(
+ if (line.find('2.14') >= 0):
+ line = line.replace('a href=', 'a onClick=\"javascript:urchinTracker(\'/download/v2.14\');\" href=')
+ elif (line.find('2.15') >= 0):
+ line = line.replace('a href=', 'a onClick=\"javascript:urchinTracker(\'/download/v2.15\');\" href=')
+ ### add language selection footer
+ if (line.find("<div id=\"verifier_texinfo\">") >= 0):
+ outfile.write("<div id=\"footer\">\n")
+ outfile.write( lang_footer )
+ if (line.find("</body") >= 0):
+ outfile.write("</div>\n")
+ outfile.write(line)
+ outfile.close()