]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_5.py
Call python with the -tt switch to make mixed tab/space indentation an error
[lyx.git] / lib / lyx2lyx / lyx_1_5.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2006 José Matos <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 from parser_tools import find_token_exact, find_tokens, get_value
20
21 ##
22 #  Notes: Framed/Shaded
23 #
24
25 def revert_framed(file):
26     i = 0
27     while 1:
28         i = find_tokens(file.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
29
30         if i == -1:
31             return
32         file.body[i] = "\\begin_inset Note"
33         i = i + 1
34
35
36 ##
37 #  Fonts
38 #
39
40 roman_fonts      = {'default' : 'default', 'ae'       : 'ae',
41                     'times'   : 'times',   'palatino' : 'palatino',
42                     'helvet'  : 'default', 'avant'    : 'default',
43                     'newcent' : 'newcent', 'bookman'  : 'bookman',
44                     'pslatex' : 'times'}
45 sans_fonts       = {'default' : 'default', 'ae'       : 'default',
46                     'times'   : 'default', 'palatino' : 'default',
47                     'helvet'  : 'helvet',  'avant'    : 'avant',
48                     'newcent' : 'default', 'bookman'  : 'default',
49                     'pslatex' : 'helvet'}
50 typewriter_fonts = {'default' : 'default', 'ae'       : 'default',
51                     'times'   : 'default', 'palatino' : 'default',
52                     'helvet'  : 'default', 'avant'    : 'default',
53                     'newcent' : 'default', 'bookman'  : 'default',
54                     'pslatex' : 'courier'}
55
56 def convert_font_settings(file):
57     i = 0
58     i = find_token_exact(file.header, "\\fontscheme", i)
59     if i == -1:
60         file.warning("Malformed LyX file: Missing `\\fontscheme'.")
61         return
62     font_scheme = get_value(file.header, "\\fontscheme", i, i + 1)
63     if font_scheme == '':
64         file.warning("Malformed LyX file: Empty `\\fontscheme'.")
65         font_scheme = 'default'
66     if not font_scheme in roman_fonts.keys():
67         file.warning("Malformed LyX file: Unknown `\\fontscheme' `%s'." % font_scheme)
68         font_scheme = 'default'
69     file.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
70                           '\\font_sans %s' % sans_fonts[font_scheme],
71                           '\\font_typewriter %s' % typewriter_fonts[font_scheme],
72                           '\\font_default_family default',
73                           '\\font_sc false',
74                           '\\font_osf false',
75                           '\\font_sf_scale 100',
76                           '\\font_tt_scale 100']
77
78
79 def revert_font_settings(file):
80     i = 0
81     insert_line = -1
82     fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
83     for family in 'roman', 'sans', 'typewriter':
84         name = '\\font_%s' % family
85         i = find_token_exact(file.header, name, i)
86         if i == -1:
87             file.warning("Malformed LyX file: Missing `%s'." % name)
88             i = 0
89         else:
90             if (insert_line < 0):
91                 insert_line = i
92             fonts[family] = get_value(file.header, name, i, i + 1)
93             del file.header[i]
94     i = find_token_exact(file.header, '\\font_default_family', i)
95     if i == -1:
96         file.warning("Malformed LyX file: Missing `\\font_default_family'.")
97         font_default_family = 'default'
98     else:
99         font_default_family = get_value(file.header, "\\font_default_family", i, i + 1)
100         del file.header[i]
101     i = find_token_exact(file.header, '\\font_sc', i)
102     if i == -1:
103         file.warning("Malformed LyX file: Missing `\\font_sc'.")
104         font_sc = 'false'
105     else:
106         font_sc = get_value(file.header, '\\font_sc', i, i + 1)
107         del file.header[i]
108     if font_sc != 'false':
109         file.warning("Conversion of '\\font_sc' not yet implemented.")
110     i = find_token_exact(file.header, '\\font_osf', i)
111     if i == -1:
112         file.warning("Malformed LyX file: Missing `\\font_osf'.")
113         font_osf = 'false'
114     else:
115         font_osf = get_value(file.header, '\\font_osf', i, i + 1)
116         del file.header[i]
117     i = find_token_exact(file.header, '\\font_sf_scale', i)
118     if i == -1:
119         file.warning("Malformed LyX file: Missing `\\font_sf_scale'.")
120         font_sf_scale = '100'
121     else:
122         font_sf_scale = get_value(file.header, '\\font_sf_scale', i, i + 1)
123         del file.header[i]
124     if font_sf_scale != '100':
125         file.warning("Conversion of '\\font_sf_scale' not yet implemented.")
126     i = find_token_exact(file.header, '\\font_tt_scale', i)
127     if i == -1:
128         file.warning("Malformed LyX file: Missing `\\font_tt_scale'.")
129         font_tt_scale = '100'
130     else:
131         font_tt_scale = get_value(file.header, '\\font_tt_scale', i, i + 1)
132         del file.header[i]
133     if font_tt_scale != '100':
134         file.warning("Conversion of '\\font_tt_scale' not yet implemented.")
135     for font_scheme in roman_fonts.keys():
136         if (roman_fonts[font_scheme] == fonts['roman'] and
137             sans_fonts[font_scheme] == fonts['sans'] and
138             typewriter_fonts[font_scheme] == fonts['typewriter']):
139             file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
140             if font_default_family != 'default':
141                 file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
142             if font_osf == 'true':
143                 file.warning("Ignoring `\\font_osf = true'")
144             return
145     font_scheme = 'default'
146     file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
147     if fonts['roman'] == 'cmr':
148         file.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
149         if font_osf == 'true':
150             file.preamble.append('\\usepackage{eco}')
151             font_osf = 'false'
152     for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
153         if fonts['roman'] == font:
154             file.preamble.append('\\usepackage{%s}' % font)
155     for font in 'cmss', 'lmss', 'cmbr':
156         if fonts['sans'] == font:
157             file.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
158     for font in 'berasans':
159         if fonts['sans'] == font:
160             file.preamble.append('\\usepackage{%s}' % font)
161     for font in 'cmtt', 'lmtt', 'cmtl':
162         if fonts['typewriter'] == font:
163             file.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
164     for font in 'courier', 'beramono', 'luximono':
165         if fonts['typewriter'] == font:
166             file.preamble.append('\\usepackage{%s}' % font)
167     if font_default_family != 'default':
168         file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
169     if font_osf == 'true':
170         file.warning("Ignoring `\\font_osf = true'")
171
172
173 ##
174 # Conversion hub
175 #
176
177 convert = [[246, []],
178            [247, [convert_font_settings]]]
179
180 revert  = [[246, [revert_font_settings]],
181            [245, [revert_framed]]]
182
183 if __name__ == "__main__":
184     pass
185