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