1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
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.
20 """ Convert files to the file format generated by lyx 1.5"""
23 from parser_tools import find_token, find_token_exact, find_tokens, find_end_of, get_value
24 from LyX import get_encoding
27 ####################################################################
28 # Private helper functions
30 def find_end_of_inset(lines, i):
31 " Find beginning of inset, where lines[i] is included."
32 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
34 # End of helper functions
35 ####################################################################
39 # Notes: Framed/Shaded
42 def revert_framed(document):
43 "Revert framed notes. "
46 i = find_tokens(document.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
50 document.body[i] = "\\begin_inset Note"
58 roman_fonts = {'default' : 'default', 'ae' : 'ae',
59 'times' : 'times', 'palatino' : 'palatino',
60 'helvet' : 'default', 'avant' : 'default',
61 'newcent' : 'newcent', 'bookman' : 'bookman',
63 sans_fonts = {'default' : 'default', 'ae' : 'default',
64 'times' : 'default', 'palatino' : 'default',
65 'helvet' : 'helvet', 'avant' : 'avant',
66 'newcent' : 'default', 'bookman' : 'default',
68 typewriter_fonts = {'default' : 'default', 'ae' : 'default',
69 'times' : 'default', 'palatino' : 'default',
70 'helvet' : 'default', 'avant' : 'default',
71 'newcent' : 'default', 'bookman' : 'default',
72 'pslatex' : 'courier'}
74 def convert_font_settings(document):
75 " Convert font settings. "
77 i = find_token_exact(document.header, "\\fontscheme", i)
79 document.warning("Malformed LyX document: Missing `\\fontscheme'.")
81 font_scheme = get_value(document.header, "\\fontscheme", i, i + 1)
83 document.warning("Malformed LyX document: Empty `\\fontscheme'.")
84 font_scheme = 'default'
85 if not font_scheme in roman_fonts.keys():
86 document.warning("Malformed LyX document: Unknown `\\fontscheme' `%s'." % font_scheme)
87 font_scheme = 'default'
88 document.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
89 '\\font_sans %s' % sans_fonts[font_scheme],
90 '\\font_typewriter %s' % typewriter_fonts[font_scheme],
91 '\\font_default_family default',
94 '\\font_sf_scale 100',
95 '\\font_tt_scale 100']
98 def revert_font_settings(document):
99 " Revert font settings. "
102 fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
103 for family in 'roman', 'sans', 'typewriter':
104 name = '\\font_%s' % family
105 i = find_token_exact(document.header, name, i)
107 document.warning("Malformed LyX document: Missing `%s'." % name)
110 if (insert_line < 0):
112 fonts[family] = get_value(document.header, name, i, i + 1)
113 del document.header[i]
114 i = find_token_exact(document.header, '\\font_default_family', i)
116 document.warning("Malformed LyX document: Missing `\\font_default_family'.")
117 font_default_family = 'default'
119 font_default_family = get_value(document.header, "\\font_default_family", i, i + 1)
120 del document.header[i]
121 i = find_token_exact(document.header, '\\font_sc', i)
123 document.warning("Malformed LyX document: Missing `\\font_sc'.")
126 font_sc = get_value(document.header, '\\font_sc', i, i + 1)
127 del document.header[i]
128 if font_sc != 'false':
129 document.warning("Conversion of '\\font_sc' not yet implemented.")
130 i = find_token_exact(document.header, '\\font_osf', i)
132 document.warning("Malformed LyX document: Missing `\\font_osf'.")
135 font_osf = get_value(document.header, '\\font_osf', i, i + 1)
136 del document.header[i]
137 i = find_token_exact(document.header, '\\font_sf_scale', i)
139 document.warning("Malformed LyX document: Missing `\\font_sf_scale'.")
140 font_sf_scale = '100'
142 font_sf_scale = get_value(document.header, '\\font_sf_scale', i, i + 1)
143 del document.header[i]
144 if font_sf_scale != '100':
145 document.warning("Conversion of '\\font_sf_scale' not yet implemented.")
146 i = find_token_exact(document.header, '\\font_tt_scale', i)
148 document.warning("Malformed LyX document: Missing `\\font_tt_scale'.")
149 font_tt_scale = '100'
151 font_tt_scale = get_value(document.header, '\\font_tt_scale', i, i + 1)
152 del document.header[i]
153 if font_tt_scale != '100':
154 document.warning("Conversion of '\\font_tt_scale' not yet implemented.")
155 for font_scheme in roman_fonts.keys():
156 if (roman_fonts[font_scheme] == fonts['roman'] and
157 sans_fonts[font_scheme] == fonts['sans'] and
158 typewriter_fonts[font_scheme] == fonts['typewriter']):
159 document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
160 if font_default_family != 'default':
161 document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
162 if font_osf == 'true':
163 document.warning("Ignoring `\\font_osf = true'")
165 font_scheme = 'default'
166 document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
167 if fonts['roman'] == 'cmr':
168 document.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
169 if font_osf == 'true':
170 document.preamble.append('\\usepackage{eco}')
172 for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
173 if fonts['roman'] == font:
174 document.preamble.append('\\usepackage{%s}' % font)
175 for font in 'cmss', 'lmss', 'cmbr':
176 if fonts['sans'] == font:
177 document.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
178 for font in 'berasans':
179 if fonts['sans'] == font:
180 document.preamble.append('\\usepackage{%s}' % font)
181 for font in 'cmtt', 'lmtt', 'cmtl':
182 if fonts['typewriter'] == font:
183 document.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
184 for font in 'courier', 'beramono', 'luximono':
185 if fonts['typewriter'] == font:
186 document.preamble.append('\\usepackage{%s}' % font)
187 if font_default_family != 'default':
188 document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
189 if font_osf == 'true':
190 document.warning("Ignoring `\\font_osf = true'")
193 def revert_booktabs(document):
194 " We remove the booktabs flag or everything else will become a mess. "
195 re_row = re.compile(r'^<row.*space="[^"]+".*>$')
196 re_tspace = re.compile(r'\s+topspace="[^"]+"')
197 re_bspace = re.compile(r'\s+bottomspace="[^"]+"')
198 re_ispace = re.compile(r'\s+interlinespace="[^"]+"')
201 i = find_token(document.body, "\\begin_inset Tabular", i)
204 j = find_end_of_inset(document.body, i + 1)
206 document.warning("Malformed LyX document: Could not find end of tabular.")
208 for k in range(i, j):
209 if re.search('^<features.* booktabs="true".*>$', document.body[k]):
210 document.warning("Converting 'booktabs' table to normal table.")
211 document.body[k] = document.body[k].replace(' booktabs="true"', '')
212 if re.search(re_row, document.body[k]):
213 document.warning("Removing extra row space.")
214 document.body[k] = re_tspace.sub('', document.body[k])
215 document.body[k] = re_bspace.sub('', document.body[k])
216 document.body[k] = re_ispace.sub('', document.body[k])
220 def convert_utf8(document):
221 document.encoding = "utf8"
224 def revert_utf8(document):
225 i = find_token(document.header, "\\inputencoding", 0)
227 document.header.append("\\inputencoding auto")
228 elif get_value(document.header, "\\inputencoding", i) == "utf8":
229 document.header[i] = "\\inputencoding auto"
230 document.inputencoding = get_value(document.header, "\\inputencoding", 0)
231 document.encoding = get_encoding(document.language, document.inputencoding, 248)
238 supported_versions = ["1.5.0","1.5"]
239 convert = [[246, []],
240 [247, [convert_font_settings]],
242 [249, [convert_utf8]],
246 [248, [revert_utf8]],
247 [247, [revert_booktabs]],
248 [246, [revert_font_settings]],
249 [245, [revert_framed]]]
252 if __name__ == "__main__":