]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_5.py
merge booktabs branch
[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 # Copyright (C) 2004-2006 Georg Baum <Georg.Baum@post.rwth-aachen.de>
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 import re
21 from parser_tools import find_token, find_token_exact, find_tokens, find_end_of_inset, get_value
22 from string import replace
23
24
25 ##
26 #  Notes: Framed/Shaded
27 #
28
29 def revert_framed(file):
30     i = 0
31     while 1:
32         i = find_tokens(file.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
33
34         if i == -1:
35             return
36         file.body[i] = "\\begin_inset Note"
37         i = i + 1
38
39
40 ##
41 #  Fonts
42 #
43
44 roman_fonts      = {'default' : 'default', 'ae'       : 'ae',
45                     'times'   : 'times',   'palatino' : 'palatino',
46                     'helvet'  : 'default', 'avant'    : 'default',
47                     'newcent' : 'newcent', 'bookman'  : 'bookman',
48                     'pslatex' : 'times'}
49 sans_fonts       = {'default' : 'default', 'ae'       : 'default',
50                     'times'   : 'default', 'palatino' : 'default',
51                     'helvet'  : 'helvet',  'avant'    : 'avant',
52                     'newcent' : 'default', 'bookman'  : 'default',
53                     'pslatex' : 'helvet'}
54 typewriter_fonts = {'default' : 'default', 'ae'       : 'default',
55                     'times'   : 'default', 'palatino' : 'default',
56                     'helvet'  : 'default', 'avant'    : 'default',
57                     'newcent' : 'default', 'bookman'  : 'default',
58                     'pslatex' : 'courier'}
59
60 def convert_font_settings(file):
61     i = 0
62     i = find_token_exact(file.header, "\\fontscheme", i)
63     if i == -1:
64         file.warning("Malformed LyX file: Missing `\\fontscheme'.")
65         return
66     font_scheme = get_value(file.header, "\\fontscheme", i, i + 1)
67     if font_scheme == '':
68         file.warning("Malformed LyX file: Empty `\\fontscheme'.")
69         font_scheme = 'default'
70     if not font_scheme in roman_fonts.keys():
71         file.warning("Malformed LyX file: Unknown `\\fontscheme' `%s'." % font_scheme)
72         font_scheme = 'default'
73     file.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
74                           '\\font_sans %s' % sans_fonts[font_scheme],
75                           '\\font_typewriter %s' % typewriter_fonts[font_scheme],
76                           '\\font_default_family default',
77                           '\\font_sc false',
78                           '\\font_osf false',
79                           '\\font_sf_scale 100',
80                           '\\font_tt_scale 100']
81
82
83 def revert_font_settings(file):
84     i = 0
85     insert_line = -1
86     fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
87     for family in 'roman', 'sans', 'typewriter':
88         name = '\\font_%s' % family
89         i = find_token_exact(file.header, name, i)
90         if i == -1:
91             file.warning("Malformed LyX file: Missing `%s'." % name)
92             i = 0
93         else:
94             if (insert_line < 0):
95                 insert_line = i
96             fonts[family] = get_value(file.header, name, i, i + 1)
97             del file.header[i]
98     i = find_token_exact(file.header, '\\font_default_family', i)
99     if i == -1:
100         file.warning("Malformed LyX file: Missing `\\font_default_family'.")
101         font_default_family = 'default'
102     else:
103         font_default_family = get_value(file.header, "\\font_default_family", i, i + 1)
104         del file.header[i]
105     i = find_token_exact(file.header, '\\font_sc', i)
106     if i == -1:
107         file.warning("Malformed LyX file: Missing `\\font_sc'.")
108         font_sc = 'false'
109     else:
110         font_sc = get_value(file.header, '\\font_sc', i, i + 1)
111         del file.header[i]
112     if font_sc != 'false':
113         file.warning("Conversion of '\\font_sc' not yet implemented.")
114     i = find_token_exact(file.header, '\\font_osf', i)
115     if i == -1:
116         file.warning("Malformed LyX file: Missing `\\font_osf'.")
117         font_osf = 'false'
118     else:
119         font_osf = get_value(file.header, '\\font_osf', i, i + 1)
120         del file.header[i]
121     i = find_token_exact(file.header, '\\font_sf_scale', i)
122     if i == -1:
123         file.warning("Malformed LyX file: Missing `\\font_sf_scale'.")
124         font_sf_scale = '100'
125     else:
126         font_sf_scale = get_value(file.header, '\\font_sf_scale', i, i + 1)
127         del file.header[i]
128     if font_sf_scale != '100':
129         file.warning("Conversion of '\\font_sf_scale' not yet implemented.")
130     i = find_token_exact(file.header, '\\font_tt_scale', i)
131     if i == -1:
132         file.warning("Malformed LyX file: Missing `\\font_tt_scale'.")
133         font_tt_scale = '100'
134     else:
135         font_tt_scale = get_value(file.header, '\\font_tt_scale', i, i + 1)
136         del file.header[i]
137     if font_tt_scale != '100':
138         file.warning("Conversion of '\\font_tt_scale' not yet implemented.")
139     for font_scheme in roman_fonts.keys():
140         if (roman_fonts[font_scheme] == fonts['roman'] and
141             sans_fonts[font_scheme] == fonts['sans'] and
142             typewriter_fonts[font_scheme] == fonts['typewriter']):
143             file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
144             if font_default_family != 'default':
145                 file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
146             if font_osf == 'true':
147                 file.warning("Ignoring `\\font_osf = true'")
148             return
149     font_scheme = 'default'
150     file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
151     if fonts['roman'] == 'cmr':
152         file.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
153         if font_osf == 'true':
154             file.preamble.append('\\usepackage{eco}')
155             font_osf = 'false'
156     for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
157         if fonts['roman'] == font:
158             file.preamble.append('\\usepackage{%s}' % font)
159     for font in 'cmss', 'lmss', 'cmbr':
160         if fonts['sans'] == font:
161             file.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
162     for font in 'berasans':
163         if fonts['sans'] == font:
164             file.preamble.append('\\usepackage{%s}' % font)
165     for font in 'cmtt', 'lmtt', 'cmtl':
166         if fonts['typewriter'] == font:
167             file.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
168     for font in 'courier', 'beramono', 'luximono':
169         if fonts['typewriter'] == font:
170             file.preamble.append('\\usepackage{%s}' % font)
171     if font_default_family != 'default':
172         file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
173     if font_osf == 'true':
174         file.warning("Ignoring `\\font_osf = true'")
175
176
177 def revert_booktabs(file):
178 # we just remove the booktabs flag, everything else will become a mess.
179     re_row = re.compile(r'^<row.*space="[^"]+".*>$')
180     re_tspace = re.compile(r'\s+topspace="[^"]+"')
181     re_bspace = re.compile(r'\s+bottomspace="[^"]+"')
182     re_ispace = re.compile(r'\s+interlinespace="[^"]+"')
183     i = 0
184     while 1:
185         i = find_token(file.body, "\\begin_inset Tabular", i)
186         if i == -1:
187             return
188         j = find_end_of_inset(file.body, i + 1)
189         if j == -1:
190             file.warning("Malformed LyX file: Could not find end of tabular.")
191             continue
192         for k in range(i, j):
193             if re.search('^<features.* booktabs="true".*>$', file.body[k]):
194                 file.warning("Converting 'booktabs' table to normal table.")
195                 file.body[k] = replace(file.body[k], ' booktabs="true"', '')
196             if re.search(re_row, file.body[k]):
197                 file.warning("Removing extra row space.")
198                 file.body[k] = re_tspace.sub('', file.body[k])
199                 file.body[k] = re_bspace.sub('', file.body[k])
200                 file.body[k] = re_ispace.sub('', file.body[k])
201         i = i + 1
202
203
204 ##
205 # Conversion hub
206 #
207
208 convert = [[246, []],
209            [247, [convert_font_settings]],
210            [248, []]]
211
212 revert =  [[247, [revert_booktabs]],
213            [246, [revert_font_settings]],
214            [245, [revert_framed]]]
215
216
217 if __name__ == "__main__":
218     pass
219