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