]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
6007a95cc3535f44453a32f6df976696b7e14109
[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 # Incremented to format 25, by lasgouttes
106 #   Remove use_qimage preference
107
108 # Incremented to format 26, by spitz
109 #   Rename font_encoding preference
110
111 # NOTE: The format should also be updated in LYXRC.cpp and
112 # in configure.py.
113
114 import re
115
116 ###########################################################
117 #
118 # Conversion chain
119
120 def get_format(line):
121         entries = []
122         i = 0
123         while i < len(line):
124                 if line[i] == '"':
125                         beg = i + 1
126                         i = i + 1
127                         while i < len(line) and line[i] != '"':
128                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
129                                         # convert \" to "
130                                         i = i + 1
131                                 i = i + 1
132                         end = i
133                         entries.append(line[beg:end].replace('\\"', '"'))
134                 elif line[i] == '#':
135                         return entries
136                 elif not line[i].isspace():
137                         beg = i
138                         while i < len(line) and not line[i].isspace():
139                                 i = i + 1
140                         end = i
141                         entries.append(line[beg:end])
142                 i = i + 1
143         return entries
144
145
146 def simple_renaming(line, old, new):
147         i = line.lower().find(old.lower())
148         if i == -1:
149                 return no_match
150         line = line[:i] + new + line[i+len(old):]
151         return (True, line)
152
153 no_match = (False, [])
154
155 ######################################
156 ### Format 1 conversions (for LyX 2.0)
157
158 def remove_obsolete(line):
159         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
160                 "\\plaintext_roff_command", "\\use_alt_language",
161                 "\\use_escape_chars", "\\use_input_encoding",
162                 "\\use_personal_dictionary", "\\use_pspell",
163                 "\\use_spell_lib")
164         line = line.lower().lstrip()
165         for tag in tags:
166                 if line.lower().startswith(tag):
167                         return (True, "")
168         return no_match
169
170
171 def language_use_babel(line):
172         if not line.lower().startswith("\language_use_babel"):
173                 return no_match
174         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
175         m = re_lub.match(line)
176         val = m.group(1)
177         newval = '0'
178         if val == 'false':
179                 newval = '3'
180         newline = "\\language_package_selection " + newval
181         return (True, newline)
182
183
184 def language_package(line):
185         return simple_renaming(line, "\\language_package", "\\language_custom_package")
186
187
188 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
189 def latex_flavor(line):
190         if not line.lower().startswith("\\converter"):
191                 return no_match
192         m = lfre.match(line)
193         if not m:
194                 return no_match
195         conv = m.group(1)
196         fmat = m.group(2)
197         args = m.group(3)
198         conv2fl = {
199                    "luatex":   "lualatex",
200                    "pplatex":  "latex",
201                    "xetex":    "xelatex",
202                   }
203         if conv in conv2fl.keys():
204                 flavor = conv2fl[conv]
205         else:
206                 flavor = conv
207         if flavor == "latex":
208                 return no_match
209         return (True,
210                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
211
212
213 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
214 def export_menu(line):
215         if not line.lower().startswith("\\format"):
216                 return no_match
217         m = emre.match(line)
218         if not m:
219                 return no_match
220         fmat = m.group(1)
221         opts = m.group(2)
222         return (True,
223                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
224
225 # End format 1 conversions (for LyX 2.0)
226 ########################################
227
228 #################################
229 # Conversions from LyX 2.0 to 2.1
230 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
231 def zipped_native(line):
232         if not line.lower().startswith("\\format"):
233                 return no_match
234         m = zipre.match(line)
235         if not m:
236                 return no_match
237         fmat = m.group(1)
238         opts = m.group(2)
239         return (True,
240                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
241
242 def remove_default_papersize(line):
243         if not line.lower().startswith("\\default_papersize"):
244                 return no_match
245         return (True, "")
246
247 def add_mime_types(line):
248         if not line.lower().startswith("\\format"):
249                 return no_match
250         entries = get_format(line)
251         converted = line
252         i = len(entries)
253         while i < 7:
254                 converted = converted + '       ""'
255                 i = i + 1
256         formats = {'tgif':'application/x-tgif', \
257                 'fig':'application/x-xfig', \
258                 'dia':'application/x-dia-diagram', \
259                 'odg':'application/vnd.oasis.opendocument.graphics', \
260                 'svg':'image/svg+xml', \
261                 'bmp':'image/x-bmp', \
262                 'gif':'image/gif', \
263                 'jpg':'image/jpeg', \
264                 'pbm':'image/x-portable-bitmap', \
265                 'pgm':'image/x-portable-graymap', \
266                 'png':'image/x-png', \
267                 'ppm':'image/x-portable-pixmap', \
268                 'tiff':'image/tiff', \
269                 'xbm':'image/x-xbitmap', \
270                 'xpm':'image/x-xpixmap', \
271                 'docbook-xml':'application/docbook+xml', \
272                 'dot':'text/vnd.graphviz', \
273                 'ly':'text/x-lilypond', \
274                 'latex':'text/x-tex', \
275                 'text':'text/plain', \
276                 'gnumeric':'application/x-gnumeric', \
277                 'excel':'application/vnd.ms-excel', \
278                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
279                 'xhtml':'application/xhtml+xml', \
280                 'bib':'text/x-bibtex', \
281                 'eps':'image/x-eps', \
282                 'ps':'application/postscript', \
283                 'pdf':'application/pdf', \
284                 'dvi':'application/x-dvi', \
285                 'html':'text/html', \
286                 'odt':'application/vnd.oasis.opendocument.text', \
287                 'sxw':'application/vnd.sun.xml.writer', \
288                 'rtf':'application/rtf', \
289                 'doc':'application/msword', \
290                 'csv':'text/csv', \
291                 'lyx':'application/x-lyx', \
292                 'wmf':'image/x-wmf', \
293                 'emf':'image/x-emf'}
294         if entries[1] in formats.keys():
295                 converted = converted + '       "' + formats[entries[1]] + '"'
296         else:
297                 converted = converted + '       ""'
298         return (True, converted)
299
300 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
301
302 def split_pdf_format(line):
303         # strictly speaking, a new format would not require to bump the
304         # version number, but the old pdf format was hardcoded at several
305         # places in the C++ code, so an update seemed like a good idea.
306         if line.lower().startswith("\\format"):
307                 entries = get_format(line)
308                 if entries[1] == 'pdf':
309                         if len(entries) < 6:
310                                 viewer = ''
311                         else:
312                                 viewer = entries[5]
313                         converted = line.replace('application/pdf', '') + '''
314 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
315                         return (True, converted)
316         elif line.lower().startswith("\\viewer_alternatives") or \
317              line.lower().startswith("\\editor_alternatives"):
318                 entries = get_format(line)
319                 if entries[1] == 'pdf':
320                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
321                         return (True, converted)
322         elif re_converter.match(line):
323                 entries = get_format(line)
324                 # The only converter from pdf that is touched is pdf->eps:
325                 # All other converters are likely meant for further processing on export.
326                 # The only converter to pdf that stays untouched is dvi->pdf:
327                 # All other converters are likely meant for graphics.
328                 if len(entries) > 2 and \
329                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
330                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
331                         if entries[1] == 'pdf':
332                                 converted = entries[0] + ' pdf6 ' + entries[2]
333                         else:
334                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
335                         i = 3
336                         while i < len(entries):
337                                 converted = converted + ' "' + entries[i] + '"'
338                                 i = i + 1
339                         return (True, converted)
340         return no_match
341
342 def remove_default_language(line):
343         if not line.lower().startswith("\\default_language"):
344                 return no_match
345         return (True, "")
346
347 def mac_cursor_movement(line):
348         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
349
350 # End conversions for LyX 2.0 to 2.1
351 ####################################
352
353
354 #################################
355 # Conversions from LyX 2.1 to 2.2
356
357 def remove_force_paint_single_char(line):
358         if not line.lower().startswith("\\force_paint_single_char"):
359                 return no_match
360         return (True, "")
361
362 def remove_rtl(line):
363         if not line.lower().startswith("\\rtl "):
364                 return no_match
365         return (True, "")
366
367 def remove_print_support(line):
368         tags = ("\\printer", "\\print_adapt_output", "\\print_command",
369                 "\\print_evenpage_flag", "\\print_oddpage_flag", "\\print_pagerange_flag",
370                 "\\print_copies_flag", "\\print_collcopies_flag", "\\print_reverse_flag",
371                 "\\print_to_printer", "\\print_to_file", "\\print_file_extension")
372         line = line.lower().lstrip()
373         for tag in tags:
374                 if line.lower().startswith(tag):
375                         return (True, "")
376         return no_match
377
378 # End conversions for LyX 2.1 to 2.2
379 ####################################
380
381
382 #################################
383 # Conversions from LyX 2.2 to 2.3
384
385 def rename_collapsible(line):
386         return simple_renaming(line, "\\set_color \"collapsable", "\\set_color \"collapsible")
387
388 # End conversions for LyX 2.2 to 2.3
389 ####################################
390
391
392 #################################
393 # Conversions from LyX 2.3 to 2.4
394
395 def remove_use_qimage(line):
396         if not line.lower().startswith("\\use_qimage "):
397                 return no_match
398         return (True, "")
399
400 def remove_font_encoding(line):
401         if not line.lower().startswith("\\font_encoding "):
402                 return no_match
403         return (True, "")
404
405 # End conversions for LyX 2.3 to 2.4
406 ####################################
407
408
409
410 ############################################################
411 # Format-conversion map. Also add empty format changes here.
412
413 conversions = [
414         [  1, [ # there were several conversions for format 1
415                 export_menu,
416                 latex_flavor,
417                 remove_obsolete,
418                 language_use_babel,
419                 language_package
420         ]],
421         [ 2, []],
422         [ 3, [ zipped_native ]],
423         [ 4, [ remove_default_papersize ]],
424         [ 5, []],
425         [ 6, []],
426         [ 7, [add_mime_types]],
427         [ 8, []],
428         [ 9, [ remove_default_language ]],
429         [ 10, []],
430         [ 11, [split_pdf_format]],
431         [ 12, []],
432         [ 13, [mac_cursor_movement]],
433         [ 14, []],
434         [ 15, []],
435         [ 16, [remove_force_paint_single_char]],
436         [ 17, [remove_rtl]],
437         [ 18, []],
438         [ 19, [remove_print_support]],
439         [ 20, []],
440         [ 21, []],
441         [ 22, []],
442         [ 23, []],
443         [ 24, [rename_collapsible]],
444         [ 25, [remove_use_qimage]],
445         [ 26, [remove_font_encoding]]
446 ]