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