]> 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     'it': {
31         'English': 'Italiano',
32         'Other languages': 'Altre lingue',
33         },
34     'ja': {
35         'English': 'Japanese',
36         'Other languages': '他の言語',
37         },
38     'nl': {
39         'English': 'Nederlands',
40         'Other languages': 'Andere talen',
41         },
42     }
43
44 # needs at least: make -C po or make- C Documentation/po
45 HAVE_GETTEXT = False
46
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)
55
56 #### this works on lilypond.org
57 def _ (string, lang):
58     return translations.get (lang.split ('_')[0], {}).get (string, string)
59
60
61 exclude_manuals = [
62     '/music-glossary',
63     '/snippets',
64     '/internals',
65     '/contributor'
66 ]
67
68 ###### Actual program
69
70 dir = sys.argv[1]
71
72 os.chdir(dir)
73 html_files = glob.glob( '*.html' )
74
75
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()
91
92
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
104
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>
111 '''
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 ()
120
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
134
135
136
137
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
160
161     ### we need to replace parts of the file
162     lines = open(file).readlines()
163     os.remove(file)
164     outfile = open(file, 'w')
165
166     lang_footer = makeFooter (file_base, lang)
167
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 language selection footer
181         if (line.find("<!-- FOOTER -->") >= 0):
182             outfile.write( lang_footer )
183         outfile.write(line)
184     outfile.close()
185