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