]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_210.py
Georg Baum's patch
[lyx.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] == '':
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:
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     while 1:
129         i = find_token(lines, '\\cursor', i)
130         if i == -1:
131             break
132         lines[i] = ''
133         i = i + 1
134
135 def add_end_document(lines):
136     lines.append('\\the_end')
137
138 def header_update(lines):
139     i = 0
140     l = len(lines)
141     while i < l:
142         if check_token(lines[i], '\\begin_preamble'):
143             i = find_token(lines, '\\end_preamble', i)
144             if i == -1:
145                 sys.stderr.write('Unfinished preamble')
146                 sys.exit(1)
147             i = i + 1
148             continue
149
150         if lines[i][-1:] == ' ':
151             lines[i] = lines[i][:-1]
152
153         if check_token(lines[i], '\\epsfig'):
154             lines[i] = string.replace(lines[i], '\\epsfig', '\\graphics')
155             i = i + 1
156             continue
157
158         if check_token(lines[i], '\\papersize'):
159             size = string.split(lines[i])[1]
160             if size == 'usletter':
161                 lines[i] = '\\papersize letterpaper'
162             i = i + 1
163             continue
164
165         if check_token(lines[i], '\\baselinestretch'):
166             size = string.split(lines[i])[1]
167             if size == '1.00':
168                 name = 'single'
169             elif size == '1.50':
170                 name = 'onehalf'
171             elif size == '2.00':
172                 name == 'double'
173             lines[i] = '\\spacing %s ' % name
174             i = i + 1
175             continue
176
177         i = i + 1
178
179     lines.append('\\paperpackage a4')
180     lines.append('\\use_geometry 0')
181     lines.append('\\use_amsmath 0')
182     lines.append('\\paperorientation portrait')
183
184
185 def convert(header,body):
186     header_update(header)
187     add_end_document(body)
188     remove_cursor(body)
189     final_dot(body)
190     update_inset_label(body)
191     update_latexdel(body)
192     update_space_units(body)
193     update_inset_accent(body)
194     space_before_layout(body)
195     formula_inset_space_eat(body)
196     update_tabular(body)
197     update_vfill(body)
198
199 if __name__ == "__main__":
200     pass
201