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