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