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
26 ####################################################################
27 # Private helper functions
29 def find_end_of_inset(lines, i):
30 " Find beginning of inset, where lines[i] is included."
31 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
33 # End of helper functions
34 ####################################################################
38 # Notes: Framed/Shaded
41 def revert_framed(document):
42 "Revert framed notes. "
45 i = find_tokens(document.body, ["\\begin_inset Note Framed", "\\begin_inset Note Shaded"], i)
49 document.body[i] = "\\begin_inset Note"
57 roman_fonts = {'default' : 'default', 'ae' : 'ae',
58 'times' : 'times', 'palatino' : 'palatino',
59 'helvet' : 'default', 'avant' : 'default',
60 'newcent' : 'newcent', 'bookman' : 'bookman',
62 sans_fonts = {'default' : 'default', 'ae' : 'default',
63 'times' : 'default', 'palatino' : 'default',
64 'helvet' : 'helvet', 'avant' : 'avant',
65 'newcent' : 'default', 'bookman' : 'default',
67 typewriter_fonts = {'default' : 'default', 'ae' : 'default',
68 'times' : 'default', 'palatino' : 'default',
69 'helvet' : 'default', 'avant' : 'default',
70 'newcent' : 'default', 'bookman' : 'default',
71 'pslatex' : 'courier'}
73 def convert_font_settings(document):
74 " Convert font settings. "
76 i = find_token_exact(document.header, "\\fontscheme", i)
78 document.warning("Malformed LyX document: Missing `\\fontscheme'.")
80 font_scheme = get_value(document.header, "\\fontscheme", i, i + 1)
82 document.warning("Malformed LyX document: Empty `\\fontscheme'.")
83 font_scheme = 'default'
84 if not font_scheme in roman_fonts.keys():
85 document.warning("Malformed LyX document: Unknown `\\fontscheme' `%s'." % font_scheme)
86 font_scheme = 'default'
87 document.header[i:i+1] = ['\\font_roman %s' % roman_fonts[font_scheme],
88 '\\font_sans %s' % sans_fonts[font_scheme],
89 '\\font_typewriter %s' % typewriter_fonts[font_scheme],
90 '\\font_default_family default',
93 '\\font_sf_scale 100',
94 '\\font_tt_scale 100']
97 def revert_font_settings(document):
98 " Revert font settings. "
101 fonts = {'roman' : 'default', 'sans' : 'default', 'typewriter' : 'default'}
102 for family in 'roman', 'sans', 'typewriter':
103 name = '\\font_%s' % family
104 i = find_token_exact(document.header, name, i)
106 document.warning("Malformed LyX document: Missing `%s'." % name)
109 if (insert_line < 0):
111 fonts[family] = get_value(document.header, name, i, i + 1)
112 del document.header[i]
113 i = find_token_exact(document.header, '\\font_default_family', i)
115 document.warning("Malformed LyX document: Missing `\\font_default_family'.")
116 font_default_family = 'default'
118 font_default_family = get_value(document.header, "\\font_default_family", i, i + 1)
119 del document.header[i]
120 i = find_token_exact(document.header, '\\font_sc', i)
122 document.warning("Malformed LyX document: Missing `\\font_sc'.")
125 font_sc = get_value(document.header, '\\font_sc', i, i + 1)
126 del document.header[i]
127 if font_sc != 'false':
128 document.warning("Conversion of '\\font_sc' not yet implemented.")
129 i = find_token_exact(document.header, '\\font_osf', i)
131 document.warning("Malformed LyX document: Missing `\\font_osf'.")
134 font_osf = get_value(document.header, '\\font_osf', i, i + 1)
135 del document.header[i]
136 i = find_token_exact(document.header, '\\font_sf_scale', i)
138 document.warning("Malformed LyX document: Missing `\\font_sf_scale'.")
139 font_sf_scale = '100'
141 font_sf_scale = get_value(document.header, '\\font_sf_scale', i, i + 1)
142 del document.header[i]
143 if font_sf_scale != '100':
144 document.warning("Conversion of '\\font_sf_scale' not yet implemented.")
145 i = find_token_exact(document.header, '\\font_tt_scale', i)
147 document.warning("Malformed LyX document: Missing `\\font_tt_scale'.")
148 font_tt_scale = '100'
150 font_tt_scale = get_value(document.header, '\\font_tt_scale', i, i + 1)
151 del document.header[i]
152 if font_tt_scale != '100':
153 document.warning("Conversion of '\\font_tt_scale' not yet implemented.")
154 for font_scheme in roman_fonts.keys():
155 if (roman_fonts[font_scheme] == fonts['roman'] and
156 sans_fonts[font_scheme] == fonts['sans'] and
157 typewriter_fonts[font_scheme] == fonts['typewriter']):
158 document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
159 if font_default_family != 'default':
160 document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
161 if font_osf == 'true':
162 document.warning("Ignoring `\\font_osf = true'")
164 font_scheme = 'default'
165 document.header.insert(insert_line, '\\fontscheme %s' % font_scheme)
166 if fonts['roman'] == 'cmr':
167 document.preamble.append('\\renewcommand{\\rmdefault}{cmr}')
168 if font_osf == 'true':
169 document.preamble.append('\\usepackage{eco}')
171 for font in 'lmodern', 'charter', 'utopia', 'beraserif', 'ccfonts', 'chancery':
172 if fonts['roman'] == font:
173 document.preamble.append('\\usepackage{%s}' % font)
174 for font in 'cmss', 'lmss', 'cmbr':
175 if fonts['sans'] == font:
176 document.preamble.append('\\renewcommand{\\sfdefault}{%s}' % font)
177 for font in 'berasans':
178 if fonts['sans'] == font:
179 document.preamble.append('\\usepackage{%s}' % font)
180 for font in 'cmtt', 'lmtt', 'cmtl':
181 if fonts['typewriter'] == font:
182 document.preamble.append('\\renewcommand{\\ttdefault}{%s}' % font)
183 for font in 'courier', 'beramono', 'luximono':
184 if fonts['typewriter'] == font:
185 document.preamble.append('\\usepackage{%s}' % font)
186 if font_default_family != 'default':
187 document.preamble.append('\\renewcommand{\\familydefault}{\\%s}' % font_default_family)
188 if font_osf == 'true':
189 document.warning("Ignoring `\\font_osf = true'")
192 def revert_booktabs(document):
193 " We remove the booktabs flag or everything else will become a mess. "
194 re_row = re.compile(r'^<row.*space="[^"]+".*>$')
195 re_tspace = re.compile(r'\s+topspace="[^"]+"')
196 re_bspace = re.compile(r'\s+bottomspace="[^"]+"')
197 re_ispace = re.compile(r'\s+interlinespace="[^"]+"')
200 i = find_token(document.body, "\\begin_inset Tabular", i)
203 j = find_end_of_inset(document.body, i + 1)
205 document.warning("Malformed LyX document: Could not find end of tabular.")
207 for k in range(i, j):
208 if re.search('^<features.* booktabs="true".*>$', document.body[k]):
209 document.warning("Converting 'booktabs' table to normal table.")
210 document.body[k] = document.body[k].replace(' booktabs="true"', '')
211 if re.search(re_row, document.body[k]):
212 document.warning("Removing extra row space.")
213 document.body[k] = re_tspace.sub('', document.body[k])
214 document.body[k] = re_bspace.sub('', document.body[k])
215 document.body[k] = re_ispace.sub('', document.body[k])
219 def convert_utf8(document):
220 i = find_token(document.header, "\\inputencoding", 0)
222 document.header.append("\\inputencoding utf-8")
224 document.header[i] = "\\inputencoding utf-8"
225 document.inputencoding = "utf-8"
226 document.encoding = "utf-8"
232 supported_versions = ["1.5.0","1.5"]
233 convert = [[246, []],
234 [247, [convert_font_settings]],
236 # ,[xxx, [convert_utf8]] uncomment to support convertion to utf-8
239 revert = [[247, [revert_booktabs]],
240 [246, [revert_font_settings]],
241 [245, [revert_framed]]]
244 if __name__ == "__main__":