]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/website_post.py
Merge branch 'lilypond/translation' of ssh://git.sv.gnu.org/srv/git/lilypond
[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
13 lang_lookup = {
14     'fr': 'français',
15     'es': 'español',
16     '': 'english'
17 }
18
19 lang_other_langs = {
20     'es': 'Otros idiomas: ',
21     'fr': 'Autres langues : ',
22     '': 'Other languages: '
23 }
24
25 exclude_manuals = [
26     '/music-glossary',
27     '/snippets',
28     '/internals',
29     '/contributor'
30 ]
31
32 ###### Actual program
33
34 dir = sys.argv[1]
35
36 os.chdir(dir)
37 html_files = glob.glob( '*.html' )
38
39
40 ### messy way to get all languages
41 langs_set = set()
42 for file in html_files:
43     file_split = file.split('.')
44     if (len(file_split) == 2):
45         # it's English
46         lang = ''
47     elif (len(file_split) == 3):
48         # it's a translation
49         lang = file_split[1]
50     # make sure it's a translated language
51     if (not (lang == "en")):
52         langs_set.add(lang)
53 langs = list(langs_set)
54 langs.sort()
55
56
57 ### helper functions
58 def addLangExt(filename, lang, ext):
59     text = filename
60     exclude = 0
61     for dir in exclude_manuals:
62         if (text.find(dir) >= 0):
63             exclude = 1
64     if (not (exclude or (lang==""))):
65         text += "." + lang
66     text += "." + ext
67     return text
68
69 def makeFooter(filename, currentLang):
70     text = "<p id=\"languages\">\n"
71     text += lang_other_langs[currentLang]
72     for i in range(len(langs)):
73         lang = langs[i]
74         if (lang == currentLang):
75             continue
76         text += "<a href=\""
77         text += addLangExt(filename, lang, "html")
78         text += "\">"
79         text += lang_lookup[lang]
80         text += "</a>"
81         if (i < len(langs)-2):
82             text += ", "
83         else:
84             text += ".\n"
85     # TODO: add link to automatic language selection?
86     # still need to include this page in the new webpages somewhere
87     text += "</p>\n"
88     return text
89
90 def getLocalHref(line):
91     match = re.search(r'href=[\'"]?([^\'" >]+)', line)
92     if match:
93         url = match.group(0)[6:]
94         if (url[0:7] == "http://"):
95             url = ''
96         # strip any '#'
97         omit = url.find('#')
98         if (omit >= 0):
99             url = url[0:omit]
100     else:
101         url = ''
102     return url
103
104
105
106
107 ### main loop
108 for file in html_files:
109     ### we want to strip the .html and get the lang
110     file_split = file.split('.')
111     file_base = os.path.basename( file_split[0] )
112     if (len(file_split) == 2):
113         # it's English
114         lang = ''
115         # possibly necessary for automatic language selection
116         file_symlink = file.replace(".html", ".en.html")
117         if (not (os.path.exists(file_symlink))):
118             os.symlink (file, file_symlink)
119     elif (len(file_split) == 3):
120         # it's a translation
121         lang = file_split[1]
122         if (lang == "en"):
123             # it's a symlink
124             continue
125     else:
126         # it's a mess
127         print "is a mess"
128         continue
129
130     ### we need to replace parts of the file
131     lines = open(file).readlines()
132     os.remove(file)
133     outfile = open(file, 'w')
134
135     lang_footer = makeFooter(file_base, lang)
136
137
138     ### alter file
139     for line in lines:
140         ### alter links as appropriate
141         link = getLocalHref(line)
142         if (link != ""):
143             # quesitonable
144             if (link.endswith(".html")):
145                 langlink = addLangExt(link[:-5], lang, "html")
146                 line = line.replace(link, langlink)
147             if (link.endswith(".pdf")):
148                 langlink = addLangExt(link[:-4], lang, "pdf")
149                 line = line.replace(link, langlink)
150         ### add language selection footer
151         if (line.find("<!-- FOOTER -->") >= 0):
152             outfile.write( lang_footer )
153         outfile.write(line)
154     outfile.close()
155