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