]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/html-gettext.py
Merge branch 'master' into topic/master-translation
[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 html2texi_command = re.compile (r'<samp><span class="command">(.*?)</span></samp>')
36 texi2html_command = re.compile (r'@command{(.*?)}')
37
38 def _ (s):
39         for c in html_codes:
40                 s = s.replace (c[1], c[0])
41         s = html2texi_command.sub (r'@command{\1}', s)
42         s = my_gettext (s)
43         s = texi2html_command.sub (r'<samp><span class="command">\1</span></samp>', s)
44         for c in html_codes:
45                 s = s.replace (c[0], c[1])
46         return s
47
48 def link_gettext (m):
49         return '<link rel="' + m.group(1) + '" ' + m.group(2) + ' title="' + _(m.group(3)) + '">'
50
51 def title_gettext (m):
52         return '<title>' + _(m.group(1)) + ' - ' + m.group(2) + '</title>'
53
54 def a_href_gettext (m):
55         if m.group(6) == ':':
56                 s = double_punct_char_separator + ':'
57         elif m.group(6) == None:
58                 s = ''
59         return '<a ' + (m.group(1) or '') + m.group(2) + m.group(3) + _(m.group(4)) + m.group(5) + '</a>' + s
60
61 def h_gettext (m):
62         return '<h' + m.group(1) + m.group(2) + '>' + \
63                m.group(3) + _(m.group(4)) + '</h' + m.group(1) + '>'
64
65 def rglos_gettext (m):
66         return '<a href="../music-glossary/' + m.group(1) + '">' + _(m.group(2)) + '</a>'
67
68 for filename in args[3:]:
69         f = open (filename, 'r')
70         page = f.read ()
71         f.close()
72         page = re.sub (r'<link rel="(up|prev|next)" (.*?) title="([^"]*?)">', link_gettext, page)
73         page = re.sub (r'<title>([^<]*?) - ([^<]*?)</title>', title_gettext, page)
74         page = re.sub (r'<a ((?:rel="\w+")? ?(?:accesskey="[^"]+?")? ?(?:name=".*?")? ?)(href="[^"]+?">)((?:<code>|)(?:[\d.]+ |))([^<]+)(</code>|)</a>(:)?', a_href_gettext, page)
75         page = re.sub (r'<h(\d)( class="\w+"|)>([\d.]+ |)?([^<]+)</h\1>', h_gettext, page)
76         page = re.sub (r'<a href="../music-glossary/(.+?)">(.+?)</a>', rglos_gettext, page)
77         for w in ('Next:', 'Previous:', 'Up:'):
78                 page = re.sub (w, _(w), page)
79         f = open (os.path.join (outdir, filename), 'w')
80         f.write (page)
81         f.close ()