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