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