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