4 # USAGE: html-gettext.py [-o OUTDIR] LANG FILES
6 # -o OUTDIR specifies that output files should be written in OUTDIR
7 # rather than be overwritten
17 optlist, args = getopt.getopt(sys.argv[1:],'o:')
26 double_punct_char_separator = langdefs.LANGDICT[lang].double_punct_char_sep
27 my_gettext = langdefs.translation[lang]
29 html_codes = ((' -- ', ' – '),
30 (' --- ', ' — '),
32 texi_html_conversion = {
35 (re.compile (r'(?:<samp><span class="command">|<code>)(.*?)(?:</span></samp>|</code>)'),
38 (re.compile (r'@command{(.*?)}'),
43 (re.compile (r'<code>(.*?)</code>'),
46 (re.compile (r'@code{(.*?)}'),
51 whitespaces = re.compile (r'\s+')
57 str = whitespaces.sub (' ', s)
59 str = str.replace (c[1], c[0])
60 for command in texi_html_conversion:
61 d = texi_html_conversion[command]
62 str = d['html2texi'][0].sub (d['html2texi'][1], str)
63 str = my_gettext (str)
64 str = d['texi2html'][0].sub (d['texi2html'][1], str)
66 str = str.replace (c[0], c[1])
69 link_re = re.compile (r'<link rel="(up|prev|next)" (.*?) title="([^"]*?)">')
72 return '<link rel="' + m.group (1) + '" ' + m.group (2) \
73 + ' title="' + _ (m.group (3)) + '">'
75 makeinfo_title_re = re.compile (r'<title>([^<]*?) - ([^<]*?)</title>')
77 def makeinfo_title_gettext (m):
78 return '<title>' + _ (m.group (1)) + ' - ' + m.group (2) + '</title>'
80 texi2html_title_re = re.compile (r'<title>(.+): ([A-Z\d.]+ |)(.+?)</title>')
82 def texi2html_title_gettext (m):
83 return '<title>' + _ (m.group (1)) + double_punct_char_separator + ': ' \
84 + m.group (2) + _ (m.group (3)) + '</title>'
86 a_href_re = re.compile ('(?s)<a (?P<attributes>[^>]*?href="[\\w.#-_]+"[^>]*?>)(?P<code><code>)?\
87 (?P<appendix>Appendix )?(?P<leading>[A-Z0-9.]+ | (?:<){1,2} | [^>:]+?: | |)\
88 (?P<name>(?:<samp><span class="command">|</?code>|</span>|[^>])+?)(?P<end_code>(?(code)</code>|))\
89 (?P<trailing> (?:>){1,2} | |)</a>:?')
91 def a_href_gettext (m):
93 if m.group(0)[-1] == ':':
94 s = double_punct_char_separator + ':'
96 if m.group ('appendix'):
97 t = _ (m.group ('appendix'))
98 return '<a ' + m.group ('attributes') + (m.group ('code') or '') + \
99 t + m.group ('leading') + _ (m.group ('name')) + \
100 m.group ('end_code') + m.group ('trailing') + '</a>' + s
102 h_re = re.compile (r'<h(\d)( class="\w+"|)>\s*(Appendix |)([A-Z\d.]+ |)(.+?)\s*</h\1>')
109 return '<h' + m.group (1) + m.group (2) + '>' + s +\
110 m.group (4) + _ (m.group (5)) + '</h' + m.group (1) + '>'
112 for filename in files:
113 f = open (filename, 'r')
116 page = link_re.sub (link_gettext, page)
117 page = makeinfo_title_re.sub (makeinfo_title_gettext, page)
118 page = texi2html_title_re.sub (texi2html_title_gettext, page)
119 page = a_href_re.sub (a_href_gettext, page)
120 page = h_re.sub (h_gettext, page)
121 for w in ('Next:', 'Previous:', 'Up:'):
122 page = page.replace (w, _ (w))
123 page = langdefs.LANGDICT[lang].html_filter (page)
124 f = open (os.path.join (outdir, filename), 'w')