]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/website_post.py
fbb462a51dab90a205963ba780d70251d0363f24
[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 # Keep some freakin' gettext compatibility
44 if HAVE_GETTEXT:
45     import lilylib as ly;
46     global _;_=ly._
47 else: # poor mans translation
48     def _ (string, lang=os.environ['LANG']):
49         return translations.get (lang.split ('_')[0], {}).get (string, string)
50
51
52 exclude_manuals = [
53     '/music-glossary',
54     '/snippets',
55     '/internals',
56     '/contributor'
57 ]
58
59 ###### Actual program
60
61 dir = sys.argv[1]
62
63 os.chdir(dir)
64 html_files = glob.glob( '*.html' )
65
66
67 ### messy way to get all languages
68 langs_set = set()
69 for file in html_files:
70     file_split = file.split('.')
71     if (len(file_split) == 2):
72         # it's English
73         lang = ''
74     elif (len(file_split) == 3):
75         # it's a translation
76         lang = file_split[1]
77     # make sure it's a translated language
78     if lang != "en":
79         langs_set.add(lang)
80 langs = list(langs_set)
81 langs.sort()
82
83
84 ### helper functions
85 def addLangExt(filename, lang, ext):
86     text = filename
87     exclude = 0
88     for dir in exclude_manuals:
89         if (text.find(dir) >= 0):
90             exclude = 1
91     if (not (exclude or (lang==""))):
92         text += "." + lang
93     text += "." + ext
94     return text
95
96 def makeFooter (filename, currentLang):
97     # TODO: add link to automatic language selection?
98     # still need to include this page in the new webpages somewhere
99     footer = '''<p id="languages">
100 %(other)s: %(lst)s.
101 </p>
102 '''
103     def link (lang):
104         str = '''<a href="%(file_name)s">%(language_name)s</a>'''
105         file_name = addLangExt (filename, lang, 'html')
106         language_name = _ ('English', lang)
107         return str % locals ()
108     lst = ', '.join ([link (lang) for lang in langs if lang != currentLang])
109     other = _ ('Other languages', currentLang)
110     return footer % locals ()
111
112 def getLocalHref(line):
113     match = re.search(r'href=[\'"]?([^\'" >]+)', line)
114     if match:
115         url = match.group(0)[6:]
116         if (url[0:7] == "http://"):
117             url = ''
118         # strip any '#'
119         omit = url.find('#')
120         if (omit >= 0):
121             url = url[0:omit]
122     else:
123         url = ''
124     return url
125
126
127
128
129 ### main loop
130 for file in html_files:
131     ### we want to strip the .html and get the lang
132     file_split = file.split('.')
133     file_base = os.path.basename( file_split[0] )
134     if (len(file_split) == 2):
135         # it's English
136         lang = ''
137         # possibly necessary for automatic language selection
138         file_symlink = file.replace(".html", ".en.html")
139         if (not (os.path.exists(file_symlink))):
140             os.symlink (file, file_symlink)
141     elif (len(file_split) == 3):
142         # it's a translation
143         lang = file_split[1]
144         if (lang == "en"):
145             # it's a symlink
146             continue
147     else:
148         # it's a mess
149         print "is a mess"
150         continue
151
152     ### we need to replace parts of the file
153     lines = open(file).readlines()
154     os.remove(file)
155     outfile = open(file, 'w')
156
157     lang_footer = makeFooter (file_base, lang)
158
159     ### alter file
160     for line in lines:
161         ### alter links as appropriate
162         link = getLocalHref(line)
163         if (link != ""):
164             # quesitonable
165             if (link.endswith(".html")):
166                 langlink = addLangExt(link[:-5], lang, "html")
167                 line = line.replace(link, langlink)
168             if (link.endswith(".pdf")):
169                 langlink = addLangExt(link[:-4], lang, "pdf")
170                 line = line.replace(link, langlink)
171         ### add language selection footer
172         if (line.find("<!-- FOOTER -->") >= 0):
173             outfile.write( lang_footer )
174         outfile.write(line)
175     outfile.close()
176