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>
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.
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.
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.
21 from parser_tools import find_token, find_token_exact, find_tokens, find_end_of_inset, get_value
22 from string import replace
26 # Notes: Framed/Shaded
29 def revert_framed(file):
32 i = find_tokens(file.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
36 file.body[i] = "\\begin_inset Note"
44 roman_fonts = {'default' : 'default', 'ae' : 'ae',
45 'times' : 'times', 'palatino' : 'palatino',
46 'helvet' : 'default', 'avant' : 'default',
47 'newcent' : 'newcent', 'bookman' : 'bookman',
49 sans_fonts = {'default' : 'default', 'ae' : 'default',
50 'times' : 'default', 'palatino' : 'default',
51 'helvet' : 'helvet', 'avant' : 'avant',
52 'newcent' : 'default', 'bookman' : 'default',
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'}
60 def convert_font_settings(file):
62 i = find_token_exact(file.header, "\\fontscheme", i)
64 file.warning("Malformed LyX file: Missing `\\fontscheme'.")
66 font_scheme = get_value(file.header, "\\fontscheme", i, i + 1)
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',
79 '\\font_sf_scale 100',
80 '\\font_tt_scale 100']
83 def revert_font_settings(file):
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)
91 file.warning("Malformed LyX file: Missing `%s'." % name)
96 fonts[family] = get_value(file.header, name, i, i + 1)
98 i = find_token_exact(file.header, '\\font_default_family', i)
100 file.warning("Malformed LyX file: Missing `\\font_default_family'.")
101 font_default_family = 'default'
103 font_default_family = get_value(file.header, "\\font_default_family", i, i + 1)
105 i = find_token_exact(file.header, '\\font_sc', i)
107 file.warning("Malformed LyX file: Missing `\\font_sc'.")
110 font_sc = get_value(file.header, '\\font_sc', i, i + 1)
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)
116 file.warning("Malformed LyX file: Missing `\\font_osf'.")
119 font_osf = get_value(file.header, '\\font_osf', i, i + 1)
121 i = find_token_exact(file.header, '\\font_sf_scale', i)
123 file.warning("Malformed LyX file: Missing `\\font_sf_scale'.")
124 font_sf_scale = '100'
126 font_sf_scale = get_value(file.header, '\\font_sf_scale', i, i + 1)
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)
132 file.warning("Malformed LyX file: Missing `\\font_tt_scale'.")
133 font_tt_scale = '100'
135 font_tt_scale = get_value(file.header, '\\font_tt_scale', i, i + 1)
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'")
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}')
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'")
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="[^"]+"')
185 i = find_token(file.body, "\\begin_inset Tabular", i)
188 j = find_end_of_inset(file.body, i + 1)
190 file.warning("Malformed LyX file: Could not find end of tabular.")
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])
208 convert = [[246, []],
209 [247, [convert_font_settings]],
212 revert = [[247, [revert_booktabs]],
213 [246, [revert_font_settings]],
214 [245, [revert_framed]]]
217 if __name__ == "__main__":