]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
1bbe22e85e577a5fcc0855bf77a14e96977d95c0
[lyx.git] / lib / scripts / prefs2prefs_prefs.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # file prefs2prefs-prefs.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
7
8 # author Richard Heck
9
10 # Full author contact details are available in file CREDITS
11
12 # This file houses conversion information for the preferences file.
13
14 # The converter functions take a line as argument and return a list: 
15 #       (Bool, NewLine), 
16 # where the Bool says if  we've modified anything and the NewLine is 
17 # the new line, if so, which will be used to replace the old line.
18
19 # Incremented to format 2, r39670 by jrioux
20 #   Support for multiple file extensions per format.
21 #   No conversion necessary.
22
23 # Incremented to format 3, r39705 by tommaso
24 #   Support for file formats that are natively (g)zipped.
25 #   We must add the flag zipped=native to formats that
26 #   were previously hardcoded in the C++ source: dia.
27
28 # Incremented to format 4, r40028 by vfr
29 #   Remove support for default paper size.
30
31 # Incremented to format 5, r40030 by vfr
32 #   Add a default length unit.
33 #   No conversion necessary.
34
35 # Incremented to format 6, r40515 by younes
36 #   Add use_qimage option.
37 #   No conversion necessary.
38
39 # Incremented to format 7, r40789 by gb
40 #   Add mime type to file format
41
42 # Incremented to format 8, 288c1e0f by rgh
43 #   Add "nice" flag for converters
44 #   No conversion necessary.
45
46 # Incremented to format 9, a18af620 by spitz
47 #  Remove default_language rc.
48
49 # Incremented to format 10, 4985015 by tommaso
50 #  Add close_buffer_with_last_view in preferences.
51 #  No conversion necessary.
52
53 # Incremented to format 11, by gb
54 #   Split pdf format into pdf and pdf6
55
56 # Incremented to format 12, by vfr
57 #   Add option to use the system's theme icons
58 #   No conversion necessary.
59
60 # Incremented to format 13, by bh
61 #   Rename mac_like_word_movement to mac_like_cursor_movement
62
63 # Incremented to format 14, by spitz
64 #   New RC default_otf_view_format
65 #   No conversion necessary.
66
67 import re
68
69 ###########################################################
70 #
71 # Conversion chain
72
73 def get_format(line):
74         entries = []
75         i = 0
76         while i < len(line):
77                 if line[i] == '"':
78                         beg = i + 1
79                         i = i + 1
80                         while i < len(line) and line[i] != '"':
81                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
82                                         # convert \" to "
83                                         i = i + 1
84                                 i = i + 1
85                         end = i
86                         entries.append(line[beg:end].replace('\\"', '"'))
87                 elif line[i] == '#':
88                         return entries
89                 elif not line[i].isspace():
90                         beg = i
91                         while i < len(line) and not line[i].isspace():
92                                 i = i + 1
93                         end = i
94                         entries.append(line[beg:end])
95                 i = i + 1
96         return entries
97
98
99 def simple_renaming(line, old, new):
100         i = line.lower().find(old.lower())
101         if i == -1:
102                 return no_match
103         line = line[:i] + new + line[i+len(old):]
104         return (True, line)
105
106 no_match = (False, [])
107
108 ######################################
109 ### Format 1 conversions (for LyX 2.0)
110
111 def remove_obsolete(line):
112         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
113                                 "\\plaintext_roff_command", "\\use_alt_language", 
114                                 "\\use_escape_chars", "\\use_input_encoding",
115                                 "\\use_personal_dictionary", "\\use_pspell",
116                                 "\\use_spell_lib")
117         line = line.lower().lstrip()
118         for tag in tags:
119                 if line.lower().startswith(tag):
120                         return (True, "")
121         return no_match
122
123
124 def language_use_babel(line):
125         if not line.lower().startswith("\language_use_babel"):
126                 return no_match
127         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
128         m = re_lub.match(line)
129         val = m.group(1)
130         newval = '0'
131         if val == 'false':
132                 newval = '3'
133         newline = "\\language_package_selection " + newval
134         return (True, newline)
135
136
137 def language_package(line):
138         return simple_renaming(line, "\\language_package", "\\language_custom_package")
139
140
141 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
142 def latex_flavor(line):
143         if not line.lower().startswith("\\converter"):
144                 return no_match
145         m = lfre.match(line)
146         if not m:
147                 return no_match
148         conv = m.group(1)
149         fmat = m.group(2)
150         args = m.group(3)
151         conv2fl = {
152                    "luatex":   "lualatex",
153                    "pplatex":  "latex",
154                    "xetex":    "xelatex",
155                   }
156         if conv in conv2fl.keys():
157                 flavor = conv2fl[conv]
158         else:
159                 flavor = conv
160         if flavor == "latex":
161                 return no_match
162         return (True,
163                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
164
165
166 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
167 def export_menu(line):
168         if not line.lower().startswith("\\format"):
169                 return no_match
170         m = emre.match(line)
171         if not m:
172                 return no_match
173         fmat = m.group(1)
174         opts = m.group(2)
175         return (True,
176                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
177
178 # End format 1 conversions (for LyX 2.0)
179 ########################################
180
181 #################################
182 # Conversions from LyX 2.0 to 2.1
183 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
184 def zipped_native(line):
185         if not line.lower().startswith("\\format"):
186                 return no_match
187         m = zipre.match(line)
188         if not m:
189                 return no_match
190         fmat = m.group(1)
191         opts = m.group(2)
192         return (True,
193                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
194
195 def remove_default_papersize(line):
196         if not line.lower().startswith("\\default_papersize"):
197                 return no_match
198         return (True, "")
199
200 def add_mime_types(line):
201         if not line.lower().startswith("\\format"):
202                 return no_match
203         entries = get_format(line)
204         converted = line
205         i = len(entries)
206         while i < 7:
207                 converted = converted + '       ""'
208                 i = i + 1
209         formats = {'tgif':'application/x-tgif', \
210                 'fig':'application/x-xfig', \
211                 'dia':'application/x-dia-diagram', \
212                 'odg':'application/vnd.oasis.opendocument.graphics', \
213                 'svg':'image/svg+xml', \
214                 'bmp':'image/x-bmp', \
215                 'gif':'image/gif', \
216                 'jpg':'image/jpeg', \
217                 'pbm':'image/x-portable-bitmap', \
218                 'pgm':'image/x-portable-graymap', \
219                 'png':'image/x-png', \
220                 'ppm':'image/x-portable-pixmap', \
221                 'tiff':'image/tiff', \
222                 'xbm':'image/x-xbitmap', \
223                 'xpm':'image/x-xpixmap', \
224                 'docbook-xml':'application/docbook+xml', \
225                 'dot':'text/vnd.graphviz', \
226                 'ly':'text/x-lilypond', \
227                 'latex':'text/x-tex', \
228                 'text':'text/plain', \
229                 'gnumeric':'application/x-gnumeric', \
230                 'excel':'application/vnd.ms-excel', \
231                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
232                 'xhtml':'application/xhtml+xml', \
233                 'bib':'text/x-bibtex', \
234                 'eps':'image/x-eps', \
235                 'ps':'application/postscript', \
236                 'pdf':'application/pdf', \
237                 'dvi':'application/x-dvi', \
238                 'html':'text/html', \
239                 'odt':'application/vnd.oasis.opendocument.text', \
240                 'sxw':'application/vnd.sun.xml.writer', \
241                 'rtf':'application/rtf', \
242                 'doc':'application/msword', \
243                 'csv':'text/csv', \
244                 'lyx':'application/x-lyx', \
245                 'wmf':'image/x-wmf', \
246                 'emf':'image/x-emf'}
247         if entries[1] in formats.keys():
248                 converted = converted + '       "' + formats[entries[1]] + '"'
249         else:
250                 converted = converted + '       ""'
251         return (True, converted)
252
253 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
254
255 def split_pdf_format(line):
256         # strictly speaking, a new format would not require to bump the
257         # version number, but the old pdf format was hardcoded at several
258         # places in the C++ code, so an update seemed like a good idea.
259         if line.lower().startswith("\\format"):
260                 entries = get_format(line)
261                 if entries[1] == 'pdf':
262                         if len(entries) < 6:
263                                 viewer = ''
264                         else:
265                                 viewer = entries[5]
266                         converted = line.replace('application/pdf', '') + '''
267 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
268                         return (True, converted)
269         elif line.lower().startswith("\\viewer_alternatives") or \
270              line.lower().startswith("\\editor_alternatives"):
271                 entries = get_format(line)
272                 if entries[1] == 'pdf':
273                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
274                         return (True, converted)
275         elif re_converter.match(line):
276                 entries = get_format(line)
277                 # The only converter from pdf that is touched is pdf->eps:
278                 # All other converters are likely meant for further processing on export.
279                 # The only converter to pdf that stays untouched is dvi->pdf:
280                 # All other converters are likely meant for graphics.
281                 if len(entries) > 2 and \
282                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
283                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
284                         if entries[1] == 'pdf':
285                                 converted = entries[0] + ' pdf6 ' + entries[2]
286                         else:
287                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
288                         i = 3
289                         while i < len(entries):
290                                 converted = converted + ' "' + entries[i] + '"'
291                                 i = i + 1
292                         return (True, converted)
293         return no_match
294
295 def remove_default_language(line):
296         if not line.lower().startswith("\\default_language"):
297                 return no_match
298         return (True, "")
299
300 def mac_cursor_movement(line):
301         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
302
303 # End conversions for LyX 2.0 to 2.1
304 ####################################
305
306
307 conversions = [
308         [  1, [ # there were several conversions for format 1
309                 export_menu,
310                 latex_flavor,
311                 remove_obsolete,
312                 language_use_babel,
313                 language_package
314         ]],
315         [ 2, []],
316         [ 3, [ zipped_native ]],
317         [ 4, [ remove_default_papersize ]],
318         [ 5, []],
319         [ 6, []],
320         [ 7, [add_mime_types]],
321         [ 8, []],
322         [ 9, [ remove_default_language ]],
323         [ 10, []],
324         [ 11, [split_pdf_format]],
325         [ 12, []],
326         [ 13, [mac_cursor_movement]],
327         [ 14, []]
328 ]