Add scripts/auxiliar/update-patch-version
[lilypond/mpolesky.git] / scripts / build / website_post.py
blob5267a8582807ae4f5df29d494d0511922382df6b
1 #!@PYTHON@
2 #-*- coding: utf-8 -*-
4 ##### This is web_post.py. This script deals with translations
5 ##### in the "make website" target.
7 import sys
8 import os
9 import glob
10 import re
12 ###### Translation data, move out, see create-weblinks-itexi.py
13 translations = {
14 'de': {
15 'English': 'Deutsch',
16 'Other languages': 'Andere Sprachen',
18 'es': {
19 'English': 'Español',
20 'Other languages': 'Otros idiomas',
22 'fr': {
23 'English': 'Français',
24 'Other languages': 'Autres langues',
26 'hu': {
27 'English': 'Magyar',
28 'Other languages': 'Más nyelvek',
30 'it': {
31 'English': 'Italiano',
32 'Other languages': 'Altre lingue',
34 'ja': {
35 'English': 'Japanese',
36 'Other languages': '他の言語',
38 'nl': {
39 'English': 'Nederlands',
40 'Other languages': 'Andere talen',
44 # needs at least: make -C po or make- C Documentation/po
45 HAVE_GETTEXT = False
47 #### this breaks on lilypond.org
48 # Keep some freakin' gettext compatibility
49 #if HAVE_GETTEXT:
50 # import lilylib as ly;
51 # global _;_=ly._
52 #else: # poor mans translation
53 # def _ (string, lang=os.environ['LANG']):
54 # return translations.get (lang.split ('_')[0], {}).get (string, string)
56 #### this works on lilypond.org
57 def _ (string, lang):
58 return translations.get (lang.split ('_')[0], {}).get (string, string)
61 exclude_manuals = [
62 '/music-glossary',
63 '/snippets',
64 '/internals',
65 '/contributor'
68 ###### Actual program
70 dir = sys.argv[1]
72 os.chdir(dir)
73 html_files = glob.glob( '*.html' )
76 ### messy way to get all languages
77 langs_set = set()
78 for file in html_files:
79 file_split = file.split('.')
80 if (len(file_split) == 2):
81 # it's English
82 lang = ''
83 elif (len(file_split) == 3):
84 # it's a translation
85 lang = file_split[1]
86 # make sure it's a translated language
87 if lang != "en":
88 langs_set.add(lang)
89 langs = list(langs_set)
90 langs.sort()
93 ### helper functions
94 def addLangExt(filename, lang, ext):
95 text = filename
96 exclude = 0
97 for dir in exclude_manuals:
98 if (text.find(dir) >= 0):
99 exclude = 1
100 if (not (exclude or (lang==""))):
101 text += "." + lang
102 text += "." + ext
103 return text
105 def makeFooter (filename, currentLang):
106 # TODO: add link to automatic language selection?
107 # still need to include this page in the new webpages somewhere
108 footer = '''<p id="languages">
109 %(other)s: %(lst)s.
110 </p>
112 def link (lang):
113 str = '''<a href="%(file_name)s">%(language_name)s</a>'''
114 file_name = addLangExt (filename, lang, 'html')
115 language_name = _ ('English', lang)
116 return str % locals ()
117 lst = ', '.join ([link (lang) for lang in langs if lang != currentLang])
118 other = _ ('Other languages', currentLang)
119 return footer % locals ()
121 def getLocalHref(line):
122 match = re.search(r'href=[\'"]?([^\'" >]+)', line)
123 if match:
124 url = match.group(0)[6:]
125 if (url[0:7] == "http://"):
126 url = ''
127 # strip any '#'
128 omit = url.find('#')
129 if (omit >= 0):
130 url = url[0:omit]
131 else:
132 url = ''
133 return url
138 ### main loop
139 for file in html_files:
140 ### we want to strip the .html and get the lang
141 file_split = file.split('.')
142 file_base = os.path.basename( file_split[0] )
143 if (len(file_split) == 2):
144 # it's English
145 lang = ''
146 # possibly necessary for automatic language selection
147 file_symlink = file.replace(".html", ".en.html")
148 if (not (os.path.exists(file_symlink))):
149 os.symlink (file, file_symlink)
150 elif (len(file_split) == 3):
151 # it's a translation
152 lang = file_split[1]
153 if (lang == "en"):
154 # it's a symlink
155 continue
156 else:
157 # it's a mess
158 print "is a mess"
159 continue
161 ### we need to replace parts of the file
162 lines = open(file).readlines()
163 os.remove(file)
164 outfile = open(file, 'w')
166 lang_footer = makeFooter (file_base, lang)
168 ### alter file
169 for line in lines:
170 ### alter links as appropriate
171 link = getLocalHref(line)
172 if (link != ""):
173 # quesitonable
174 if (link.endswith(".html")):
175 langlink = addLangExt(link[:-5], lang, "html")
176 line = line.replace(link, langlink)
177 if (link.endswith(".pdf")):
178 langlink = addLangExt(link[:-4], lang, "pdf")
179 line = line.replace(link, langlink)
180 ### add google tracker header
181 if (line.find("</head>") >= 0):
182 outfile.write("""<!-- Google tracking !-->
183 <script src="http://www.google-analytics.com/urchin.js"
184 type="text/javascript">
185 </script>
186 <script type="text/javascript">
187 _uacct = "UA-68969-1";
188 urchinTracker();
189 </script>
190 """);
191 #### add google tracker goals
192 if (line.find("href=\"http://download.linuxaudio.org") >= 0):
193 # TODO: more ugly hardcoding to make releases hard. :(
194 if (line.find('2.12') >= 0):
195 line = line.replace('a href=', 'a onClick=\"javascript:urchinTracker(\'/download/v2.12\');\" href=')
196 elif (line.find('2.13') >= 0):
197 line = line.replace('a href=', 'a onClick=\"javascript:urchinTracker(\'/download/v2.13\');\" href=')
198 ### add language selection footer
199 if (line.find("<div id=\"verifier_texinfo\">") >= 0):
200 outfile.write("<div id=\"footer\">\n")
201 outfile.write( lang_footer )
202 if (line.find("</body") >= 0):
203 outfile.write("</div>\n")
204 outfile.write(line)
205 outfile.close()