2 # -*- coding: utf-8 -*-
4 # file prefs2prefs-prefs.py
5 # This file is part of LyX, the document processor.
6 # Licence details can be found in the file COPYING.
10 # Full author contact details are available in file CREDITS
12 # This file houses conversion information for the preferences file.
14 # The converter functions take a line as argument and return a list:
16 # where the Bool says if we've modified anything and the NewLine is
17 # the new line, if so, which will be used to replace the old line.
19 # Incremented to format 2, r39670 by jrioux
20 # Support for multiple file extensions per format.
21 # No conversion necessary.
23 # Incremented to format 3, r39705 by tommaso
24 # Support for file formats that are natively (g)zipped.
25 # We must add the flag zipped=native to formats that
26 # were previously hardcoded in the C++ source: dia.
28 # Incremented to format 4, r40028 by vfr
29 # Remove support for default paper size.
31 # Incremented to format 5, r40030 by vfr
32 # Add a default length unit.
33 # No conversion necessary.
35 # Incremented to format 6, r40515 by younes
36 # Add use_qimage option.
37 # No conversion necessary.
39 # Incremented to format 7, r40789 by gb
40 # Add mime type to file format
42 # Incremented to format 8, 288c1e0f by rgh
43 # Add "nice" flag for converters
44 # No conversion necessary.
46 # Incremented to format 9, a18af620 by spitz
47 # Remove default_language rc.
49 # Incremented to format 10, 4985015 by tommaso
50 # Add close_buffer_with_last_view in preferences.
51 # No conversion necessary.
53 # Incremented to format 11, by gb
54 # Split pdf format into pdf and pdf6
58 ###########################################################
69 while i < len(line) and line[i] != '"':
70 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
75 entries.append(line[beg:end].replace('\\"', '"'))
78 elif not line[i].isspace():
80 while i < len(line) and not line[i].isspace():
83 entries.append(line[beg:end])
88 def simple_renaming(line, old, new):
89 i = line.lower().find(old.lower())
92 line = line[:i] + new + line[i+len(old):]
95 no_match = (False, [])
97 ######################################
98 ### Format 1 conversions (for LyX 2.0)
100 def remove_obsolete(line):
101 tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
102 "\\plaintext_roff_command", "\\use_alt_language",
103 "\\use_escape_chars", "\\use_input_encoding",
104 "\\use_personal_dictionary", "\\use_pspell",
106 line = line.lower().lstrip()
108 if line.lower().startswith(tag):
113 def language_use_babel(line):
114 if not line.lower().startswith("\language_use_babel"):
116 re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
117 m = re_lub.match(line)
122 newline = "\\language_package_selection " + newval
123 return (True, newline)
126 def language_package(line):
127 return simple_renaming(line, "\\language_package", "\\language_custom_package")
130 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
131 def latex_flavor(line):
132 if not line.lower().startswith("\\converter"):
141 "luatex": "lualatex",
145 if conv in conv2fl.keys():
146 flavor = conv2fl[conv]
149 if flavor == "latex":
152 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
155 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
156 def export_menu(line):
157 if not line.lower().startswith("\\format"):
165 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
167 # End format 1 conversions (for LyX 2.0)
168 ########################################
170 #################################
171 # Conversions from LyX 2.0 to 2.1
172 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
173 def zipped_native(line):
174 if not line.lower().startswith("\\format"):
176 m = zipre.match(line)
182 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
184 def remove_default_papersize(line):
185 if not line.lower().startswith("\\default_papersize"):
189 def add_mime_types(line):
190 if not line.lower().startswith("\\format"):
192 entries = get_format(line)
196 converted = converted + ' ""'
198 formats = {'tgif':'application/x-tgif', \
199 'fig':'application/x-xfig', \
200 'dia':'application/x-dia-diagram', \
201 'odg':'application/vnd.oasis.opendocument.graphics', \
202 'svg':'image/svg+xml', \
203 'bmp':'image/x-bmp', \
205 'jpg':'image/jpeg', \
206 'pbm':'image/x-portable-bitmap', \
207 'pgm':'image/x-portable-graymap', \
208 'png':'image/x-png', \
209 'ppm':'image/x-portable-pixmap', \
210 'tiff':'image/tiff', \
211 'xbm':'image/x-xbitmap', \
212 'xpm':'image/x-xpixmap', \
213 'docbook-xml':'application/docbook+xml', \
214 'dot':'text/vnd.graphviz', \
215 'ly':'text/x-lilypond', \
216 'latex':'text/x-tex', \
217 'text':'text/plain', \
218 'gnumeric':'application/x-gnumeric', \
219 'excel':'application/vnd.ms-excel', \
220 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
221 'xhtml':'application/xhtml+xml', \
222 'bib':'text/x-bibtex', \
223 'eps':'image/x-eps', \
224 'ps':'application/postscript', \
225 'pdf':'application/pdf', \
226 'dvi':'application/x-dvi', \
227 'html':'text/html', \
228 'odt':'application/vnd.oasis.opendocument.text', \
229 'sxw':'application/vnd.sun.xml.writer', \
230 'rtf':'application/rtf', \
231 'doc':'application/msword', \
233 'lyx':'application/x-lyx', \
234 'wmf':'image/x-wmf', \
236 if entries[1] in formats.keys():
237 converted = converted + ' "' + formats[entries[1]] + '"'
239 converted = converted + ' ""'
240 return (True, converted)
242 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
244 def split_pdf_format(line):
245 # strictly speaking, a new format would not require to bump the
246 # version number, but the old pdf format was hardcoded at several
247 # places in the C++ code, so an update seemed like a good idea.
248 if line.lower().startswith("\\format"):
249 entries = get_format(line)
250 if entries[1] == 'pdf':
255 converted = line.replace('application/pdf', '') + '''
256 \Format pdf6 pdf "PDF (graphics)" "" "''' + viewer + '" "" "vector" "application/pdf"'
257 return (True, converted)
258 elif line.lower().startswith("\\viewer_alternatives") or \
259 line.lower().startswith("\\editor_alternatives"):
260 entries = get_format(line)
261 if entries[1] == 'pdf':
262 converted = line + "\n" + entries[0] + ' pdf6 "' + entries[2] + '"'
263 return (True, converted)
264 elif re_converter.match(line):
265 entries = get_format(line)
266 # The only converter from pdf that is touched is pdf->eps:
267 # All other converters are likely meant for further processing on export.
268 # The only converter to pdf that stays untouched is dvi->pdf:
269 # All other converters are likely meant for graphics.
270 if len(entries) > 2 and \
271 ((entries[1] == 'pdf' and entries[2] == 'eps') or \
272 (entries[1] != 'ps' and entries[2] == 'pdf')):
273 if entries[1] == 'pdf':
274 converted = entries[0] + ' pdf6 ' + entries[2]
276 converted = entries[0] + ' ' + entries[1] + ' pdf6'
278 while i < len(entries):
279 converted = converted + ' "' + entries[i] + '"'
281 return (True, converted)
284 def remove_default_language(line):
285 if not line.lower().startswith("\\default_language"):
290 # End conversions for LyX 2.0 to 2.1
291 ####################################
295 [ 1, [ # there were several conversions for format 1
303 [ 3, [ zipped_native ]],
304 [ 4, [ remove_default_papersize ]],
307 [ 7, [add_mime_types]],
309 [ 9, [ remove_default_language ]],
311 [ 11, [split_pdf_format]]