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