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