]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/website_post.py
Merge branch 'master' into lilypond/translation
[lilypond.git] / scripts / build / website_post.py
1 #!@PYTHON@
2 #-*- coding: utf-8 -*-
3
4 ##### This is web_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     'de': {
15         'English': 'Deutsch',
16         'Other languages': 'Andere Sprachen',
17         },
18     'es': {
19         'English': 'Español',
20         'Other languages': 'Otros idiomas',
21         },
22     'fr': {
23         'English': 'Français',
24         'Other languages': 'Autres langues',
25         },
26     'hu': {
27         'English': 'Magyar',
28         'Other languages': 'Más nyelvek',
29         },
30     'ja': {
31         'English': 'Japanese',
32         'Other languages': '他の言語',
33         },
34     'nl': {
35         'English': 'Nederlands',
36         'Other languages': 'Andere talen',
37         },
38     }
39
40 # needs at least: make -C po or make- C Documentation/po
41 HAVE_GETTEXT = False
42
43 ####  this breaks on lilypond.org
44 # Keep some freakin' gettext compatibility
45 #if HAVE_GETTEXT:
46 #    import lilylib as ly;
47 #    global _;_=ly._
48 #else: # poor mans translation
49 #    def _ (string, lang=os.environ['LANG']):
50 #        return translations.get (lang.split ('_')[0], {}).get (string, string)
51
52 #### this works on lilypond.org
53 def _ (string, lang):
54     return translations.get (lang.split ('_')[0], {}).get (string, string)
55
56
57 exclude_manuals = [
58     '/music-glossary',
59     '/snippets',
60     '/internals',
61     '/contributor'
62 ]
63
64 ###### Actual program
65
66 dir = sys.argv[1]
67
68 os.chdir(dir)
69 html_files = glob.glob( '*.html' )
70
71
72 ### messy way to get all languages
73 langs_set = set()
74 for file in html_files:
75     file_split = file.split('.')
76     if (len(file_split) == 2):
77         # it's English
78         lang = ''
79     elif (len(file_split) == 3):
80         # it's a translation
81         lang = file_split[1]
82     # make sure it's a translated language
83     if lang != "en":
84         langs_set.add(lang)
85 langs = list(langs_set)
86 langs.sort()
87
88
89 ### helper functions
90 def addLangExt(filename, lang, ext):
91     text = filename
92     exclude = 0
93     for dir in exclude_manuals:
94         if (text.find(dir) >= 0):
95             exclude = 1
96     if (not (exclude or (lang==""))):
97         text += "." + lang
98     text += "." + ext
99     return text
100
101 def makeFooter (filename, currentLang):
102     # TODO: add link to automatic language selection?
103     # still need to include this page in the new webpages somewhere
104     footer = '''<p id="languages">
105 %(other)s: %(lst)s.
106 </p>
107 '''
108     def link (lang):
109         str = '''<a href="%(file_name)s">%(language_name)s</a>'''
110         file_name = addLangExt (filename, lang, 'html')
111         language_name = _ ('English', lang)
112         return str % locals ()
113     lst = ', '.join ([link (lang) for lang in langs if lang != currentLang])
114     other = _ ('Other languages', currentLang)
115     return footer % locals ()
116
117 def getLocalHref(line):
118     match = re.search(r'href=[\'"]?([^\'" >]+)', line)
119     if match:
120         url = match.group(0)[6:]
121         if (url[0:7] == "http://"):
122             url = ''
123         # strip any '#'
124         omit = url.find('#')
125         if (omit >= 0):
126             url = url[0:omit]
127     else:
128         url = ''
129     return url
130
131
132
133
134 ### main loop
135 for file in html_files:
136     ### we want to strip the .html and get the lang
137     file_split = file.split('.')
138     file_base = os.path.basename( file_split[0] )
139     if (len(file_split) == 2):
140         # it's English
141         lang = ''
142         # possibly necessary for automatic language selection
143         file_symlink = file.replace(".html", ".en.html")
144         if (not (os.path.exists(file_symlink))):
145             os.symlink (file, file_symlink)
146     elif (len(file_split) == 3):
147         # it's a translation
148         lang = file_split[1]
149         if (lang == "en"):
150             # it's a symlink
151             continue
152     else:
153         # it's a mess
154         print "is a mess"
155         continue
156
157     ### we need to replace parts of the file
158     lines = open(file).readlines()
159     os.remove(file)
160     outfile = open(file, 'w')
161
162     lang_footer = makeFooter (file_base, lang)
163
164     ### alter file
165     for line in lines:
166         ### alter links as appropriate
167         link = getLocalHref(line)
168         if (link != ""):
169             # quesitonable
170             if (link.endswith(".html")):
171                 langlink = addLangExt(link[:-5], lang, "html")
172                 line = line.replace(link, langlink)
173             if (link.endswith(".pdf")):
174                 langlink = addLangExt(link[:-4], lang, "pdf")
175                 line = line.replace(link, langlink)
176         ### add language selection footer
177         if (line.find("<!-- FOOTER -->") >= 0):
178             outfile.write( lang_footer )
179         outfile.write(line)
180     outfile.close()
181