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