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")
32 ####################################################################
35 def revert_swiss(document):
36 "Set language german-ch to ngerman"
38 if document.language == "german-ch":
39 document.language = "ngerman"
40 i = find_token(document.header, "\\language", 0)
42 document.header[i] = "\\language ngerman"
45 j = find_token(document.body, "\\lang german-ch", j)
48 document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
51 def revert_tabularvalign(document):
\r
52 "Revert the tabular valign option"
\r
55 i = find_token(document.body, "\\begin_inset Tabular", i)
\r
58 j = find_end_of_inset(document.body, i)
\r
60 document.warning("Malformed LyX document: Could not find end of tabular.")
\r
64 k = find_token(document.body, "<features tabularvalignment=", i)
\r
69 # which valignment is specified?
\r
70 tabularvalignment_re = re.compile(r'<features tabularvalignment="(top|bottom)">')
\r
71 m = tabularvalignment_re.match(document.body[k])
\r
76 tabularvalignment = m.group(1)
\r
78 subst = ['\\end_layout', '\\end_inset']
\r
79 document.body[j+1:j+1] = subst # just inserts those lines
\r
80 subst = ['\\begin_inset Box Frameless',
\r
81 'position "' + tabularvalignment[0] +'"',
\r
89 'height_special "totalheight"',
\r
92 '\\begin_layout Plain Layout']
\r
93 document.body[i:i] = subst # this just inserts the array at i
\r
94 i += len(subst) + 2 # adjust i to save a few cycles
100 supported_versions = ["2.0.0","2.0"]
101 convert = [[346, []],
105 revert = [[346, [revert_tabularvalign]],
106 [345, [revert_swiss]]
110 if __name__ == "__main__":