]> git.lyx.org Git - lyx.git/blob - lib/scripts/prefs2prefs_prefs.py
7a2cdd69bcf0156511148e5e7008e69db21bec9f
[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 import re
20
21
22 ###########################################################
23 #
24 # Conversion chain
25
26 def simple_renaming(line, old, new):
27         if line.find(old) == -1:
28                 return no_match
29         line = line.replace(old, new)
30         return (True, line)
31
32 no_match = (False, [])
33
34 ########################
35 ### Format 1 conversions
36
37 def remove_obsolete(line):
38         tags = ("\\use_tempdir", "\\spell_command", "\\personal_dictionary",
39                                 "\\plaintext_roff_command", "\\use_alt_language", 
40                                 "\\use_escape_chars", "\\use_input_encoding",
41                                 "\\use_personal_dictionary", "\\use_pspell",
42                                 "\\use_spell_lib")
43         line = line.lstrip()
44         for tag in tags:
45                 if line.startswith(tag):
46                         return (True, "")
47         return no_match
48
49 def language_use_babel(line):
50         if not line.startswith("\language_use_babel"):
51                 return no_match
52         re_lub = re.compile(r'^\\language_use_babel\s+"?(true|false)')
53         m = re_lub.match(line)
54         val = m.group(1)
55         newval = '0'
56         if val == 'false':
57                 newval = '3'
58         newline = "\\language_package_selection " + newval
59         return (True, newline)
60
61 def language_package(line):
62         return simple_renaming(line, "\\language_package", "\\language_custom_package")
63
64 lfre = re.compile(r'^\\converter\s+"?(\w+)"?\s+"?(\w+)"?\s+"([^"]*?)"\s+"latex"')
65 def latex_flavor(line):
66         if not line.startswith("\\converter"):
67                 return no_match
68         m = lfre.match(line)
69         if not m:
70                 return no_match
71         conv = m.group(1)
72         fmat = m.group(2)
73         args = m.group(3)
74         conv2fl = {
75                    "luatex":   "lualatex",
76                    "pplatex":  "latex",
77                    "xetex":    "xelatex",
78                   }
79         if conv in conv2fl.keys():
80                 flavor = conv2fl[conv]
81         else:
82                 flavor = conv
83         if flavor == "latex":
84                 return no_match
85         return (True,
86                 "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor))
87
88 emre = re.compile(r'^\\[Ff]ormat\s+(.*)\s+"(document[^"]*?)"')
89 def export_menu(line):
90         if not line.lower().startswith("\\format"):
91                 return no_match
92         m = emre.match(line)
93         if not m:
94                 return no_match
95         fmat = m.group(1)
96         opts = m.group(2)
97         return (True,
98                 "\\Format %s \"%s,menu=export\"" % (fmat, opts))
99
100 ########################
101
102
103 conversions = [
104         [ # this will be a long list of conversions for format 0
105                 export_menu,
106                 latex_flavor,
107                 remove_obsolete,
108                 language_use_babel,
109                 language_package
110         ] # end conversions for format 0
111 ]