]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
Update it.po
[lyx.git] / lib / scripts / prefs2prefs_prefs.py
1 # -*- coding: utf-8 -*-
2
3 # file prefs2prefs-prefs.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6
7 # author Richard Heck
8
9 # Full author contact details are available in file CREDITS
10
11 # This file houses conversion information for the preferences file.
12
13 # The converter functions take a line as argument and return a list: 
14 #       (Bool, NewLine), 
15 # where the Bool says if  we've modified anything and the NewLine is 
16 # the new line, if so, which will be used to replace the old line.
17
18 # Incremented to format 2, r39670 by jrioux
19 #   Support for multiple file extensions per format.
20 #   No conversion necessary.
21
22 # Incremented to format 3, r39705 by tommaso
23 #   Support for file formats that are natively (g)zipped.
24 #   We must add the flag zipped=native to formats that
25 #   were previously hardcoded in the C++ source: dia.
26
27 # Incremented to format 4, r40028 by vfr
28 #   Remove support for default paper size.
29
30 # Incremented to format 5, r40030 by vfr
31 #   Add a default length unit.
32 #   No conversion necessary.
33
34 # Incremented to format 6, r40515 by younes
35 #   Add use_qimage option.
36 #   No conversion necessary.
37
38 # Incremented to format 7, r40789 by gb
39 #   Add mime type to file format
40
41 # Incremented to format 8, 288c1e0f by rgh
42 #   Add "nice" flag for converters
43 #   No conversion necessary.
44
45 # Incremented to format 9, a18af620 by spitz
46 #  Remove default_language rc.
47
48 # Incremented to format 10, 4985015 by tommaso
49 #  Add close_buffer_with_last_view in preferences.
50 #  No conversion necessary.
51
52 # Incremented to format 11, by gb
53 #   Split pdf format into pdf and pdf6
54
55 # Incremented to format 12, by vfr
56 #   Add option to use the system's theme icons
57 #   No conversion necessary.
58
59 # Incremented to format 13, by bh
60 #   Rename mac_like_word_movement to mac_like_cursor_movement
61
62 # Incremented to format 14, by spitz
63 #   New RC default_otf_view_format
64 #   No conversion necessary.
65
66 # Incremented to format 15, by prannoy
67 #   Add fullscreen_statusbar
68 #   No conversion necessary.
69
70 # Incremented to format 16, by lasgouttes
71 #  Remove force_paint_single_char rc.
72
73 # Incremented to format 17, by lasgouttes
74 #  Remove rtl_support rc.
75
76 # Incremented to format 18, by ef
77 #   Add option to allow saving the document directory
78 #   No conversion necessary.
79
80 # Incremented to format 19, by rgh
81 #   remove print support
82
83 # Incremented to format 20, by tommaso
84 #   Add options to forbid/ignore 'needauth' option
85 #   No conversion necessary.
86
87 # Incremented to format 21, by spitz
88 #   Add jbibtex_alternatives, allow "automatic" value
89 #   of bibtex_command and jbibtex_command (actually the
90 #   default now)
91 #   No conversion necessary.
92
93 # Incremented to format 22, by ef
94 #   Add pygmentize_command for the python pygments syntax highlighter
95 #   No conversion necessary.
96
97 # Incremented to format 23, by spitz
98 #   Add default_platex_view_format, a default output format for
99 #   Japanese documents via pLaTeX.
100 #   No conversion necessary.
101
102 # Incremented to format 24, by spitz
103 #   Rename collapsable to collapsible
104
105 # NOTE: The format should also be updated in LYXRC.cpp and
106 # in configure.py.
107
108 import re
109
110 ###########################################################
111 #
112 # Conversion chain
113
114 def get_format(line):
115         entries = []
116         i = 0
117         while i < len(line):
118                 if line[i] == '"':
119                         beg = i + 1
120                         i = i + 1
121                         while i < len(line) and line[i] != '"':
122                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
123                                         # convert \" to "
124                                         i = i + 1
125                                 i = i + 1
126                         end = i
127                         entries.append(line[beg:end].replace('\\"', '"'))
128                 elif line[i] == '#':
129                         return entries
130                 elif not line[i].isspace():
131                         beg = i
132                         while i < len(line) and not line[i].isspace():
133                                 i = i + 1
134                         end = i
135                         entries.append(line[beg:end])
136                 i = i + 1
137         return entries
138
139
140 def simple_renaming(line, old, new):
141         i = line.lower().find(old.lower())
142         if i == -1:
143                 return no_match
144         line = line[:i] + new + line[i+len(old):]
145         return (True, line)
146
147 no_match = (False, [])
148
149 ######################################
150 ### Format 1 conversions (for LyX 2.0)
151
152 def remove_obsolete(line):
153         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
154                 "\\plaintext_roff_command", "\\use_alt_language",
155                 "\\use_escape_chars", "\\use_input_encoding",
156                 "\\use_personal_dictionary", "\\use_pspell",
157                 "\\use_spell_lib")
158         line = line.lower().lstrip()
159         for tag in tags:
160                 if line.lower().startswith(tag):
161                         return (True, "")
162         return no_match
163
164
165 def language_use_babel(line):
166         if not line.lower().startswith("\language_use_babel"):
167                 return no_match
168         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
169         m = re_lub.match(line)
170         val = m.group(1)
171         newval = '0'
172         if val == 'false':
173                 newval = '3'
174         newline = "\\language_package_selection " + newval
175         return (True, newline)
176
177
178 def language_package(line):
179         return simple_renaming(line, "\\language_package", "\\language_custom_package")
180
181
182 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
183 def latex_flavor(line):
184         if not line.lower().startswith("\\converter"):
185                 return no_match
186         m = lfre.match(line)
187         if not m:
188                 return no_match
189         conv = m.group(1)
190         fmat = m.group(2)
191         args = m.group(3)
192         conv2fl = {
193                    "luatex":   "lualatex",
194                    "pplatex":  "latex",
195                    "xetex":    "xelatex",
196                   }
197         if conv in conv2fl.keys():
198                 flavor = conv2fl[conv]
199         else:
200                 flavor = conv
201         if flavor == "latex":
202                 return no_match
203         return (True,
204                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
205
206
207 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
208 def export_menu(line):
209         if not line.lower().startswith("\\format"):
210                 return no_match
211         m = emre.match(line)
212         if not m:
213                 return no_match
214         fmat = m.group(1)
215         opts = m.group(2)
216         return (True,
217                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
218
219 # End format 1 conversions (for LyX 2.0)
220 ########################################
221
222 #################################
223 # Conversions from LyX 2.0 to 2.1
224 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
225 def zipped_native(line):
226         if not line.lower().startswith("\\format"):
227                 return no_match
228         m = zipre.match(line)
229         if not m:
230                 return no_match
231         fmat = m.group(1)
232         opts = m.group(2)
233         return (True,
234                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
235
236 def remove_default_papersize(line):
237         if not line.lower().startswith("\\default_papersize"):
238                 return no_match
239         return (True, "")
240
241 def add_mime_types(line):
242         if not line.lower().startswith("\\format"):
243                 return no_match
244         entries = get_format(line)
245         converted = line
246         i = len(entries)
247         while i < 7:
248                 converted = converted + '       ""'
249                 i = i + 1
250         formats = {'tgif':'application/x-tgif', \
251                 'fig':'application/x-xfig', \
252                 'dia':'application/x-dia-diagram', \
253                 'odg':'application/vnd.oasis.opendocument.graphics', \
254                 'svg':'image/svg+xml', \
255                 'bmp':'image/x-bmp', \
256                 'gif':'image/gif', \
257                 'jpg':'image/jpeg', \
258                 'pbm':'image/x-portable-bitmap', \
259                 'pgm':'image/x-portable-graymap', \
260                 'png':'image/x-png', \
261                 'ppm':'image/x-portable-pixmap', \
262                 'tiff':'image/tiff', \
263                 'xbm':'image/x-xbitmap', \
264                 'xpm':'image/x-xpixmap', \
265                 'docbook-xml':'application/docbook+xml', \
266                 'dot':'text/vnd.graphviz', \
267                 'ly':'text/x-lilypond', \
268                 'latex':'text/x-tex', \
269                 'text':'text/plain', \
270                 'gnumeric':'application/x-gnumeric', \
271                 'excel':'application/vnd.ms-excel', \
272                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
273                 'xhtml':'application/xhtml+xml', \
274                 'bib':'text/x-bibtex', \
275                 'eps':'image/x-eps', \
276                 'ps':'application/postscript', \
277                 'pdf':'application/pdf', \
278                 'dvi':'application/x-dvi', \
279                 'html':'text/html', \
280                 'odt':'application/vnd.oasis.opendocument.text', \
281                 'sxw':'application/vnd.sun.xml.writer', \
282                 'rtf':'application/rtf', \
283                 'doc':'application/msword', \
284                 'csv':'text/csv', \
285                 'lyx':'application/x-lyx', \
286                 'wmf':'image/x-wmf', \
287                 'emf':'image/x-emf'}
288         if entries[1] in formats.keys():
289                 converted = converted + '       "' + formats[entries[1]] + '"'
290         else:
291                 converted = converted + '       ""'
292         return (True, converted)
293
294 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
295
296 def split_pdf_format(line):
297         # strictly speaking, a new format would not require to bump the
298         # version number, but the old pdf format was hardcoded at several
299         # places in the C++ code, so an update seemed like a good idea.
300         if line.lower().startswith("\\format"):
301                 entries = get_format(line)
302                 if entries[1] == 'pdf':
303                         if len(entries) < 6:
304                                 viewer = ''
305                         else:
306                                 viewer = entries[5]
307                         converted = line.replace('application/pdf', '') + '''
308 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
309                         return (True, converted)
310         elif line.lower().startswith("\\viewer_alternatives") or \
311              line.lower().startswith("\\editor_alternatives"):
312                 entries = get_format(line)
313                 if entries[1] == 'pdf':
314                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
315                         return (True, converted)
316         elif re_converter.match(line):
317                 entries = get_format(line)
318                 # The only converter from pdf that is touched is pdf->eps:
319                 # All other converters are likely meant for further processing on export.
320                 # The only converter to pdf that stays untouched is dvi->pdf:
321                 # All other converters are likely meant for graphics.
322                 if len(entries) > 2 and \
323                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
324                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
325                         if entries[1] == 'pdf':
326                                 converted = entries[0] + ' pdf6 ' + entries[2]
327                         else:
328                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
329                         i = 3
330                         while i < len(entries):
331                                 converted = converted + ' "' + entries[i] + '"'
332                                 i = i + 1
333                         return (True, converted)
334         return no_match
335
336 def remove_default_language(line):
337         if not line.lower().startswith("\\default_language"):
338                 return no_match
339         return (True, "")
340
341 def mac_cursor_movement(line):
342         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
343
344 # End conversions for LyX 2.0 to 2.1
345 ####################################
346
347
348 #################################
349 # Conversions from LyX 2.1 to 2.2
350
351 def remove_force_paint_single_char(line):
352         if not line.lower().startswith("\\force_paint_single_char"):
353                 return no_match
354         return (True, "")
355
356 def remove_rtl(line):
357         if not line.lower().startswith("\\rtl "):
358                 return no_match
359         return (True, "")
360
361 def remove_print_support(line):
362         tags = ("\\printer", "\\print_adapt_output", "\\print_command",
363                 "\\print_evenpage_flag", "\\print_oddpage_flag", "\\print_pagerange_flag",
364                 "\\print_copies_flag", "\\print_collcopies_flag", "\\print_reverse_flag",
365                 "\\print_to_printer", "\\print_to_file", "\\print_file_extension")
366         line = line.lower().lstrip()
367         for tag in tags:
368                 if line.lower().startswith(tag):
369                         return (True, "")
370         return no_match
371
372 # End conversions for LyX 2.1 to 2.2
373 ####################################
374
375 #################################
376 # Conversions from LyX 2.2 to 2.3
377
378 def rename_collapsible(line):
379         return simple_renaming(line, "\\set_color \"collapsable", "\\set_color \"collapsible")
380
381 # End conversions for LyX 2.2 to 2.3
382 ####################################
383
384
385 ############################################################
386 # Format-conversion map. Also add empty format changes here.
387
388 conversions = [
389         [  1, [ # there were several conversions for format 1
390                 export_menu,
391                 latex_flavor,
392                 remove_obsolete,
393                 language_use_babel,
394                 language_package
395         ]],
396         [ 2, []],
397         [ 3, [ zipped_native ]],
398         [ 4, [ remove_default_papersize ]],
399         [ 5, []],
400         [ 6, []],
401         [ 7, [add_mime_types]],
402         [ 8, []],
403         [ 9, [ remove_default_language ]],
404         [ 10, []],
405         [ 11, [split_pdf_format]],
406         [ 12, []],
407         [ 13, [mac_cursor_movement]],
408         [ 14, []],
409         [ 15, []],
410         [ 16, [remove_force_paint_single_char]],
411         [ 17, [remove_rtl]],
412         [ 18, []],
413         [ 19, [remove_print_support]],
414         [ 20, []],
415         [ 21, []],
416         [ 22, []],
417         [ 23, []],
418         [ 24, [rename_collapsible]]
419 ]