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