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