]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/html-gettext.py
Merge with master
[lilypond.git] / buildscripts / html-gettext.py
1 #!@PYTHON@
2 # html-gettext.py
3
4 # USAGE:  html-gettext.py [-o OUTDIR] BUILDSCRIPT-DIR LOCALEDIR LANG FILES
5 #
6 # -o OUTDIR specifies that output files should be written in OUTDIR
7 #    rather than be overwritten
8 #
9
10 import sys
11 import re
12 import os
13 import getopt
14 import gettext
15
16 optlist, args = getopt.getopt(sys.argv[1:],'o:')
17 buildscript_dir, localedir, lang = args[0:3]
18
19 outdir = '.'
20 for x in optlist:
21         if x[0] == '-o':
22                 outdir = x[1]
23
24 sys.path.append (buildscript_dir)
25 import langdefs
26
27 double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
28 print localedir
29 print lang
30 t = gettext.translation('lilypond-doc', localedir, [lang])
31 my_gettext = t.gettext
32
33 html_codes = ((' -- ', ' – '),
34               (' --- ', ' — '))
35
36 def _ (s):
37         for c in html_codes:
38                 s = s.replace (c[1], c[0])
39         s = my_gettext (s)
40         for c in html_codes:
41                 s = s.replace (c[0], c[1])
42         return s
43
44 def link_gettext (m):
45         return '<link rel="' + m.group(1) + '" ' + m.group(2) + ' title="' + _(m.group(3)) + '">'
46
47 def title_gettext (m):
48         return '<title>' + _(m.group(1)) + ' - ' + m.group(2) + '</title>'
49
50 def a_href_gettext (m):
51         if m.group(6) == ':':
52                 s = double_punct_char_separator + ':'
53         elif m.group(6) == None:
54                 s = ''
55         return '<a ' + (m.group(1) or '') + m.group(2) + m.group(3) + _(m.group(4)) + m.group(5) + '</a>' + s
56
57 def h_gettext (m):
58         return '<h' + m.group(1) + m.group(2) + '>' + \
59                m.group(3) + _(m.group(4)) + '</h' + m.group(1) + '>'
60
61 for filename in args[3:]:
62         f = open (filename, 'r')
63         page = f.read ()
64         f.close()
65         page = re.sub (r'<link rel="(up|prev|next)" (.*?) title="([^"]*?)">', link_gettext, page)
66         page = re.sub (r'<title>([^<]*?) - ([^<]*?)</title>', title_gettext, page)
67         page = re.sub (r'<a ((?:rel="\w+")? ?(?:accesskey="[^"]+?" ?)?)(href="[^"]+?">)((?:<code>|)(?:[\d.]+ |))([^<]+)(</code>|)</a>(:)?', a_href_gettext, page)
68         page = re.sub (r'<h(\d)( class="\w+"|)>([\d.]+ |)?([^<]+)</h\1>', h_gettext, page)
69         for w in ('Next:', 'Previous:', 'Up:'):
70                 page = re.sub (w, _(w), page)
71         f = open (os.path.join (outdir, filename), 'w')
72         f.write (page)
73         f.close ()