]> git.donarmstrong.com Git - lilypond.git/blob - scripts/build/website_post.py
Web build: add .en.html symlinks.
[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         langs_set.add(lang)
48 langs = list(langs_set)
49 langs.sort()
50
51 def makeFooter(currentLang, currentPage):
52         text = "<p id=\"languages\">\n"
53         text += lang_other_langs[currentLang]
54         for i in range(len(langs)):
55                 l = langs[i]
56                 if (l == currentLang):
57                         continue
58                 text += "<a href=\""
59                 text += currentPage
60                 if (not (l=="")):
61                         text += "." + l
62                 text += ".html\">"
63                 text += lang_lookup[l]
64                 text += "</a>"
65                 if (i < len(langs)-2):
66                         text += ", "
67                 else:
68                         text += ".\n"
69         # TODO: add link to automatic language selection?
70         # still need to include this page in the new webpages somewhere
71         text += "</p>\n"
72         return text
73
74
75 for file in html_files:
76         file_split = file.split('.')
77         # we want to strip the .html
78         out_filename = os.path.basename(file_split[0])
79         if (len(file_split) == 2):
80                 # it's English
81                 lang = ''
82                 # possibly necessary for automatic language selection
83                 file_symlink =file.replace(".html", ".en.html")
84                 os.symlink (file, file_symlink)
85         else:
86                 # it's a translation
87                 lang = file_split[1]
88         out_filename += '.'+lang
89
90 # I can't get the previous name to work
91         out_filename = os.path.basename(file)
92
93         # translation links should point to translations
94         lines = open(file).readlines()
95         # ick
96         os.remove(file)
97
98         # ick
99         lang_footer = makeFooter(lang, out_filename.split('.')[0])
100         
101         outfile = open( out_filename, 'w')
102         for line in lines:
103                 # avoid external links
104                 if ((line.find("href") >= 0) and (line.find("http")==-1)):
105 # eventually we want to do this, but I can't get it to work.
106 # waiting for help with apache (?)
107 #                       line = line.replace(".html", "."+lang)
108                         text = ""
109                         if (not (lang=="")):
110                                 text += "." + lang
111                         text += ".html"
112                         line = line.replace(".html", text)
113                 if ((line.find("href") >= 0) and
114                     (line.find("http")==-1) and
115                     (line.find("pdf") >= 0)):
116                         text = ""
117                         if (not (lang=="")):
118                                 text += "." + lang
119                         text += ".pdf"
120                         line = line.replace(".pdf", text)
121
122
123                 if (line.find("<!-- FOOTER -->") >= 0):
124                         outfile.write( lang_footer )
125                 outfile.write(line)
126         outfile.close()
127