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