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