]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
a7216e03bd28d6b6d65fc22587d1b5e5dcddaf78
[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 import re
40
41
42 ###########################################################
43 #
44 # Conversion chain
45
46 def simple_renaming(line, old, new):
47         i = line.lower().find(old.lower())
48         if i == -1:
49                 return no_match
50         line = line[:i] + new + line[i+len(old):]
51         return (True, line)
52
53 no_match = (False, [])
54
55 ########################
56 ### Format 1 conversions
57
58 def remove_obsolete(line):
59         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
60                                 "\\plaintext_roff_command", "\\use_alt_language", 
61                                 "\\use_escape_chars", "\\use_input_encoding",
62                                 "\\use_personal_dictionary", "\\use_pspell",
63                                 "\\use_spell_lib")
64         line = line.lower().lstrip()
65         for tag in tags:
66                 if line.lower().startswith(tag):
67                         return (True, "")
68         return no_match
69
70 def language_use_babel(line):
71         if not line.lower().startswith("\language_use_babel"):
72                 return no_match
73         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)', re.IGNORECASE)
74         m = re_lub.match(line)
75         val = m.group(1)
76         newval = '0'
77         if val == 'false':
78                 newval = '3'
79         newline = "\\language_package_selection " + newval
80         return (True, newline)
81
82 def language_package(line):
83         return simple_renaming(line, "\\language_package", "\\language_custom_package")
84
85 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"', re.IGNORECASE)
86 def latex_flavor(line):
87         if not line.lower().startswith("\\converter"):
88                 return no_match
89         m = lfre.match(line)
90         if not m:
91                 return no_match
92         conv = m.group(1)
93         fmat = m.group(2)
94         args = m.group(3)
95         conv2fl = {
96                    "luatex":   "lualatex",
97                    "pplatex":  "latex",
98                    "xetex":    "xelatex",
99                   }
100         if conv in conv2fl.keys():
101                 flavor = conv2fl[conv]
102         else:
103                 flavor = conv
104         if flavor == "latex":
105                 return no_match
106         return (True,
107                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
108
109 emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE)
110 def export_menu(line):
111         if not line.lower().startswith("\\format"):
112                 return no_match
113         m = emre.match(line)
114         if not m:
115                 return no_match
116         fmat = m.group(1)
117         opts = m.group(2)
118         return (True,
119                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
120
121 zipre = re.compile(r'^\\format\s+("?dia"?\s+.*)\s+"([^"]*?)"', re.IGNORECASE)
122 def zipped_native(line):
123         if not line.lower().startswith("\\format"):
124                 return no_match
125         m = zipre.match(line)
126         if not m:
127                 return no_match
128         fmat = m.group(1)
129         opts = m.group(2)
130         return (True,
131                 "\\Format %s \"%s,zipped=native\"" % (fmat, opts))
132
133 def remove_default_papersize(line):
134         if not line.lower().startswith("\\default_papersize"):
135                 return no_match
136         return (True, "")
137
138
139 ########################
140
141
142 conversions = [
143         [  1, [ # this will be a long list of conversions to format 1
144                 export_menu,
145                 latex_flavor,
146                 remove_obsolete,
147                 language_use_babel,
148                 language_package
149         ]],
150         [  2, []],
151         [  3, [ zipped_native ]],
152         [  4, [ remove_default_papersize ]],
153         [  5, []],
154         [  6, []],
155 ]