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