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")
52 def revert_tabularvalign(document):
53 "Revert the tabular valign option"
56 i = find_token(document.body, "\\begin_inset Tabular", i)
59 j = find_end_of_inset(document.body, i)
61 document.warning("Malformed LyX document: Could not find end of tabular.")
65 k = find_token(document.body, "<features tabularvalignment=", i)
70 # which valignment is specified?
71 tabularvalignment_re = re.compile(r'<features tabularvalignment="(top|bottom)">')
72 m = tabularvalignment_re.match(document.body[k])
77 tabularvalignment = m.group(1)
79 subst = ['\\end_layout', '\\end_inset']
80 document.body[j+1:j+1] = subst # just inserts those lines
81 subst = ['\\begin_inset Box Frameless',
82 'position "' + tabularvalignment[0] +'"',
90 'height_special "totalheight"',
93 '\\begin_layout Plain Layout']
94 document.body[i:i] = subst # this just inserts the array at i
95 i += len(subst) + 2 # adjust i to save a few cycles
98 def revert_phantom(document):
99 'Reverts phantom to ERT'
103 i = find_token(document.body, "\\begin_inset Phantom Phantom", i)
106 substi = document.body[i].replace('\\begin_inset Phantom Phantom', \
107 '\\begin_inset ERT\nstatus collapsed\n\n' \
108 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
109 'phantom{\n\\end_layout\n\n\\end_inset\n')
110 substi = substi.split('\n')
111 document.body[i : i+4] = substi
113 j = find_token(document.body, "\\end_layout", i)
115 document.warning("Malformed LyX document: Could not find end of Phantom inset.")
117 substj = document.body[j].replace('\\end_layout', \
118 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
119 '\\begin_layout Plain Layout\n\n' \
120 '}\n\\end_layout\n\n\\end_inset\n')
121 substj = substj.split('\n')
122 document.body[j : j+4] = substj
126 def revert_hphantom(document):
127 'Reverts hphantom to ERT'
131 i = find_token(document.body, "\\begin_inset Phantom HPhantom", i)
134 substi = document.body[i].replace('\\begin_inset Phantom HPhantom', \
135 '\\begin_inset ERT\nstatus collapsed\n\n' \
136 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
137 'hphantom{\n\\end_layout\n\n\\end_inset\n')
138 substi = substi.split('\n')
139 document.body[i : i+4] = substi
141 j = find_token(document.body, "\\end_layout", i)
143 document.warning("Malformed LyX document: Could not find end of HPhantom inset.")
145 substj = document.body[j].replace('\\end_layout', \
146 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
147 '\\begin_layout Plain Layout\n\n' \
148 '}\n\\end_layout\n\n\\end_inset\n')
149 substj = substj.split('\n')
150 document.body[j : j+4] = substj
154 def revert_vphantom(document):
155 'Reverts vphantom to ERT'
159 i = find_token(document.body, "\\begin_inset Phantom VPhantom", i)
162 substi = document.body[i].replace('\\begin_inset Phantom VPhantom', \
163 '\\begin_inset ERT\nstatus collapsed\n\n' \
164 '\\begin_layout Plain Layout\n\n\n\\backslash\n' \
165 'vphantom{\n\\end_layout\n\n\\end_inset\n')
166 substi = substi.split('\n')
167 document.body[i : i+4] = substi
169 j = find_token(document.body, "\\end_layout", i)
171 document.warning("Malformed LyX document: Could not find end of VPhantom inset.")
173 substj = document.body[j].replace('\\end_layout', \
174 '\\size default\n\n\\begin_inset ERT\nstatus collapsed\n\n' \
175 '\\begin_layout Plain Layout\n\n' \
176 '}\n\\end_layout\n\n\\end_inset\n')
177 substj = substj.split('\n')
178 document.body[j : j+4] = substj
186 supported_versions = ["2.0.0","2.0"]
187 convert = [[346, []],
192 revert = [[347, [revert_phantom, revert_hphantom, revert_vphantom]],
193 [346, [revert_tabularvalign]],
194 [345, [revert_swiss]]
198 if __name__ == "__main__":