4 # USAGE: html-gettext.py [-o OUTDIR] BUILDSCRIPT-DIR LOCALEDIR LANG FILES
6 # -o OUTDIR specifies that output files should be written in OUTDIR
7 # rather than be overwritten
16 optlist, args = getopt.getopt(sys.argv[1:],'o:')
17 buildscript_dir, localedir, lang = args[0:3]
24 sys.path.append (buildscript_dir)
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
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}')
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>')
39 whitespaces = re.compile (r'\s+')
45 s = whitespaces.sub (' ', s)
47 s = s.replace (c[1], c[0])
48 for u in html2texi.values():
49 s = u[0].sub (u[1], s)
51 for u in texi2html.values():
52 s = u[0].sub (u[1], s)
54 s = s.replace (c[0], c[1])
58 return '<link rel="' + m.group(1) + '" ' + m.group(2) + ' title="' + _(m.group(3)) + '">'
60 def title_gettext (m):
61 return '<title>' + _(m.group(1)) + ' - ' + m.group(2) + '</title>'
63 def a_href_gettext (m):
65 if m.group(0)[-1] == ':':
66 s = double_punct_char_separator + ':'
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
77 return '<h' + m.group(1) + m.group(2) + '>' + s +\
78 m.group(4) + _(m.group(5)) + '</h' + m.group(1) + '>'
80 def crossmanual_ref_gettext (m):
81 return '<a href="' + m.group(1) + '">' + _(m.group(2)) + '</a>'
83 for filename in args[3:]:
84 f = open (filename, 'r')
87 page = re.sub (r'<link rel="(up|prev|next)" (.*?) title="([^"]*?)">', link_gettext, page)
88 page = re.sub (r'<title>([^<]*?) - ([^<]*?)</title>', title_gettext, page)
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+"|)>(Appendix |)([A-Z\d.]+ |)?([^<]+)</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')