]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
Additional.lyx: update eLyXer URL
[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 # Incremented to format 15, by prannoy
67 #   Add fullscreen_statusbar
68 #   No conversion necessary.
69
70 # Incremented to format 16, by lasgouttes
71 #  Remove force_paint_single_char rc.
72
73 # Incremented to format 17, by lasgouttes
74 #  Remove rtl_support rc.
75
76 # Incremented to format 18, by ef
77 #   Add option to allow saving the document directory
78 #   No conversion necessary.
79
80 # Incremented to format 19, by rgh
81 #   remove print support
82
83 # Incremented to format 20, by tommaso
84 #   Add options to forbid/ignore 'needauth' option
85 #   No conversion necessary.
86
87 # Incremented to format 21, by spitz
88 #   Add jbibtex_alternatives, allow "automatic" value
89 #   of bibtex_command and jbibtex_command (actually the
90 #   default now)
91 #   No conversion necessary.
92
93 # Incremented to format 22, by ef
94 #   Add pygmentize_command for the python pygments syntax highlighter
95 #   No conversion necessary.
96
97 # Incremented to format 23, by spitz
98 #   Add default_platex_view_format, a default output format for
99 #   Japanese documents via pLaTeX.
100 #   No conversion necessary.
101
102 # NOTE: The format should also be updated in LYXRC.cpp and
103 # in configure.py.
104
105 import re
106
107 ###########################################################
108 #
109 # Conversion chain
110
111 def get_format(line):
112         entries = []
113         i = 0
114         while i < len(line):
115                 if line[i] == '"':
116                         beg = i + 1
117                         i = i + 1
118                         while i < len(line) and line[i] != '"':
119                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
120                                         # convert \" to "
121                                         i = i + 1
122                                 i = i + 1
123                         end = i
124                         entries.append(line[beg:end].replace('\\"', '"'))
125                 elif line[i] == '#':
126                         return entries
127                 elif not line[i].isspace():
128                         beg = i
129                         while i < len(line) and not line[i].isspace():
130                                 i = i + 1
131                         end = i
132                         entries.append(line[beg:end])
133                 i = i + 1
134         return entries
135
136
137 def simple_renaming(line, old, new):
138         i = line.lower().find(old.lower())
139         if i == -1:
140                 return no_match
141         line = line[:i] + new + line[i+len(old):]
142         return (True, line)
143
144 no_match = (False, [])
145
146 ######################################
147 ### Format 1 conversions (for LyX 2.0)
148
149 def remove_obsolete(line):
150         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
151                 "\\plaintext_roff_command", "\\use_alt_language",
152                 "\\use_escape_chars", "\\use_input_encoding",
153                 "\\use_personal_dictionary", "\\use_pspell",
154                 "\\use_spell_lib")
155         line = line.lower().lstrip()
156         for tag in tags:
157                 if line.lower().startswith(tag):
158                         return (True, "")
159         return no_match
160
161
162 def language_use_babel(line):
163         if not line.lower().startswith("\language_use_babel"):
164                 return no_match
165         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
166         m = re_lub.match(line)
167         val = m.group(1)
168         newval = '0'
169         if val == 'false':
170                 newval = '3'
171         newline = "\\language_package_selection " + newval
172         return (True, newline)
173
174
175 def language_package(line):
176         return simple_renaming(line, "\\language_package", "\\language_custom_package")
177
178
179 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
180 def latex_flavor(line):
181         if not line.lower().startswith("\\converter"):
182                 return no_match
183         m = lfre.match(line)
184         if not m:
185                 return no_match
186         conv = m.group(1)
187         fmat = m.group(2)
188         args = m.group(3)
189         conv2fl = {
190                    "luatex":   "lualatex",
191                    "pplatex":  "latex",
192                    "xetex":    "xelatex",
193                   }
194         if conv in conv2fl.keys():
195                 flavor = conv2fl[conv]
196         else:
197                 flavor = conv
198         if flavor == "latex":
199                 return no_match
200         return (True,
201                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
202
203
204 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
205 def export_menu(line):
206         if not line.lower().startswith("\\format"):
207                 return no_match
208         m = emre.match(line)
209         if not m:
210                 return no_match
211         fmat = m.group(1)
212         opts = m.group(2)
213         return (True,
214                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
215
216 # End format 1 conversions (for LyX 2.0)
217 ########################################
218
219 #################################
220 # Conversions from LyX 2.0 to 2.1
221 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
222 def zipped_native(line):
223         if not line.lower().startswith("\\format"):
224                 return no_match
225         m = zipre.match(line)
226         if not m:
227                 return no_match
228         fmat = m.group(1)
229         opts = m.group(2)
230         return (True,
231                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
232
233 def remove_default_papersize(line):
234         if not line.lower().startswith("\\default_papersize"):
235                 return no_match
236         return (True, "")
237
238 def add_mime_types(line):
239         if not line.lower().startswith("\\format"):
240                 return no_match
241         entries = get_format(line)
242         converted = line
243         i = len(entries)
244         while i < 7:
245                 converted = converted + '       ""'
246                 i = i + 1
247         formats = {'tgif':'application/x-tgif', \
248                 'fig':'application/x-xfig', \
249                 'dia':'application/x-dia-diagram', \
250                 'odg':'application/vnd.oasis.opendocument.graphics', \
251                 'svg':'image/svg+xml', \
252                 'bmp':'image/x-bmp', \
253                 'gif':'image/gif', \
254                 'jpg':'image/jpeg', \
255                 'pbm':'image/x-portable-bitmap', \
256                 'pgm':'image/x-portable-graymap', \
257                 'png':'image/x-png', \
258                 'ppm':'image/x-portable-pixmap', \
259                 'tiff':'image/tiff', \
260                 'xbm':'image/x-xbitmap', \
261                 'xpm':'image/x-xpixmap', \
262                 'docbook-xml':'application/docbook+xml', \
263                 'dot':'text/vnd.graphviz', \
264                 'ly':'text/x-lilypond', \
265                 'latex':'text/x-tex', \
266                 'text':'text/plain', \
267                 'gnumeric':'application/x-gnumeric', \
268                 'excel':'application/vnd.ms-excel', \
269                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
270                 'xhtml':'application/xhtml+xml', \
271                 'bib':'text/x-bibtex', \
272                 'eps':'image/x-eps', \
273                 'ps':'application/postscript', \
274                 'pdf':'application/pdf', \
275                 'dvi':'application/x-dvi', \
276                 'html':'text/html', \
277                 'odt':'application/vnd.oasis.opendocument.text', \
278                 'sxw':'application/vnd.sun.xml.writer', \
279                 'rtf':'application/rtf', \
280                 'doc':'application/msword', \
281                 'csv':'text/csv', \
282                 'lyx':'application/x-lyx', \
283                 'wmf':'image/x-wmf', \
284                 'emf':'image/x-emf'}
285         if entries[1] in formats.keys():
286                 converted = converted + '       "' + formats[entries[1]] + '"'
287         else:
288                 converted = converted + '       ""'
289         return (True, converted)
290
291 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
292
293 def split_pdf_format(line):
294         # strictly speaking, a new format would not require to bump the
295         # version number, but the old pdf format was hardcoded at several
296         # places in the C++ code, so an update seemed like a good idea.
297         if line.lower().startswith("\\format"):
298                 entries = get_format(line)
299                 if entries[1] == 'pdf':
300                         if len(entries) < 6:
301                                 viewer = ''
302                         else:
303                                 viewer = entries[5]
304                         converted = line.replace('application/pdf', '') + '''
305 \Format pdf6       pdf    "PDF (graphics)"        "" "''' + viewer + '" ""      "vector"        "application/pdf"'
306                         return (True, converted)
307         elif line.lower().startswith("\\viewer_alternatives") or \
308              line.lower().startswith("\\editor_alternatives"):
309                 entries = get_format(line)
310                 if entries[1] == 'pdf':
311                         converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
312                         return (True, converted)
313         elif re_converter.match(line):
314                 entries = get_format(line)
315                 # The only converter from pdf that is touched is pdf->eps:
316                 # All other converters are likely meant for further processing on export.
317                 # The only converter to pdf that stays untouched is dvi->pdf:
318                 # All other converters are likely meant for graphics.
319                 if len(entries) > 2 and \
320                    ((entries[1] == 'pdf' and entries[2] == 'eps') or \
321                    (entries[1] != 'ps'  and entries[2] == 'pdf')):
322                         if entries[1] == 'pdf':
323                                 converted = entries[0] + ' pdf6 ' + entries[2]
324                         else:
325                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
326                         i = 3
327                         while i < len(entries):
328                                 converted = converted + ' "' + entries[i] + '"'
329                                 i = i + 1
330                         return (True, converted)
331         return no_match
332
333 def remove_default_language(line):
334         if not line.lower().startswith("\\default_language"):
335                 return no_match
336         return (True, "")
337
338 def mac_cursor_movement(line):
339         return simple_renaming(line, "\\mac_like_word_movement", "\\mac_like_cursor_movement")
340
341 # End conversions for LyX 2.0 to 2.1
342 ####################################
343
344
345 #################################
346 # Conversions from LyX 2.1 to 2.2
347
348 def remove_force_paint_single_char(line):
349         if not line.lower().startswith("\\force_paint_single_char"):
350                 return no_match
351         return (True, "")
352
353 def remove_rtl(line):
354         if not line.lower().startswith("\\rtl "):
355                 return no_match
356         return (True, "")
357
358 def remove_print_support(line):
359         tags = ("\\printer", "\\print_adapt_output", "\\print_command",
360                 "\\print_evenpage_flag", "\\print_oddpage_flag", "\\print_pagerange_flag",
361                 "\\print_copies_flag", "\\print_collcopies_flag", "\\print_reverse_flag",
362                 "\\print_to_printer", "\\print_to_file", "\\print_file_extension")
363         line = line.lower().lstrip()
364         for tag in tags:
365                 if line.lower().startswith(tag):
366                         return (True, "")
367         return no_match
368
369 # End conversions for LyX 2.1 to 2.2
370 ####################################
371
372 #################################
373 # Conversions from LyX 2.2 to 2.3
374
375 # Only format changes that don't require conversion
376
377 # End conversions for LyX 2.2 to 2.3
378 ####################################
379
380
381 ############################################################
382 # Format-conversion map. Also add empty format changes here.
383
384 conversions = [
385         [  1, [ # there were several conversions for format 1
386                 export_menu,
387                 latex_flavor,
388                 remove_obsolete,
389                 language_use_babel,
390                 language_package
391         ]],
392         [ 2, []],
393         [ 3, [ zipped_native ]],
394         [ 4, [ remove_default_papersize ]],
395         [ 5, []],
396         [ 6, []],
397         [ 7, [add_mime_types]],
398         [ 8, []],
399         [ 9, [ remove_default_language ]],
400         [ 10, []],
401         [ 11, [split_pdf_format]],
402         [ 12, []],
403         [ 13, [mac_cursor_movement]],
404         [ 14, []],
405         [ 15, []],
406         [ 16, [remove_force_paint_single_char]],
407         [ 17, [remove_rtl]],
408         [ 18, []],
409         [ 19, [remove_print_support]],
410         [ 20, []],
411         [ 21, []],
412         [ 22, []],
413         [ 23, []]
414 ]