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, get_value
22 from string import replace
25 ####################################################################
26 # Private helper functions
28 def find_end_of_inset(lines, i):
29 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
31 # End of helper functions
32 ####################################################################
36 # Notes: Framed/Shaded
39 def revert_framed(file):
42 i = find_tokens(file.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
46 file.body[i] = "\\begin_inset Note"
54 roman_fonts = {'default' : 'default', 'ae' : 'ae',
55 'times' : 'times', 'palatino' : 'palatino',
56 'helvet' : 'default', 'avant' : 'default',
57 'newcent' : 'newcent', 'bookman' : 'bookman',
59 sans_fonts = {'default' : 'default', 'ae' : 'default',
60 'times' : 'default', 'palatino' : 'default',
61 'helvet' : 'helvet', 'avant' : 'avant',
62 'newcent' : 'default', 'bookman' : 'default',
64 typewriter_fonts = {'default' : 'default', 'ae' : 'default',
65 'times' : 'default', 'palatino' : 'default',
66 'helvet' : 'default', 'avant' : 'default',
67 'newcent' : 'default', 'bookman' : 'default',
68 'pslatex' : 'courier'}
70 def convert_font_settings(file):
72 i = find_token_exact(file.header, "\\fontscheme", i)
74 file.warning("Malformed LyX file: Missing `\\fontscheme'.")
76 font_scheme = get_value(file.header, "\\fontscheme", i, i + 1)
78 file.warning("Malformed LyX file: Empty `\\fontscheme'.")
79 font_scheme = 'default'
80 if not font_scheme in roman_fonts.keys():
81 file.warning("Malformed LyX file: Unknown `\\fontscheme' `%s'." % font_scheme)
82 font_scheme = 'default'
83 file.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
84 '\\font_sans %s' % sans_fonts[font_scheme],
85 '\\font_typewriter %s' % typewriter_fonts[font_scheme],
86 '\\font_default_family default',
89 '\\font_sf_scale 100',
90 '\\font_tt_scale 100']
93 def revert_font_settings(file):
96 fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
97 for family in 'roman', 'sans', 'typewriter':
98 name = '\\font_%s' % family
99 i = find_token_exact(file.header, name, i)
101 file.warning("Malformed LyX file: Missing `%s'." % name)
104 if (insert_line < 0):
106 fonts[family] = get_value(file.header, name, i, i + 1)
108 i = find_token_exact(file.header, '\\font_default_family', i)
110 file.warning("Malformed LyX file: Missing `\\font_default_family'.")
111 font_default_family = 'default'
113 font_default_family = get_value(file.header, "\\font_default_family", i, i + 1)
115 i = find_token_exact(file.header, '\\font_sc', i)
117 file.warning("Malformed LyX file: Missing `\\font_sc'.")
120 font_sc = get_value(file.header, '\\font_sc', i, i + 1)
122 if font_sc != 'false':
123 file.warning("Conversion of '\\font_sc' not yet implemented.")
124 i = find_token_exact(file.header, '\\font_osf', i)
126 file.warning("Malformed LyX file: Missing `\\font_osf'.")
129 font_osf = get_value(file.header, '\\font_osf', i, i + 1)
131 i = find_token_exact(file.header, '\\font_sf_scale', i)
133 file.warning("Malformed LyX file: Missing `\\font_sf_scale'.")
134 font_sf_scale = '100'
136 font_sf_scale = get_value(file.header, '\\font_sf_scale', i, i + 1)
138 if font_sf_scale != '100':
139 file.warning("Conversion of '\\font_sf_scale' not yet implemented.")
140 i = find_token_exact(file.header, '\\font_tt_scale', i)
142 file.warning("Malformed LyX file: Missing `\\font_tt_scale'.")
143 font_tt_scale = '100'
145 font_tt_scale = get_value(file.header, '\\font_tt_scale', i, i + 1)
147 if font_tt_scale != '100':
148 file.warning("Conversion of '\\font_tt_scale' not yet implemented.")
149 for font_scheme in roman_fonts.keys():
150 if (roman_fonts[font_scheme] == fonts['roman'] and
151 sans_fonts[font_scheme] == fonts['sans'] and
152 typewriter_fonts[font_scheme] == fonts['typewriter']):
153 file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
154 if font_default_family != 'default':
155 file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
156 if font_osf == 'true':
157 file.warning("Ignoring `\\font_osf = true'")
159 font_scheme = 'default'
160 file.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
161 if fonts['roman'] == 'cmr':
162 file.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
163 if font_osf == 'true':
164 file.preamble.append('\\usepackage{eco}')
166 for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
167 if fonts['roman'] == font:
168 file.preamble.append('\\usepackage{%s}' % font)
169 for font in 'cmss', 'lmss', 'cmbr':
170 if fonts['sans'] == font:
171 file.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
172 for font in 'berasans':
173 if fonts['sans'] == font:
174 file.preamble.append('\\usepackage{%s}' % font)
175 for font in 'cmtt', 'lmtt', 'cmtl':
176 if fonts['typewriter'] == font:
177 file.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
178 for font in 'courier', 'beramono', 'luximono':
179 if fonts['typewriter'] == font:
180 file.preamble.append('\\usepackage{%s}' % font)
181 if font_default_family != 'default':
182 file.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
183 if font_osf == 'true':
184 file.warning("Ignoring `\\font_osf = true'")
187 def revert_booktabs(file):
188 # we just remove the booktabs flag, everything else will become a mess.
189 re_row = re.compile(r'^<row.*space="[^"]+".*>$')
190 re_tspace = re.compile(r'\s+topspace="[^"]+"')
191 re_bspace = re.compile(r'\s+bottomspace="[^"]+"')
192 re_ispace = re.compile(r'\s+interlinespace="[^"]+"')
195 i = find_token(file.body, "\\begin_inset Tabular", i)
198 j = find_end_of_inset(file.body, i + 1)
200 file.warning("Malformed LyX file: Could not find end of tabular.")
202 for k in range(i, j):
203 if re.search('^<features.* booktabs="true".*>$', file.body[k]):
204 file.warning("Converting 'booktabs' table to normal table.")
205 file.body[k] = replace(file.body[k], ' booktabs="true"', '')
206 if re.search(re_row, file.body[k]):
207 file.warning("Removing extra row space.")
208 file.body[k] = re_tspace.sub('', file.body[k])
209 file.body[k] = re_bspace.sub('', file.body[k])
210 file.body[k] = re_ispace.sub('', file.body[k])
218 convert = [[246, []],
219 [247, [convert_font_settings]],
222 revert = [[247, [revert_booktabs]],
223 [246, [revert_font_settings]],
224 [245, [revert_framed]]]
227 if __name__ == "__main__":