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