]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
Natbib authoryear uses (Ref1; Ref2) by default.
[lyx.git] / lib / scripts / prefs2prefs_prefs.py
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3
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.
7
8 # author Richard Heck
9
10 # Full author contact details are available in file CREDITS
11
12 # This file houses conversion information for the preferences file.
13
14 # The converter functions take a line as argument and return a list: 
15 #       (Bool, NewLine), 
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.
18
19 # Incremented to format 2, r39670 by jrioux
20 #   Support for multiple file extensions per format.
21 #   No conversion necessary.
22
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.
27
28 # Incremented to format 4, r40028 by vfr
29 #   Remove support for default paper size.
30
31 # Incremented to format 5, r40030 by vfr
32 #   Add a default length unit.
33 #   No conversion necessary.
34
35 # Incremented to format 6, r40515 by younes
36 #   Add use_qimage option.
37 #   No conversion necessary.
38
39 # Incremented to format 7, r40789 by gb
40 #   Add mime type to file format
41
42 # Incremented to format 8, 288c1e0f by rgh
43 #   Add "nice" flag for converters
44 #   No conversion necessary.
45
46 # Incremented to format 9, a18af620 by spitz
47 #  Remove default_language rc.
48
49 # Incremented to format 10, 4985015 by tommaso
50 #  Add close_buffer_with_last_view in preferences.
51 #  No conversion necessary.
52
53 # Incremented to format 11, by gb
54 #   Split pdf format into pdf and pdf6
55
56 import re
57
58 ###########################################################
59 #
60 # Conversion chain
61
62 def get_format(line):
63         entries = []
64         i = 0
65         while i < len(line):
66                 if line[i] == '"':
67                         beg = i + 1
68                         i = i + 1
69                         while i < len(line) and line[i] != '"':
70                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
71                                         # convert \" to "
72                                         i = i + 1
73                                 i = i + 1
74                         end = i
75                         entries.append(line[beg:end].replace('\\"', '"'))
76                 elif line[i] == '#':
77                         return entries
78                 elif not line[i].isspace():
79                         beg = i
80                         while i < len(line) and not line[i].isspace():
81                                 i = i + 1
82                         end = i
83                         entries.append(line[beg:end])
84                 i = i + 1
85         return entries
86
87
88 def simple_renaming(line, old, new):
89         i = line.lower().find(old.lower())
90         if i == -1:
91                 return no_match
92         line = line[:i] + new + line[i+len(old):]
93         return (True, line)
94
95 no_match = (False, [])
96
97 ######################################
98 ### Format 1 conversions (for LyX 2.0)
99
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",
105                                 "\\use_spell_lib")
106         line = line.lower().lstrip()
107         for tag in tags:
108                 if line.lower().startswith(tag):
109                         return (True, "")
110         return no_match
111
112
113 def language_use_babel(line):
114         if not line.lower().startswith("\language_use_babel"):
115                 return no_match
116         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
117         m = re_lub.match(line)
118         val = m.group(1)
119         newval = '0'
120         if val == 'false':
121                 newval = '3'
122         newline = "\\language_package_selection " + newval
123         return (True, newline)
124
125
126 def language_package(line):
127         return simple_renaming(line, "\\language_package", "\\language_custom_package")
128
129
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"):
133                 return no_match
134         m = lfre.match(line)
135         if not m:
136                 return no_match
137         conv = m.group(1)
138         fmat = m.group(2)
139         args = m.group(3)
140         conv2fl = {
141                    "luatex":   "lualatex",
142                    "pplatex":  "latex",
143                    "xetex":    "xelatex",
144                   }
145         if conv in conv2fl.keys():
146                 flavor = conv2fl[conv]
147         else:
148                 flavor = conv
149         if flavor == "latex":
150                 return no_match
151         return (True,
152                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
153
154
155 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
156 def export_menu(line):
157         if not line.lower().startswith("\\format"):
158                 return no_match
159         m = emre.match(line)
160         if not m:
161                 return no_match
162         fmat = m.group(1)
163         opts = m.group(2)
164         return (True,
165                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
166
167 # End format 1 conversions (for LyX 2.0)
168 ########################################
169
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"):
175                 return no_match
176         m = zipre.match(line)
177         if not m:
178                 return no_match
179         fmat = m.group(1)
180         opts = m.group(2)
181         return (True,
182                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
183
184 def remove_default_papersize(line):
185         if not line.lower().startswith("\\default_papersize"):
186                 return no_match
187         return (True, "")
188
189 def add_mime_types(line):
190         if not line.lower().startswith("\\format"):
191                 return no_match
192         entries = get_format(line)
193         converted = line
194         i = len(entries)
195         while i < 7:
196                 converted = converted + '       ""'
197                 i = i + 1
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', \
204                 'gif':'image/gif', \
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', \
232                 'csv':'text/csv', \
233                 'lyx':'application/x-lyx', \
234                 'wmf':'image/x-wmf', \
235                 'emf':'image/x-emf'}
236         if entries[1] in formats.keys():
237                 converted = converted + '       "' + formats[entries[1]] + '"'
238         else:
239                 converted = converted + '       ""'
240         return (True, converted)
241
242 re_converter = re.compile(r'^\\converter\s+', re.IGNORECASE)
243
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':
251                         if len(entries) < 6:
252                                 viewer = ''
253                         else:
254                                 viewer = entries[5]
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]
275                         else:
276                                 converted = entries[0] + ' ' + entries[1] + ' pdf6'
277                         i = 3
278                         while i < len(entries):
279                                 converted = converted + ' "' + entries[i] + '"'
280                                 i = i + 1
281                         return (True, converted)
282         return no_match
283
284 def remove_default_language(line):
285         if not line.lower().startswith("\\default_language"):
286                 return no_match
287         return (True, "")
288
289
290 # End conversions for LyX 2.0 to 2.1
291 ####################################
292
293
294 conversions = [
295         [  1, [ # there were several conversions for format 1
296                 export_menu,
297                 latex_flavor,
298                 remove_obsolete,
299                 language_use_babel,
300                 language_package
301         ]],
302         [ 2, []],
303         [ 3, [ zipped_native ]],
304         [ 4, [ remove_default_papersize ]],
305         [ 5, []],
306         [ 6, []],
307         [ 7, [add_mime_types]],
308         [ 8, []],
309         [ 9, [ remove_default_language ]],
310         [ 10, []],
311         [ 11, [split_pdf_format]]
312 ]