]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_1_5.py
* LyX.py
[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 # Private helper functions
25
26 def get_layout(line, default_layout):
27     tokens = string.split(line)
28     if len(tokens) > 1:
29         return tokens[1]
30     return default_layout
31
32
33 ####################################################################
34
35 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
36
37 def replace_protected_separator(file):
38     lines = file.body
39     i=0
40     while 1:
41         i = find_token(lines, "\\protected_separator", i)
42         if i == -1:
43             break
44         j = find_token_backwards(lines, "\\layout", i)
45         #if j == -1: print error
46         layout = get_layout(lines[j], file.default_layout)
47
48         if layout == "LyX-Code":
49             result = ""
50             while lines[i] == "\\protected_separator ":
51                 result = result + " "
52                 del lines[i]
53
54             lines[i-1] = lines[i-1] + result + lines[i]
55         else:
56             lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
57
58         del lines[i]
59
60
61 def merge_formula_inset(file):
62     lines = file.body
63     i=0
64     while 1:
65         i = find_token(lines, "\\begin_inset Formula", i)
66         if i == -1: break
67         if lines[i+1] in math_env:
68             lines[i] = lines[i] + lines[i+1]
69             del lines[i+1]
70         i = i + 1
71
72
73 # Update from tabular format 4 to 5 if necessary
74 def update_tabular(file):
75     lines = file.body
76     lyxtable_re = re.compile(r".*\\LyXTable$")
77     i=0
78     while 1:
79         i = find_re(lines, lyxtable_re, i)
80         if i == -1:
81             break
82         i = i + 1
83         format = lines[i][8]
84         if format != '4':
85             continue
86
87         lines[i]='multicol5'
88         i = i + 1
89         rows = int(string.split(lines[i])[0])
90         columns = int(string.split(lines[i])[1])
91
92         i = i + rows + 1
93         for j in range(columns):
94             col_info = string.split(lines[i])
95             if len(col_info) == 3:
96                 lines[i] = lines[i] + '"" ""'
97             else:
98                 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
99             i = i + 1
100
101         while lines[i]:
102             lines[i] = lines[i] + ' "" ""'
103             i = i + 1
104
105
106 def update_toc(file):
107     lines = file.body
108     i = 0
109     while 1:
110         i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
111         if i == -1:
112             break
113         lines[i] = lines[i] + '{}'
114         i = i + 1
115
116
117 def remove_cursor(file):
118     lines = file.body
119     i = find_token(lines, '\\cursor', 0)
120     if i != -1:
121         del lines[i]
122
123
124 def remove_vcid(file):
125     lines = file.header
126     i = find_token(lines, '\\lyxvcid', 0)
127     if i != -1:
128         del lines[i]
129     i = find_token(lines, '\\lyxrcsid', 0)
130     if i != -1:
131         del lines[i]
132
133
134 def first_layout(file):
135     lines = file.body
136     while (lines[0] == ""):
137         del lines[0]
138     if lines[0][:7] != "\\layout":
139         lines[:0] = ['\\layout %s' % file.default_layout, '']
140
141
142 def remove_space_in_units(file):
143     lines = file.header
144     margins = ["\\topmargin","\\rightmargin",
145                "\\leftmargin","\\bottommargin"]
146
147     unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
148
149     for margin in margins:
150         i = 0
151         while 1:
152             i = find_token(lines, margin, i)
153             if i == -1:
154                 break
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