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