]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_0.py
support for the LaTeX commands \*phantom, fileformat change
[lyx.git] / lib / lyx2lyx / lyx_2_0.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008 José Matos  <jamatos@lyx.org>
4 #
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.
9 #
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.
14 #
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.
18
19 """ Convert files to the file format generated by lyx 2.0"""
20
21 import re
22
23 from parser_tools import find_token, find_end_of, find_tokens, get_value, get_value_string
24
25 ####################################################################
26 # Private helper functions
27
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")
31
32 ####################################################################
33
34
35 def revert_swiss(document):
36     "Set language german-ch to ngerman"
37     i = 0
38     if document.language == "german-ch":
39         document.language = "ngerman"
40         i = find_token(document.header, "\\language", 0)
41         if i != -1:
42             document.header[i] = "\\language ngerman"
43     j = 0
44     while True:
45         j = find_token(document.body, "\\lang german-ch", j)
46         if j == -1:
47             return
48         document.body[j] = document.body[j].replace("\\lang german-ch", "\\lang ngerman")
49         j = j + 1
50
51
52 def revert_tabularvalign(document):
53    "Revert the tabular valign option"
54    i = 0
55    while True:
56        i = find_token(document.body, "\\begin_inset Tabular", i)
57        if i == -1:
58            return
59        j = find_end_of_inset(document.body, i)
60        if j == -1:
61            document.warning("Malformed LyX document: Could not find end of tabular.")
62            i = j
63            continue
64
65        k = find_token(document.body, "<features tabularvalignment=", i)
66        if k == -1:
67            i = j
68            continue
69
70        # which valignment is specified?
71        tabularvalignment_re = re.compile(r'<features tabularvalignment="(top|bottom)">')
72        m = tabularvalignment_re.match(document.body[k])
73        if not m:
74            i = j
75            continue
76
77        tabularvalignment = m.group(1)
78
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] +'"',
83            'hor_pos "c"',
84            'has_inner_box 1',
85            'inner_pos "c"',
86            'use_parbox 0',
87            'width "0col%"',
88            'special "none"',
89            'height "1in"',
90            'height_special "totalheight"',
91            'status open',
92            '',
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
96
97
98 def revert_phantom(document):
99     'Reverts phantom to ERT'
100     i = 0
101     j = 0
102     while True:
103       i = find_token(document.body, "\\begin_inset Phantom Phantom", i)
104       if i == -1:
105           return
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
112       i += len(substi)
113       j = find_token(document.body, "\\end_layout", i)
114       if j == -1:
115           document.warning("Malformed LyX document: Could not find end of Phantom inset.")
116           return
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
123       i += len(substj)
124
125
126 def revert_hphantom(document):
127     'Reverts hphantom to ERT'
128     i = 0
129     j = 0
130     while True:
131       i = find_token(document.body, "\\begin_inset Phantom HPhantom", i)
132       if i == -1:
133           return
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
140       i += len(substi)
141       j = find_token(document.body, "\\end_layout", i)
142       if j == -1:
143           document.warning("Malformed LyX document: Could not find end of HPhantom inset.")
144           return
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
151       i += len(substj)
152
153
154 def revert_vphantom(document):
155     'Reverts vphantom to ERT'
156     i = 0
157     j = 0
158     while True:
159       i = find_token(document.body, "\\begin_inset Phantom VPhantom", i)
160       if i == -1:
161           return
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
168       i += len(substi)
169       j = find_token(document.body, "\\end_layout", i)
170       if j == -1:
171           document.warning("Malformed LyX document: Could not find end of VPhantom inset.")
172           return
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
179       i += len(substj)
180
181
182 ##
183 # Conversion hub
184 #
185
186 supported_versions = ["2.0.0","2.0"]
187 convert = [[346, []],
188            [347, []],
189            [348, []]
190           ]
191
192 revert =  [[347, [revert_phantom, revert_hphantom, revert_vphantom]],
193            [346, [revert_tabularvalign]],
194            [345, [revert_swiss]]
195           ]
196
197
198 if __name__ == "__main__":
199     pass