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