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