Make --without-include-boost compile (autotools)
[lyx.git] / po / lyx_pot.py
blob40cb9bc53518e6e62aa27e14fc78a6134a85ff95
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 # file lyx_pot.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
8 # \author Bo Peng
10 # Full author contact details are available in file CREDITS
12 # Usage: use
13 # lyx_pot.py -h
14 # to get usage message
16 # This script will extract translatable strings from input files and write
17 # to output in gettext .pot format.
19 import sys, os, re, getopt
21 def relativePath(path, base):
22 '''return relative path from top source dir'''
23 # full pathname of path
24 path1 = os.path.normpath(os.path.realpath(path)).split(os.sep)
25 path2 = os.path.normpath(os.path.realpath(base)).split(os.sep)
26 if path1[:len(path2)] != path2:
27 print "Path %s is not under top source directory" % path
28 path3 = os.path.join(*path1[len(path2):]);
29 # replace all \ by / such that we get the same comments on Windows and *nix
30 path3 = path3.replace('\\', '/')
31 return path3
34 def ui_l10n(input_files, output, base):
35 '''Generate pot file from lib/ui/*'''
36 output = open(output, 'w')
37 Submenu = re.compile(r'^[^#]*Submenu\s+"([^"]*)"')
38 Popupmenu = re.compile(r'^[^#]*PopupMenu\s+"[^"]+"\s+"([^"]*)"')
39 Toolbar = re.compile(r'^[^#]*Toolbar\s+"[^"]+"\s+"([^"]*)"')
40 Item = re.compile(r'[^#]*Item\s+"([^"]*)"')
41 TableInsert = re.compile(r'[^#]*TableInsert\s+"([^"]*)"')
42 for src in input_files:
43 input = open(src)
44 for lineno, line in enumerate(input.readlines()):
45 if Submenu.match(line):
46 (string,) = Submenu.match(line).groups()
47 string = string.replace('_', ' ')
48 elif Popupmenu.match(line):
49 (string,) = Popupmenu.match(line).groups()
50 elif Toolbar.match(line):
51 (string,) = Toolbar.match(line).groups()
52 elif Item.match(line):
53 (string,) = Item.match(line).groups()
54 elif TableInsert.match(line):
55 (string,) = TableInsert.match(line).groups()
56 else:
57 continue
58 string = string.replace('"', '')
59 if string != "":
60 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
61 (relativePath(src, base), lineno+1, string)
62 input.close()
63 output.close()
66 def layouts_l10n(input_files, output, base):
67 '''Generate pot file from lib/layouts/*.layout and *.inc'''
68 output = open(output, 'w')
69 Style = re.compile(r'^Style\s+(.*)')
70 # include ???LabelString???, but exclude comment lines
71 LabelString = re.compile(r'^[^#]*LabelString\S*\s+(.*)')
72 GuiName = re.compile(r'\s*GuiName\s+(.*)')
73 ListName = re.compile(r'\s*ListName\s+(.*)')
74 for src in input_files:
75 input = open(src)
76 for lineno, line in enumerate(input.readlines()):
77 if Style.match(line):
78 (string,) = Style.match(line).groups()
79 string = string.replace('_', ' ')
80 elif LabelString.match(line):
81 (string,) = LabelString.match(line).groups()
82 elif GuiName.match(line):
83 (string,) = GuiName.match(line).groups()
84 elif ListName.match(line):
85 (string,) = ListName.match(line).groups()
86 else:
87 continue
88 string = string.replace('\\', '\\\\').replace('"', '')
89 if string != "":
90 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
91 (relativePath(src, base), lineno+1, string)
92 input.close()
93 output.close()
96 def qt4_l10n(input_files, output, base):
97 '''Generate pot file from src/frontends/qt4/ui/*.ui'''
98 output = open(output, 'w')
99 pat = re.compile(r'\s*<string>(.*)</string>')
100 prop = re.compile(r'\s*<property.*name.*=.*shortcut')
101 for src in input_files:
102 input = open(src)
103 skipNextLine = False
104 for lineno, line in enumerate(input.readlines()):
105 # skip the line after <property name=shortcut>
106 if skipNextLine:
107 skipNextLine = False
108 continue
109 if prop.match(line):
110 skipNextLine = True
111 continue
112 # get lines that match <string>...</string>
113 if pat.match(line):
114 (string,) = pat.match(line).groups()
115 string = string.replace('&amp;', '&').replace('&lt;', '<').replace('&gt;', '>').replace('"', r'\"')
116 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
117 (relativePath(src, base), lineno+1, string)
118 input.close()
119 output.close()
122 def languages_l10n(input_files, output, base):
123 '''Generate pot file from lib/language'''
124 output = open(output, 'w')
125 # assuming only one language file
126 reg = re.compile('[\w-]+\s+[\w"]+\s+"([\w \-\(\)]+)"\s+(true|false)\s+[\w-]+\s+\w+\s+"[^"]*"')
127 input = open(input_files[0])
128 for lineno, line in enumerate(input.readlines()):
129 if line[0] == '#':
130 continue
131 # From:
132 # afrikaans afrikaans "Afrikaans" false iso8859-15 af_ZA ""
133 # To:
134 # #: lib/languages:2
135 # msgid "Afrikaans"
136 # msgstr ""
137 if reg.match(line):
138 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
139 (relativePath(input_files[0], base), lineno+1, reg.match(line).groups()[0])
140 else:
141 print "Error: Unable to handle line:"
142 print line
143 sys.exit(1)
144 input.close()
145 output.close()
148 def external_l10n(input_files, output, base):
149 '''Generate pot file from lib/external_templates'''
150 output = open(output, 'w')
151 Template = re.compile(r'^Template\s+(.*)')
152 GuiName = re.compile(r'\s*GuiName\s+(.*)')
153 HelpTextStart = re.compile(r'\s*HelpText\s')
154 HelpTextSection = re.compile(r'\s*(\S.*)\s*$')
155 HelpTextEnd = re.compile(r'\s*HelpTextEnd\s')
156 i = -1
157 for src in input_files:
158 input = open(src)
159 inHelp = False
160 hadHelp = False
161 prev_help_string = ''
162 for lineno, line in enumerate(input.readlines()):
163 if Template.match(line):
164 (string,) = Template.match(line).groups()
165 elif GuiName.match(line):
166 (string,) = GuiName.match(line).groups()
167 elif inHelp:
168 if HelpTextEnd.match(line):
169 if hadHelp:
170 print >> output, '\nmsgstr ""\n'
171 inHelp = False
172 hadHelp = False
173 prev_help_string = ''
174 elif HelpTextSection.match(line):
175 (help_string,) = HelpTextSection.match(line).groups()
176 help_string = help_string.replace('"', '')
177 if help_string != "" and prev_help_string == '':
178 print >> output, '#: %s:%d\nmsgid ""\n"%s\\n"' % \
179 (relativePath(src, base), lineno+1, help_string)
180 hadHelp = True
181 elif help_string != "":
182 print >> output, '"%s\\n"' % help_string
183 prev_help_string = help_string
184 elif HelpTextStart.match(line):
185 inHelp = True
186 prev_help_string = ''
187 else:
188 continue
189 string = string.replace('"', '')
190 if string != "" and not inHelp:
191 print >> output, '#: %s:%d\nmsgid "%s"\nmsgstr ""\n' % \
192 (relativePath(src, base), lineno+1, string)
193 input.close()
194 output.close()
197 Usage = '''
198 lyx_pot.py [-b|--base top_src_dir] [-o|--output output_file] [-h|--help] -t|--type input_type input_files
200 where
201 --base:
202 path to the top source directory. default to '.'
203 --output:
204 output pot file, default to './lyx.pot'
205 --input_type can be
206 ui: lib/ui/*
207 layouts: lib/layouts/*
208 qt4: qt4 ui files
209 languages: file lib/languages
210 external: external templates file
213 if __name__ == '__main__':
214 input_type = None
215 output = 'lyx.pot'
216 base = '.'
218 optlist, args = getopt.getopt(sys.argv[1:], 'ht:o:b:',
219 ['help', 'type=', 'output=', 'base='])
220 for (opt, value) in optlist:
221 if opt in ['-h', '--help']:
222 print Usage
223 sys.exit(0)
224 elif opt in ['-o', '--output']:
225 output = value
226 elif opt in ['-b', '--base']:
227 base = value
228 elif opt in ['-t', '--type']:
229 input_type = value
230 if input_type not in ['ui', 'layouts', 'qt4', 'languages', 'external'] or output is None:
231 print 'Wrong input type or output filename.'
232 sys.exit(1)
233 if input_type == 'ui':
234 ui_l10n(args, output, base)
235 elif input_type == 'layouts':
236 layouts_l10n(args, output, base)
237 elif input_type == 'qt4':
238 qt4_l10n(args, output, base)
239 elif input_type == 'external':
240 external_l10n(args, output, base)
241 else:
242 languages_l10n(args, output, base)