]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_1_5.py
fix bug 2026 and bug 2088
[lyx.git] / lib / lyx2lyx / lyx_1_1_5.py
1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-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_token_backwards, find_re, get_layout
22
23
24 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
25
26 def replace_protected_separator(file):
27     lines = file.body
28     i=0
29     while 1:
30         i = find_token(lines, "\\protected_separator", i)
31         if i == -1:
32             break
33         j = find_token_backwards(lines, "\\layout", i)
34         #if j == -1: print error
35         layout = get_layout(lines[j], file.default_layout)
36
37         if layout == "LyX-Code":
38             result = ""
39             while lines[i] == "\\protected_separator ":
40                 result = result + " "
41                 del lines[i]
42
43             lines[i-1] = lines[i-1] + result + lines[i]
44         else:
45             lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
46
47         del lines[i]
48
49
50 def merge_formula_inset(file):
51     lines = file.body
52     i=0
53     while 1:
54         i = find_token(lines, "\\begin_inset Formula", i)
55         if i == -1: break
56         if lines[i+1] in math_env:
57             lines[i] = lines[i] + lines[i+1]
58             del lines[i+1]
59         i = i + 1
60
61
62 # Update from tabular format 4 to 5 if necessary
63 def update_tabular(file):
64     lines = file.body
65     lyxtable_re = re.compile(r".*\\LyXTable$")
66     i=0
67     while 1:
68         i = find_re(lines, lyxtable_re, i)
69         if i == -1:
70             break
71         i = i + 1
72         format = lines[i][8]
73         if format != '4':
74             continue
75
76         lines[i]='multicol5'
77         i = i + 1
78         rows = int(string.split(lines[i])[0])
79         columns = int(string.split(lines[i])[1])
80
81         i = i + rows + 1
82         for j in range(columns):
83             col_info = string.split(lines[i])
84             if len(col_info) == 3:
85                 lines[i] = lines[i] + '"" ""'
86             else:
87                 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
88             i = i + 1
89
90         while lines[i]:
91             lines[i] = lines[i] + ' "" ""'
92             i = i + 1
93
94
95 def update_toc(file):
96     lines = file.body
97     i = 0
98     while 1:
99         i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
100         if i == -1:
101             break
102         lines[i] = lines[i] + '{}'
103         i = i + 1
104
105
106 def remove_cursor(file):
107     lines = file.body
108     i = find_token(lines, '\\cursor', 0)
109     if i != -1:
110         del lines[i]
111
112
113 def remove_vcid(file):
114     lines = file.header
115     i = find_token(lines, '\\lyxvcid', 0)
116     if i != -1:
117         del lines[i]
118     i = find_token(lines, '\\lyxrcsid', 0)
119     if i != -1:
120         del lines[i]
121
122
123 def first_layout(file):
124     lines = file.body
125     while (lines[0] == ""):
126         del lines[0]
127     if lines[0][:7] != "\\layout":
128         lines[:0] = ['\\layout %s' % file.default_layout, '']
129
130
131 def remove_space_in_units(file):
132     lines = file.header
133     margins = ["\\topmargin","\\rightmargin",
134                "\\leftmargin","\\bottommargin"]
135
136     unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
137
138     for margin in margins:
139         i = 0
140         while 1:
141             i = find_token(lines, margin, i)
142             if i == -1:
143                 break
144
145             result = unit_rexp.search(lines[i])
146             if result:
147                 lines[i] = margin + " " + result.group(1) + result.group(2)
148             i = i + 1
149
150
151 def latexdel_getargs(file, i):
152     lines = file.body
153
154     # play safe, clean empty lines
155     while 1:
156         if lines[i]:
157             break
158         del lines[i]
159
160     j = find_token(lines, '\\end_inset', i)
161
162     if i == j:
163         del lines[i]
164     else:
165         file.warning("Unexpected end of inset.")
166     j = find_token(lines, '\\begin_inset LatexDel }{', i)
167
168     ref = string.join(lines[i:j])
169     del lines[i:j + 1]
170
171     # play safe, clean empty lines
172     while 1:
173         if lines[i]:
174             break
175         del lines[i]
176
177     j = find_token(lines, '\\end_inset', i - 1)
178     if i == j:
179         del lines[i]
180     else:
181         file.warning("Unexpected end of inset.")
182     j = find_token(lines, '\\begin_inset LatexDel }', i)
183     label = string.join(lines[i:j])
184     del lines[i:j + 1]
185
186     return ref, label
187
188
189 def update_ref(file):
190     lines = file.body
191     i = 0
192     while 1:
193         i = find_token(lines, '\\begin_inset LatexCommand', i)
194         if i == -1:
195             return
196
197         if string.split(lines[i])[-1] == "\\ref{":
198             i = i + 1
199             ref, label = latexdel_getargs(file, i)
200             lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label)
201
202         i = i + 1
203
204
205 def update_latexdel(file):
206     lines = file.body
207     i = 0
208     latexdel_re = re.compile(r".*\\begin_inset LatexDel")
209     while 1:
210         i = find_re(lines, latexdel_re, i)
211         if i == -1:
212             return
213         lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
214
215         j = string.find(lines[i],'\\begin_inset')
216         lines.insert(i+1, lines[i][j:])
217         lines[i] = string.strip(lines[i][:j])
218         i = i + 1
219
220         if string.split(lines[i])[-1] in ("\\url{", "\\htmlurl{"):
221             i = i + 1
222
223             ref, label = latexdel_getargs(file, i)
224             lines[i -1] = "%s[%s]{%s}" % (lines[i-1][:-1], label, ref)
225
226         i = i + 1
227
228
229 convert = [[216, [first_layout, remove_vcid, remove_cursor, update_toc,
230                   replace_protected_separator, merge_formula_inset,
231                   update_tabular, remove_space_in_units, update_ref, update_latexdel]]]
232 revert  = []
233
234 if __name__ == "__main__":
235     pass