]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/website_post.py
Merge branch 'master' of git://git.sv.gnu.org/lilypond 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
11 #### Translation data
12 lang_lookup = {
13   'fr': 'français',
14   'es': 'español',
15   '': 'english'
16 }
17
18 lang_other_langs = {
19   'es': 'Otros idiomas: ',
20   'fr': 'Autres langues : ',
21   '': 'Other languages: '
22 }
23
24
25 #### Actual program
26
27 #indir, outdir = sys.argv[1:]
28
29 # FIXME: looks dangerous!
30 indir = sys.argv[1]
31 outdir=indir
32
33 os.chdir(indir)
34
35 html_files = glob.glob( '*.html' )
36
37 # messy way to get all languages
38 langs_set = set()
39 for file in html_files:
40         file_split = file.split('.')
41         if (len(file_split) == 2):
42                 # it's English
43                 lang = ''
44         else:
45                 # it's a translation
46                 lang = file_split[1]
47         # make sure it's a real language
48         if (not (lang == "en")):
49                 langs_set.add(lang)
50 langs = list(langs_set)
51 langs.sort()
52
53 def makeFooter(currentLang, currentPage):
54         text = "<p id=\"languages\">\n"
55         text += lang_other_langs[currentLang]
56         for i in range(len(langs)):
57                 l = langs[i]
58                 if (l == currentLang):
59                         continue
60                 text += "<a href=\""
61                 text += currentPage
62                 if (not (l=="")):
63                         text += "." + l
64                 text += ".html\">"
65                 text += lang_lookup[l]
66                 text += "</a>"
67                 if (i < len(langs)-2):
68                         text += ", "
69                 else:
70                         text += ".\n"
71         # TODO: add link to automatic language selection?
72         # still need to include this page in the new webpages somewhere
73         text += "</p>\n"
74         return text
75
76
77 for file in html_files:
78         file_split = file.split('.')
79         # we want to strip the .html
80         out_filename = os.path.basename(file_split[0])
81         if (len(file_split) == 2):
82                 # it's English
83                 lang = ''
84                 # possibly necessary for automatic language selection
85                 file_symlink =file.replace(".html", ".en.html")
86                 if (not (os.path.exists(file_symlink))):
87                         os.symlink (file, file_symlink)
88         else:
89                 # it's a translation
90                 lang = file_split[1]
91         # it's a symlink
92         if (lang == "en"):
93                 continue
94         out_filename += '.'+lang
95
96 # I can't get the previous name to work
97         out_filename = os.path.basename(file)
98
99         # translation links should point to translations
100         lines = open(file).readlines()
101         # ick
102         os.remove(file)
103
104         # ick
105         lang_footer = makeFooter(lang, out_filename.split('.')[0])
106         
107         outfile = open( out_filename, 'w')
108         for line in lines:
109                 # avoid external links
110                 if ((line.find("href") >= 0) and (line.find("http")==-1)):
111 # eventually we want to do this, but I can't get it to work.
112 # waiting for help with apache (?)
113 #                       line = line.replace(".html", "."+lang)
114                         text = ""
115                         if (not (lang=="")):
116                                 text += "." + lang
117                         text += ".html"
118                         line = line.replace(".html", text)
119                 if ((line.find("href") >= 0) and
120                     (line.find("http")==-1) and
121                     (line.find("pdf") >= 0)):
122                         text = ""
123                         if (not (lang=="")):
124                                 text += "." + lang
125                         text += ".pdf"
126                         line = line.replace(".pdf", text)
127
128
129                 if (line.find("<!-- FOOTER -->") >= 0):
130                         outfile.write( lang_footer )
131                 outfile.write(line)
132         outfile.close()
133