]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/website_post.py
368e88842e16afb61f6ce0af276e67d52ca2cb14
[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     text = "<p id=\"languages\">\n"
98     text += _ ('Other languages: ', currentLang)
99     for lang in langs:
100         if (lang == currentLang):
101             continue
102         text += "<a href=\""
103         text += addLangExt(filename, lang, "html")
104         text += "\">"
105         text += _ ('English', lang)
106         text += "</a>, "
107     text = text[:-2] + '.\n'
108     # TODO: add link to automatic language selection?
109     # still need to include this page in the new webpages somewhere
110     text += "</p>\n"
111     return text
112
113 def getLocalHref(line):
114     match = re.search(r'href=[\'"]?([^\'" >]+)', line)
115     if match:
116         url = match.group(0)[6:]
117         if (url[0:7] == "http://"):
118             url = ''
119         # strip any '#'
120         omit = url.find('#')
121         if (omit >= 0):
122             url = url[0:omit]
123     else:
124         url = ''
125     return url
126
127
128
129
130 ### main loop
131 for file in html_files:
132     ### we want to strip the .html and get the lang
133     file_split = file.split('.')
134     file_base = os.path.basename( file_split[0] )
135     if (len(file_split) == 2):
136         # it's English
137         lang = ''
138         # possibly necessary for automatic language selection
139         file_symlink = file.replace(".html", ".en.html")
140         if (not (os.path.exists(file_symlink))):
141             os.symlink (file, file_symlink)
142     elif (len(file_split) == 3):
143         # it's a translation
144         lang = file_split[1]
145         if (lang == "en"):
146             # it's a symlink
147             continue
148     else:
149         # it's a mess
150         print "is a mess"
151         continue
152
153     ### we need to replace parts of the file
154     lines = open(file).readlines()
155     os.remove(file)
156     outfile = open(file, 'w')
157
158     lang_footer = makeFooter(file_base, lang)
159
160     ### alter file
161     for line in lines:
162         ### alter links as appropriate
163         link = getLocalHref(line)
164         if (link != ""):
165             # quesitonable
166             if (link.endswith(".html")):
167                 langlink = addLangExt(link[:-5], lang, "html")
168                 line = line.replace(link, langlink)
169             if (link.endswith(".pdf")):
170                 langlink = addLangExt(link[:-4], lang, "pdf")
171                 line = line.replace(link, langlink)
172         ### add language selection footer
173         if (line.find("<!-- FOOTER -->") >= 0):
174             outfile.write( lang_footer )
175         outfile.write(line)
176     outfile.close()
177