]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/website_post.py
Web build: thinkos in previous commit.
[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_pages = [
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     if (not (lang=="")):
61         text += "." + lang
62     text += "." + ext
63     return text
64
65 def makeFooter(filename, currentLang):
66     text = "<p id=\"languages\">\n"
67     text += lang_other_langs[currentLang]
68     for i in range(len(langs)):
69         lang = langs[i]
70         if (lang == currentLang):
71             continue
72         text += "<a href=\""
73         text += addLangExt(filename, lang, "html")
74         text += "\">"
75         text += lang_lookup[lang]
76         text += "</a>"
77         if (i < len(langs)-2):
78             text += ", "
79         else:
80             text += ".\n"
81     # TODO: add link to automatic language selection?
82     # still need to include this page in the new webpages somewhere
83     text += "</p>\n"
84     return text
85
86 def getLocalHref(line):
87     match = re.search(r'href=[\'"]?([^\'" >]+)', line)
88     if match:
89         url = match.group(0)[6:]
90         if (url[0:7] == "http://"):
91             url = ''
92         # strip any '#'
93         omit = url.find('#')
94         if (omit >= 0):
95             url = url[0:omit]
96     else:
97         url = ''
98     return url
99
100
101
102
103 ### main loop
104 for file in html_files:
105     ### we want to strip the .html and get the lang
106     file_split = file.split('.')
107     file_base = os.path.basename( file_split[0] )
108     if (len(file_split) == 2):
109         # it's English
110         lang = ''
111         # possibly necessary for automatic language selection
112         file_symlink = file.replace(".html", ".en.html")
113         if (not (os.path.exists(file_symlink))):
114             os.symlink (file, file_symlink)
115     elif (len(file_split) == 3):
116         # it's a translation
117         lang = file_split[1]
118         if (lang == "en"):
119             # it's a symlink
120             continue
121     else:
122         # it's a mess
123         print "is a mess"
124         continue
125
126     ### we need to replace parts of the file
127     lines = open(file).readlines()
128     os.remove(file)
129     outfile = open(file, 'w')
130
131     lang_footer = makeFooter(file_base, lang)
132
133
134     ### alter file
135     for line in lines:
136         ### alter links as appropriate
137         link = getLocalHref(line)
138         if (link != ""):
139             link_base = link.split('.')[0]
140             if (link.endswith(".html")):
141                 langlink = addLangExt(link_base, lang, "html")
142                 line = line.replace(link, langlink)
143             if (link.endswith(".pdf")):
144                 langlink = addLangExt(link_base, lang, "pdf")
145                 line = line.replace(link, langlink)
146         ### add language selection footer
147         if (line.find("<!-- FOOTER -->") >= 0):
148             outfile.write( lang_footer )
149         outfile.write(line)
150     outfile.close()
151