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