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