]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
8e16899de64f1e6e2188ddf53db02193bccd3904
[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, by spitz
47 #  Remove default_language rc.
48
49 import re
50
51 ###########################################################
52 #
53 # Conversion chain
54
55 def get_format(line):
56         entries = []
57         i = 0
58         while i < len(line):
59                 if line[i] == '"':
60                         beg = i + 1
61                         i = i + 1
62                         while i < len(line) and line[i] != '"':
63                                 if line[i] == '\\' and i < len(line) - 1 and line[i+1] == '"':
64                                         # convert \" to "
65                                         i = i + 1
66                                 i = i + 1
67                         end = i
68                         entries.append(line[beg:end].replace('\\"', '"'))
69                 elif line[i] == '#':
70                         return entries
71                 elif not line[i].isspace():
72                         beg = i
73                         while not line[i].isspace():
74                                 i = i + 1
75                         end = i
76                         entries.append(line[beg:end])
77                 i = i + 1
78         return entries
79
80
81 def simple_renaming(line, old, new):
82         i = line.lower().find(old.lower())
83         if i == -1:
84                 return no_match
85         line = line[:i] + new + line[i+len(old):]
86         return (True, line)
87
88 no_match = (False, [])
89
90 ######################################
91 ### Format 1 conversions (for LyX 2.0)
92
93 def remove_obsolete(line):
94         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
95                                 "\\plaintext_roff_command", "\\use_alt_language", 
96                                 "\\use_escape_chars", "\\use_input_encoding",
97                                 "\\use_personal_dictionary", "\\use_pspell",
98                                 "\\use_spell_lib")
99         line = line.lower().lstrip()
100         for tag in tags:
101                 if line.lower().startswith(tag):
102                         return (True, "")
103         return no_match
104
105
106 def language_use_babel(line):
107         if not line.lower().startswith("\language_use_babel"):
108                 return no_match
109         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
110         m = re_lub.match(line)
111         val = m.group(1)
112         newval = '0'
113         if val == 'false':
114                 newval = '3'
115         newline = "\\language_package_selection " + newval
116         return (True, newline)
117
118
119 def language_package(line):
120         return simple_renaming(line, "\\language_package", "\\language_custom_package")
121
122
123 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
124 def latex_flavor(line):
125         if not line.lower().startswith("\\converter"):
126                 return no_match
127         m = lfre.match(line)
128         if not m:
129                 return no_match
130         conv = m.group(1)
131         fmat = m.group(2)
132         args = m.group(3)
133         conv2fl = {
134                    "luatex":   "lualatex",
135                    "pplatex":  "latex",
136                    "xetex":    "xelatex",
137                   }
138         if conv in conv2fl.keys():
139                 flavor = conv2fl[conv]
140         else:
141                 flavor = conv
142         if flavor == "latex":
143                 return no_match
144         return (True,
145                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
146
147
148 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
149 def export_menu(line):
150         if not line.lower().startswith("\\format"):
151                 return no_match
152         m = emre.match(line)
153         if not m:
154                 return no_match
155         fmat = m.group(1)
156         opts = m.group(2)
157         return (True,
158                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
159
160 # End format 1 conversions (for LyX 2.0)
161 ########################################
162
163 #################################
164 # Conversions from LyX 2.0 to 2.1
165 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
166 def zipped_native(line):
167         if not line.lower().startswith("\\format"):
168                 return no_match
169         m = zipre.match(line)
170         if not m:
171                 return no_match
172         fmat = m.group(1)
173         opts = m.group(2)
174         return (True,
175                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
176
177 def remove_default_papersize(line):
178         if not line.lower().startswith("\\default_papersize"):
179                 return no_match
180         return (True, "")
181
182 def add_mime_types(line):
183         if not line.lower().startswith("\\format"):
184                 return no_match
185         entries = get_format(line)
186         converted = line
187         i = len(entries)
188         while i < 7:
189                 converted = converted + '       ""'
190                 i = i + 1
191         formats = {'tgif':'application/x-tgif', \
192                 'fig':'application/x-xfig', \
193                 'dia':'application/x-dia-diagram', \
194                 'odg':'application/vnd.oasis.opendocument.graphics', \
195                 'svg':'image/svg+xml', \
196                 'bmp':'image/x-bmp', \
197                 'gif':'image/gif', \
198                 'jpg':'image/jpeg', \
199                 'pbm':'image/x-portable-bitmap', \
200                 'pgm':'image/x-portable-graymap', \
201                 'png':'image/x-png', \
202                 'ppm':'image/x-portable-pixmap', \
203                 'tiff':'image/tiff', \
204                 'xbm':'image/x-xbitmap', \
205                 'xpm':'image/x-xpixmap', \
206                 'docbook-xml':'application/docbook+xml', \
207                 'dot':'text/vnd.graphviz', \
208                 'ly':'text/x-lilypond', \
209                 'latex':'text/x-tex', \
210                 'text':'text/plain', \
211                 'gnumeric':'application/x-gnumeric', \
212                 'excel':'application/vnd.ms-excel', \
213                 'oocalc':'application/vnd.oasis.opendocument.spreadsheet', \
214                 'xhtml':'application/xhtml+xml', \
215                 'bib':'text/x-bibtex', \
216                 'eps':'image/x-eps', \
217                 'ps':'application/postscript', \
218                 'pdf':'application/pdf', \
219                 'dvi':'application/x-dvi', \
220                 'html':'text/html', \
221                 'odt':'application/vnd.oasis.opendocument.text', \
222                 'sxw':'application/vnd.sun.xml.writer', \
223                 'rtf':'application/rtf', \
224                 'doc':'application/msword', \
225                 'csv':'text/csv', \
226                 'lyx':'application/x-lyx', \
227                 'wmf':'image/x-wmf', \
228                 'emf':'image/x-emf'}
229         if entries[1] in formats.keys():
230                 converted = converted + '       "' + formats[entries[1]] + '"'
231         else:
232                 converted = converted + '       ""'
233         return (True, converted)
234
235 def remove_default_language(line):
236         if not line.lower().startswith("\\default_language"):
237                 return no_match
238         return (True, "")
239
240
241 # End conversions for LyX 2.0 to 2.1
242 ####################################
243
244
245 conversions = [
246         [  1, [ # there were several conversions for format 1
247                 export_menu,
248                 latex_flavor,
249                 remove_obsolete,
250                 language_use_babel,
251                 language_package
252         ]],
253         [ 2, []],
254         [ 3, [ zipped_native ]],
255         [ 4, [ remove_default_papersize ]],
256         [ 5, []],
257         [ 6, []],
258         [ 7, [add_mime_types]],
259         [ 8, []],
260         [ 9, [ remove_default_language ]]
261 ]