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