]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_210.py
Modify the InsetBox format to always start 'Box'.
[lyx.git] / lib / lyx2lyx / lyxconvert_210.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2003 José Matos <jamatos@lyx.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18 import re
19 import string
20 from parser_tools import *
21
22 def space_before_layout(lines):
23     i = 2 # skip first layout
24     while 1:
25         i = find_token(lines, '\\layout', i)
26         if i == -1:
27             break
28
29         if lines[i - 1] == '':
30             del lines[i-1]
31         i = i + 1
32
33 def formula_inset_space_eat(lines):
34     i=0
35     while 1:
36         i = find_token(lines, "\\begin_inset Formula", i)
37         if i == -1: break
38
39         if len(lines[i]) > 22:
40             lines[i] = lines[i][:20] + lines[i][21:]
41         i = i + 1
42
43 # Update from tabular format 2 to 4
44 def update_tabular(lines):
45     lyxtable_re = re.compile(r".*\\LyXTable$")
46     i=0
47     while 1:
48         i = find_re(lines, lyxtable_re, i)
49         if i == -1:
50             break
51         i = i + 1
52         format = lines[i][8]
53
54         lines[i]='multicol4'
55         i = i + 1
56         rows = int(string.split(lines[i])[0])
57         columns = int(string.split(lines[i])[1])
58
59         lines[i] = lines[i] + ' 0 0 -1 -1 -1 -1'
60         i = i + 1
61
62         for j in range(rows):
63             lines[i] = lines[i] + ' 0 0'
64             i = i + 1
65
66         for j in range(columns):
67             lines[i] = lines[i] + ' '
68             i = i + 1
69
70         while lines[i]:
71             lines[i] = lines[i] + ' 0 0 0'
72             i = i + 1
73
74 def final_dot(lines):
75     i = 0
76     while i < len(lines):
77         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] != '':
78             lines[i] = lines[i] + lines[i+1]
79             del lines[i+1]
80         else:
81             i = i + 1
82
83 def update_inset_label(lines):
84     i = 0
85     while 1:
86         i = find_token(lines, '\\begin_inset Label', i)
87         if i == -1:
88             return
89         lines[i] = '\\begin_inset LatexCommand \label{' + lines[i][19:] + '}'
90         i = i + 1
91
92 def update_latexdel(lines):
93     i = 0
94     while 1:
95         i = find_token(lines, '\\begin_inset LatexDel', i)
96         if i == -1:
97             return
98         lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
99         i = i + 1
100
101 def update_vfill(lines):
102     for i in range(len(lines)):
103         lines[i] = string.replace(lines[i],'\\fill_top','\\added_space_top vfill')
104         lines[i] = string.replace(lines[i],'\\fill_bottom','\\added_space_bottom vfill')
105
106 def update_space_units(lines):
107     added_space_bottom = re.compile(r'\\added_space_bottom ([^ ]*)')
108     added_space_top    = re.compile(r'\\added_space_top ([^ ]*)')
109     for i in range(len(lines)):
110         result = added_space_bottom.search(lines[i])
111         if result:
112             old = '\\added_space_bottom ' + result.group(1)
113             new = '\\added_space_bottom ' + str(float(result.group(1))) + 'cm'
114             lines[i] = string.replace(lines[i], old, new)
115
116         result = added_space_top.search(lines[i])
117         if result:
118             old = '\\added_space_top ' + result.group(1)
119             new = '\\added_space_top ' + str(float(result.group(1))) + 'cm'
120             lines[i] = string.replace(lines[i], old, new)
121
122 def update_inset_accent(lines):
123     pass
124
125 def remove_cursor(lines):
126     i = 0
127     while 1:
128         i = find_token(lines, '\\cursor', i)
129         if i == -1:
130             break
131         lines[i] = ''
132         i = i + 1
133
134 def add_end_document(lines):
135     lines.append('\\the_end')
136
137 def header_update(lines):
138     i = 0
139     l = len(lines)
140     while i < l:
141         if check_token(lines[i], '\\begin_preamble'):
142             i = find_token(lines, '\\end_preamble', i)
143             if i == -1:
144                 sys.stderr.write('Unfinished preamble')
145                 sys.exit(1)
146             i = i + 1
147             continue
148
149         if lines[i][-1:] == ' ':
150             lines[i] = lines[i][:-1]
151
152         if check_token(lines[i], '\\epsfig'):
153             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
154             i = i + 1
155             continue
156
157         if check_token(lines[i], '\\papersize'):
158             size = string.split(lines[i])[1]
159             if size == 'usletter':
160                 lines[i] = '\\papersize letterpaper'
161             i = i + 1
162             continue
163
164         if check_token(lines[i], '\\baselinestretch'):
165             size = string.split(lines[i])[1]
166             if size == '1.00':
167                 name = 'single'
168             elif size == '1.50':
169                 name = 'onehalf'
170             elif size == '2.00':
171                 name == 'double'
172             lines[i] = '\\spacing %s ' % name
173             i = i + 1
174             continue
175
176         i = i + 1
177
178     lines.append('\\paperpackage a4')
179     lines.append('\\use_geometry 0')
180     lines.append('\\use_amsmath 0')
181     lines.append('\\paperorientation portrait')
182
183
184 def convert(header,body):
185     header_update(header)
186     add_end_document(body)
187     remove_cursor(body)
188     final_dot(body)
189     update_inset_label(body)
190     update_latexdel(body)
191     update_space_units(body)
192     update_inset_accent(body)
193     space_before_layout(body)
194     formula_inset_space_eat(body)
195     update_tabular(body)
196     update_vfill(body)
197
198 if __name__ == "__main__":
199     pass
200