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