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