1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008 José Matos <jamatos@lyx.org>
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 """ Convert files to the file format generated by lyx 2.0"""
23 from parser_tools import find_token, find_end_of, find_tokens, get_value, get_value_string
25 ####################################################################
26 # Private helper functions
28 def find_end_of_inset(lines, i):
29 " Find end of inset, where lines[i] is included."
30 return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
33 def add_to_preamble(document, text):
34 """ Add text to the preamble if it is not already there.
35 Only the first line is checked!"""
37 if find_token(document.preamble, text[0], 0) != -1:
40 document.preamble.extend(text)
43 def insert_to_preamble(index, document, text):
44 """ Insert text to the preamble at a given line"""
46 document.preamble.insert(index, text)
49 ####################################################################
52 def revert_swiss(document):
53 " Set language german-ch to ngerman "
55 if document.language == "german-ch":
56 document.language = "ngerman"
57 i = find_token(document.header, "\\language", 0)
59 document.header[i] = "\\language ngerman"
62 j = find_token(document.body, "\\lang german-ch", j)
65 document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
69 def revert_tabularvalign(document):
70 " Revert the tabular valign option "
73 i = find_token(document.body, "\\begin_inset Tabular", i)
76 j = find_end_of_inset(document.body, i)
78 document.warning("Malformed LyX document: Could not find end of tabular.")
82 k = find_token(document.body, "<features tabularvalignment=", i)
87 # which valignment is specified?
88 tabularvalignment_re = re.compile(r'<features tabularvalignment="(top|bottom)">')
89 m = tabularvalignment_re.match(document.body[k])
94 tabularvalignment = m.group(1)
96 subst = ['\\end_layout', '\\end_inset']
97 document.body[j+1:j+1] = subst # just inserts those lines
98 subst = ['\\begin_inset Box Frameless',
99 'position "' + tabularvalignment[0] +'"',
107 'height_special "totalheight"',
110 '\\begin_layout Plain Layout']
111 document.body[i:i] = subst # this just inserts the array at i
112 i += len(subst) + 2 # adjust i to save a few cycles
115 def revert_phantom(document):
116 " Reverts phantom to ERT "
120 i = find_token(document.body, "\\begin_inset Phantom Phantom", i)
123 substi = document.body[i].replace('\\begin_inset Phantom Phantom', \
124 '\\begin_inset ERT\nstatus collapsed\n\n' \
125 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
126 'phantom{\n\\end_layout\n\n\\end_inset\n')
127 substi = substi.split('\n')
128 document.body[i : i+4] = substi
130 j = find_token(document.body, "\\end_layout", i)
132 document.warning("Malformed LyX document: Could not find end of Phantom inset.")
134 substj = document.body[j].replace('\\end_layout', \
135 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
136 '\\begin_layout Plain Layout\n\n' \
137 '}\n\\end_layout\n\n\\end_inset\n')
138 substj = substj.split('\n')
139 document.body[j : j+4] = substj
143 def revert_hphantom(document):
144 " Reverts hphantom to ERT "
148 i = find_token(document.body, "\\begin_inset Phantom HPhantom", i)
151 substi = document.body[i].replace('\\begin_inset Phantom HPhantom', \
152 '\\begin_inset ERT\nstatus collapsed\n\n' \
153 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
154 'hphantom{\n\\end_layout\n\n\\end_inset\n')
155 substi = substi.split('\n')
156 document.body[i : i+4] = substi
158 j = find_token(document.body, "\\end_layout", i)
160 document.warning("Malformed LyX document: Could not find end of HPhantom inset.")
162 substj = document.body[j].replace('\\end_layout', \
163 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
164 '\\begin_layout Plain Layout\n\n' \
165 '}\n\\end_layout\n\n\\end_inset\n')
166 substj = substj.split('\n')
167 document.body[j : j+4] = substj
171 def revert_vphantom(document):
172 " Reverts vphantom to ERT "
176 i = find_token(document.body, "\\begin_inset Phantom VPhantom", i)
179 substi = document.body[i].replace('\\begin_inset Phantom VPhantom', \
180 '\\begin_inset ERT\nstatus collapsed\n\n' \
181 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
182 'vphantom{\n\\end_layout\n\n\\end_inset\n')
183 substi = substi.split('\n')
184 document.body[i : i+4] = substi
186 j = find_token(document.body, "\\end_layout", i)
188 document.warning("Malformed LyX document: Could not find end of VPhantom inset.")
190 substj = document.body[j].replace('\\end_layout', \
191 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
192 '\\begin_layout Plain Layout\n\n' \
193 '}\n\\end_layout\n\n\\end_inset\n')
194 substj = substj.split('\n')
195 document.body[j : j+4] = substj
199 def revert_xetex(document):
200 " Reverts documents that use XeTeX "
201 i = find_token(document.header, '\\use_xetex', 0)
204 del document.header[i]
205 # 1.) set doc encoding to utf8-plain
206 i = find_token(document.header, "\\inputencoding", 0)
208 document.warning("Malformed LyX document: Missing \\inputencoding.")
209 document.header[i] = "\\inputencoding utf8-plain"
210 # 2.) check font settings
211 l = find_token(document.header, "\\font_roman", 0)
213 document.warning("Malformed LyX document: Missing \\font_roman.")
214 line = document.header[l]
215 l = re.compile(r'\\font_roman (.*)$')
218 l = find_token(document.header, "\\font_sans", 0)
220 document.warning("Malformed LyX document: Missing \\font_sans.")
221 line = document.header[l]
222 l = re.compile(r'\\font_sans (.*)$')
225 l = find_token(document.header, "\\font_typewriter", 0)
227 document.warning("Malformed LyX document: Missing \\font_typewriter.")
228 line = document.header[l]
229 l = re.compile(r'\\font_typewriter (.*)$')
231 typewriter = m.group(1)
232 osf = get_value(document.header, '\\font_osf', 0) == "true"
233 sf_scale = float(get_value(document.header, '\\font_sf_scale', 0))
234 tt_scale = float(get_value(document.header, '\\font_tt_scale', 0))
235 # 3.) set preamble stuff
236 pretext = '%% This document must be processed with xelatex!\n'
237 pretext += '\\usepackage{fontspec}\n'
238 pretext += '\\setmainfont[Mapping=tex-text]{' + roman + '}\n'
239 pretext += '\\setsansfont['
241 pretext += 'Scale=' + str(sf_scale / 100) + ','
242 pretext += 'Mapping=tex-text]{' + sans + '}\n'
243 pretext += '\\setmonofont'
245 pretext += '[Scale=' + str(tt_scale / 100) + ']'
246 pretext += '{' + typewriter + '}\n'
248 pretext += '\\defaultfontfeatures{Numbers=OldStyle}\n'
249 insert_to_preamble(0, document, pretext)
250 # 4.) reset font settings
251 i = find_token(document.header, "\\font_roman", 0)
253 document.warning("Malformed LyX document: Missing \\font_roman.")
254 document.header[i] = "\\font_roman default"
255 i = find_token(document.header, "\\font_sans", 0)
257 document.warning("Malformed LyX document: Missing \\font_sans.")
258 document.header[i] = "\\font_sans default"
259 i = find_token(document.header, "\\font_typewriter", 0)
261 document.warning("Malformed LyX document: Missing \\font_typewriter.")
262 document.header[i] = "\\font_typewriter default"
263 i = find_token(document.header, "\\font_osf", 0)
265 document.warning("Malformed LyX document: Missing \\font_osf.")
266 document.header[i] = "\\font_osf false"
267 i = find_token(document.header, "\\font_sc", 0)
269 document.warning("Malformed LyX document: Missing \\font_sc.")
270 document.header[i] = "\\font_sc false"
271 i = find_token(document.header, "\\font_sf_scale", 0)
273 document.warning("Malformed LyX document: Missing \\font_sf_scale.")
274 document.header[i] = "\\font_sf_scale 100"
275 i = find_token(document.header, "\\font_tt_scale", 0)
277 document.warning("Malformed LyX document: Missing \\font_tt_scale.")
278 document.header[i] = "\\font_tt_scale 100"
285 supported_versions = ["2.0.0","2.0"]
286 convert = [[346, []],
292 revert = [[348, [revert_xetex]],
293 [347, [revert_phantom, revert_hphantom, revert_vphantom]],
294 [346, [revert_tabularvalign]],
295 [345, [revert_swiss]]
299 if __name__ == "__main__":