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