]> git.donarmstrong.com Git - lilypond.git/blob - buildscripts/html-gettext.py
Merge branch 'master' into dev/texi2html
[lilypond.git] / buildscripts / html-gettext.py
1 #!@PYTHON@
2 # html-gettext.py
3
4 # USAGE:  html-gettext.py [-o OUTDIR] 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
15 import langdefs
16
17 optlist, args = getopt.getopt(sys.argv[1:],'o:')
18 lang = args[0]
19 files = args [1:]
20
21 outdir = '.'
22 for x in optlist:
23     if x[0] == '-o':
24         outdir = x[1]
25
26 double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
27 my_gettext = langdefs.translation[lang]
28
29 html_codes = ((' -- ', ' – '),
30               (' --- ', ' — '))
31 html2texi = {'command': (re.compile (r'<samp><span class="command">(.*?)</span></samp>'), r'@command{\1}'),
32              'code': (re.compile (r'<code>(.*?)</code>'), r'@code{\1}')
33              }
34 texi2html = {'command': (re.compile (r'@command{(.*?)}'), r'<samp><span class="command">\1</span></samp>'),
35              'code': (re.compile (r'@code{(.*?)}'), r'<code>\1</code>')
36              }
37 whitespaces = re.compile (r'\s+')
38
39
40 def _ (s):
41     if not s:
42         return ''
43     s = whitespaces.sub (' ', s)
44     for c in html_codes:
45         s = s.replace (c[1], c[0])
46     for u in html2texi.values():
47         s = u[0].sub (u[1], s)
48     s = my_gettext (s)
49     for u in texi2html.values():
50         s = u[0].sub (u[1], s)
51     for c in html_codes:
52         s = s.replace (c[0], c[1])
53     return s
54
55 def link_gettext (m):
56     return '<link rel="' + m.group(1) + '" ' + m.group(2) + ' title="' + _(m.group(3)) + '">'
57
58 def title_gettext (m):
59     return '<title>' + _(m.group(1)) + ' - ' + m.group(2) + '</title>'
60
61 def a_href_gettext (m):
62     s = ''
63     if m.group(0)[-1] == ':':
64         s = double_punct_char_separator + ':'
65     t = ''
66     if m.lastindex == 7:
67         t = m.group(7)
68     return '<a ' + (m.group(1) or '') + m.group(2) + (m.group(3) or '') + _(m.group(4)) + m.group(5) + _(m.group(6)) + t + '</a>' + s
69
70 def h_gettext (m):
71     if m.group (3):
72         s = _(m.group(3))
73     else:
74         s= ''
75     return '<h' + m.group(1) + m.group(2) + '>' + s +\
76            m.group(4) + _(m.group(5)) + '</h' + m.group(1) + '>'
77
78 def crossmanual_ref_gettext (m):
79     return '<a href="' + m.group(1) + '">' + _(m.group(2)) + '</a>'
80
81 for filename in files:
82     f = open (filename, 'r')
83     page = f.read ()
84     f.close()
85     page = re.sub (r'<link rel="(up|prev|next)" (.*?) title="([^"]*?)">', link_gettext, page)
86     page = re.sub (r'<title>([^<]*?) - ([^<]*?)</title>', title_gettext, page)
87     # ugh
88     page = re.sub (r'(?ms)<a ((?:rel="\w+")? ?(?:accesskey="[^"]+?")? ?(?:name=".*?")? ?)(href=".+?">)(<code>)?(Appendix )?([A-Z\d.]+ |)(.+?)(?(3)</code>)</a>:?', a_href_gettext, page)
89     page = re.sub (r'<h(\d)( class="\w+"|)>\s*(Appendix |)([A-Z\d.]+ |)?([^<]+)\s*</h\1>', h_gettext, page)
90     page = re.sub (r'<a href="(\.\./(?:music-glossary|lilypond-program/)?(?:.+?))">(.+?)</a>', crossmanual_ref_gettext, page)
91     # this is necessary for entries not translated by a_href_gettext
92     page = re.sub (r'<a href="(.+?)">(.+?)</a>', crossmanual_ref_gettext, page)
93     for w in ('Next:', 'Previous:', 'Up:'):
94         page = re.sub (w, _(w), page)
95     page = langdefs.LANGDICT[lang].html_filter (page)
96     f = open (os.path.join (outdir, filename), 'w')
97     f.write (page)
98     f.close ()