]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_0_12.py
9a44e1da37400be51aab66178030cc42cc79cde7
[lyx.git] / lib / lyx2lyx / lyx_0_12.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2003-2004 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 import re
20 import string
21 from parser_tools import find_token, find_re, check_token
22
23
24 def space_before_layout(lines):
25     i = 2 # skip first layout
26     while 1:
27         i = find_token(lines, '\\layout', i)
28         if i == -1:
29             break
30
31         if lines[i - 1] == '' and string.find(lines[i-2],'\\protected_separator') == -1:
32             del lines[i-1]
33         i = i + 1
34
35
36 def formula_inset_space_eat(lines):
37     i=0
38     while 1:
39         i = find_token(lines, "\\begin_inset Formula", i)
40         if i == -1: break
41
42         if len(lines[i]) > 22 and lines[i][21] == ' ':
43             lines[i] = lines[i][:20] + lines[i][21:]
44         i = i + 1
45
46
47 # Update from tabular format 2 to 4
48 def update_tabular(lines):
49     lyxtable_re = re.compile(r".*\\LyXTable$")
50     i=0
51     while 1:
52         i = find_re(lines, lyxtable_re, i)
53         if i == -1:
54             break
55         i = i + 1
56         format = lines[i][8]
57
58         lines[i]='multicol4'
59         i = i + 1
60         rows = int(string.split(lines[i])[0])
61         columns = int(string.split(lines[i])[1])
62
63         lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
64         i = i + 1
65
66         for j in range(rows):
67             lines[i] = lines[i] + ' 0 0'
68             i = i + 1
69
70         for j in range(columns):
71             lines[i] = lines[i] + ' '
72             i = i + 1
73
74         while lines[i]:
75             lines[i] = lines[i] + ' 0 0 0'
76             i = i + 1
77
78
79 def final_dot(lines):
80     i = 0
81     while i < len(lines):
82         if lines[i][-1:] == '.' and lines[i+1][:1] != '\\' and  lines[i+1][:1] != ' ' and len(lines[i]) + len(lines[i+1])<= 72 and lines[i+1] != '':
83             lines[i] = lines[i] + lines[i+1]
84             del lines[i+1]
85         else:
86             i = i + 1
87
88
89 def update_inset_label(lines):
90     i = 0
91     while 1:
92         i = find_token(lines, '\\begin_inset Label', i)
93         if i == -1:
94             return
95         lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
96         i = i + 1
97
98
99 def update_latexdel(lines):
100     i = 0
101     while 1:
102         i = find_token(lines, '\\begin_inset LatexDel', i)
103         if i == -1:
104             return
105         lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
106         i = i + 1
107
108
109 def update_vfill(lines):
110     for i in range(len(lines)):
111         lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
112         lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
113
114
115 def update_space_units(lines):
116     added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
117     added_space_top    = re.compile(r'\\added_space_top ([^ ]*)')
118     for i in range(len(lines)):
119         result = added_space_bottom.search(lines[i])
120         if result:
121             old = '\\added_space_bottom ' + result.group(1)
122             new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
123             lines[i] = string.replace(lines[i], old, new)
124
125         result = added_space_top.search(lines[i])
126         if result:
127             old = '\\added_space_top ' + result.group(1)
128             new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
129             lines[i] = string.replace(lines[i], old, new)
130
131
132 def update_inset_accent(lines):
133     pass
134
135
136 def remove_cursor(lines):
137     i = 0
138     cursor_re = re.compile(r'.*(\\cursor \d*)')
139     while 1:
140         i = find_re(lines, cursor_re, i)
141         if i == -1:
142             break
143         cursor = cursor_re.search(lines[i]).group(1)
144         lines[i]= string.replace(lines[i], cursor, '')
145         i = i + 1
146
147
148 def remove_empty_insets(lines):
149     i = 0
150     while 1:
151         i = find_token(lines, '\\begin_inset ',i)
152         if i == -1:
153             break
154         if lines[i] == '\\begin_inset ' and lines[i+1] == '\\end_inset ':
155             del lines[i]
156             del lines[i]
157         i = i + 1
158
159
160 def remove_formula_latex(lines):
161     i = 0
162     while 1:
163         i = find_token(lines, '\\latex formula_latex ', i)
164         if i == -1:
165             break
166         del lines[i]
167
168         i = find_token(lines, '\\latex default', i)
169         if i == -1:
170             break
171         del lines[i]
172
173
174 def add_end_document(lines):
175     i = find_token(lines, '\\the_end', 0)
176     if i == -1:
177         lines.append('\\the_end')
178
179
180 def header_update(lines, opt):
181     i = 0
182     l = len(lines)
183     while i < l:
184         if check_token(lines[i], '\\begin_preamble'):
185             i = find_token(lines, '\\end_preamble', i)
186             if i == -1:
187                 opt.error('Unfinished preamble')
188             i = i + 1
189             continue
190
191         if lines[i][-1:] == ' ':
192             lines[i] = lines[i][:-1]
193
194         if check_token(lines[i], '\\epsfig'):
195             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
196             i = i + 1
197             continue
198
199         if check_token(lines[i], '\\papersize'):
200             size = string.split(lines[i])[1]
201             new_size = size
202             paperpackage = ""
203
204             if size == 'usletter':
205                 new_size = 'letterpaper'
206             if size == 'a4wide':
207                 new_size = 'Default'
208                 paperpackage = "widemarginsa4"
209
210             lines[i] = '\\papersize ' + new_size
211             i = i + 1
212             if paperpackage:
213                 lines.insert(i, '\\paperpackage ' + paperpackage)
214                 i = i + 1
215
216             lines.insert(i,'\\use_geometry 0')
217             lines.insert(i + 1,'\\use_amsmath 0')
218             i = i + 2
219             continue
220
221
222         if check_token(lines[i], '\\baselinestretch'):
223             size = string.split(lines[i])[1]
224             if size == '1.00':
225                 name = 'single'
226             elif size == '1.50':
227                 name = 'onehalf'
228             elif size == '2.00':
229                 name = 'double'
230             else:
231                 name = 'other ' + size
232             lines[i] = '\\spacing %s ' % name
233             i = i + 1
234             continue
235
236         i = i + 1
237
238
239 def convert(header,body, opt):
240     header_update(header, opt)
241     add_end_document(body)
242     remove_cursor(body)
243     final_dot(body)
244     update_inset_label(body)
245     update_latexdel(body)
246     update_space_units(body)
247     update_inset_accent(body)
248     space_before_layout(body)
249     formula_inset_space_eat(body)
250     update_tabular(body)
251     update_vfill(body)
252     remove_empty_insets(body)
253     remove_formula_latex(body)
254     opt.format = 215
255
256
257 def revert(header, body, opt):
258     opt.error("The convertion to an older format (%s) is not implemented." % opt.format)
259
260
261 if __name__ == "__main__":
262     pass