]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_218.py
Small changes
[lyx.git] / lib / lyx2lyx / lyxconvert_218.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18
19 import sys,string,re
20 from parser_tools import *
21
22 floats = {
23     "footnote": ["\\begin_inset Foot",
24                  "collapsed true"],
25     "margin":   ["\\begin_inset Marginal",
26                  "collapsed true"],
27     "fig":      ["\\begin_inset Float figure",
28                  "placement htbp",
29                  "wide false",
30                  "collapsed false"],
31     "tab":      ["\\begin_inset Float table",
32                  "placement htbp",
33                  "wide false",
34                  "collapsed false"],
35     "alg":      ["\\begin_inset Float algorithm",
36                  "placement htbp",
37                  "wide false",
38                  "collapsed false"],
39     "wide-fig": ["\\begin_inset Float figure",
40                  "placement htbp",
41                  "wide true",
42                  "collapsed false"],
43     "wide-tab": ["\\begin_inset Float table",
44                  "placement htbp",
45                  "wide true",
46                  "collapsed false"]
47 }
48
49 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
50                "\\bar", "\\noun", "\\color", "\\lang"]
51
52 #
53 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
54 #
55
56 def remove_oldfloat(lines, language):
57     i = 0
58     while 1:
59         i = find_token(lines, "\\begin_float", i)
60         if i == -1:
61             break
62         j = find_token(lines, "\\end_float", i+1)
63         floattype = string.split(lines[i])[1]
64         if not floats.has_key(floattype):
65             sys.stderr.write("Error! Unknown float type "+floattype+"\n")
66             floattype = "fig"
67
68         # skip \end_deeper tokens
69         i2 = i+1
70         while check_token(lines[i2], "\\end_deeper"):
71             i2 = i2+1
72         if i2 > i+1:
73             j2 = find_token(lines, "\\layout", j+1)
74             lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
75
76         new = floats[floattype]+[""]
77         new = new+lines[i2:j]+["\\end_inset ", ""]
78         # After a float, all font attribute are reseted.
79         # We need to output '\foo default' for every attribute foo
80         # whose value is not default before the float.
81         # The check here is not accurate, but it doesn't matter
82         # as extra '\foo default' commands are ignored.
83         # In fact, it might be safer to output '\foo default' for all 
84         # font attributes.
85         k = get_paragraph(lines, i)
86         flag = 0
87         for token in font_tokens:
88             if find_token(lines, token, k, i) != -1:
89                 if not flag:
90                     # This is not necessary, but we want the output to be
91                     # as similar as posible to the lyx format
92                     flag = 1
93                     new.append("")
94                 if token == "\\lang":
95                     new.append(token+" "+language+" ")
96                 else:
97                     new.append(token+" default ")
98         lines[i:j+1]= new
99
100         i = i+1
101
102 def remove_oldminipage(lines):
103     i = 0
104     flag = 0
105     while 1:
106         i = find_token(lines, "\\pextra_type 2", i)
107         if i == -1:
108             break
109         hfill = 0
110         line = string.split(lines[i])
111         if line[4] == "\\pextra_hfill":
112             hfill = 1
113             line[4:6] = []
114         if line[4] == "\\pextra_start_minipage":
115             # We just ignore this
116             line[4:6] = []
117
118         position = line[3]
119         width = line[5]
120         if line[4] == "\\pextra_widthp":
121             width = line[5]+"col%"
122         
123
124         start = ["\\begin_inset Minipage",
125                  "position " + position,
126                  "inner_position 0",
127                  'height "0pt"',
128                  'width "%s"' % width,
129                  "collapsed false"
130                  ]
131         if flag:
132             flag = 0
133             if hfill:
134                 start = ["","\hfill",""]+start
135         else:
136             start = ["\\layout Standard"] + start
137
138         j = find_token_backwards(lines,"\\layout", i-1)
139         j0 = j
140         mid = lines[j:i]
141
142         j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
143         # j can be -1, but this is still ok
144         mid = mid+lines[i+1:j]
145
146         count = 0
147         while 1:
148             # collect more paragraphs to the minipage
149             count = count+1
150             if j == -1 or not check_token(lines[j], "\\layout"):
151                 break
152             i = find_tokens(lines, ["\\layout", "\\pextra_type"], j+1)
153             if i == -1 or not check_token(lines[i], "\\pextra_type"):
154                 break
155             line = string.split(lines[i])
156             if line[4] == "\\pextra_hfill":
157                 line[4:6] = []
158             if line[4] == "\\pextra_start_minipage" and line[5] == "1":
159                 flag = 1
160                 break
161             j = find_token_backwards(lines,"\\layout", i-1)
162             mid = mid+lines[j:i]
163             j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
164             mid = mid+lines[i+1:j]
165
166         end = ["\\end_inset "]
167
168         lines[j0:j] = start+mid+end
169         i = i+1
170
171 def is_empty(lines):
172     return filter(is_nonempty_line, lines) == []
173
174 font_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
175 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
176 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
177 ert_begin = ["\\begin_inset ERT",
178              "status Collapsed",
179              "",
180              "\\layout Standard"]
181
182 def remove_oldert(lines):
183     i = 0
184     while 1:
185         i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
186         if i == -1:
187             break
188         j = find_tokens(lines, ["\\latex default", "\\layout", "\\end_float", "\\the_end"],
189                         i+1)
190         if check_token(lines[j], "\\layout"):
191             while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
192                 j = j-1
193
194         # We need to remove insets, special chars & font commands from ERT text
195         new = []
196         new2 = []
197         if check_token(lines[i], "\\layout LaTeX"):
198             new = ["\layout Standard", "", ""]
199             # We have a problem with classes in which Standard is not the default layout!
200
201         k = i+1
202         while 1:
203             k2 = find_re(lines, ert_rexp, k, j)
204             inset = specialchar = 0
205             if k2 == -1:
206                 k2 = j
207             elif check_token(lines[k2], "\\begin_inset"):
208                 inset = 1
209             else:
210                 specialchar = 1
211                 mo = spchar_rexp.match(lines[k2])
212                 lines[k2] = mo.group(1)
213                 specialchar_str = mo.group(2)
214                 k2 = k2+1
215
216             tmp = []
217             for line in lines[k:k2]:
218                 if font_rexp.match(line):
219                     if new2 == []:
220                         # This is not necessary, but we want the output to be
221                         # as similar as posible to the lyx format
222                         new2 = [""]
223                     new2.append(line)
224                 else:
225                     tmp.append(line)
226
227             if is_empty(tmp):
228                 if filter(lambda x:x != "", tmp) != []:
229                     if new == []:
230                         # This is not necessary, but we want the output to be
231                         # as similar as posible to the lyx format
232                         lines[i-1] = lines[i-1]+" "
233                     else:
234                         new = new+[" "]
235             else:
236                 new = new+ert_begin+tmp+["\\end_inset ", ""]
237
238             if inset:
239                 k3 = find_token(lines, "\\end_inset", k2+1)
240                 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
241                 k = k3+1
242                 # Skip the empty line after \end_inset
243                 if not is_nonempty_line(lines[k]):
244                     k = k+1
245                     new.append("")
246             elif specialchar:
247                 if new == []:
248                     # This is not necessary, but we want the output to be
249                     # as similar as posible to the lyx format
250                     lines[i-1] = lines[i-1]+specialchar_str
251                     new = [""]
252                 else:
253                     new = new+[specialchar_str, ""]
254                 k = k2
255             else:
256                 break
257
258         new = new+new2
259         if not check_token(lines[j], "\\latex default"):
260             new = new+[""]+[lines[j]]
261         lines[i:j+1] = new
262         i = i+1
263
264 def is_ert_paragraph(lines, i):
265     i = find_nonempty_line(lines, i+1)
266     if not check_token(lines[i], "\\begin_inset ERT"):
267         return 0
268     j = find_token(lines, "\\end_inset", i)
269     k = find_nonempty_line(lines, j+1)
270     return check_token(lines[k], "\\layout")
271
272 def combine_ert(lines):
273     i = 0
274     while 1:
275         i = find_token(lines, "\\begin_inset ERT", i)
276         if i == -1:
277             break
278         j = find_token_backwards(lines,"\\layout", i-1)
279         count = 0
280         text = []
281         while is_ert_paragraph(lines, j):
282
283             count = count+1
284             i2 = find_token(lines, "\\layout", j+1)
285             k = find_token(lines, "\\end_inset", i2+1)
286             text = text+lines[i2:k]
287             j = find_token(lines, "\\layout", k+1)
288             if j == -1:
289                 break
290
291         if count >= 2:
292             j = find_token(lines, "\\layout", i+1)
293             lines[j:k] = text
294
295         i = i+1
296         
297 oldunits = ["pt", "cm", "in", "text%", "col%"]
298
299 def get_length(lines, name, start, end):
300     i = find_token(lines, name, start, end)
301     if i == -1:
302         return ""
303     x = string.split(lines[i])
304     return x[2]+oldunits[int(x[1])]
305
306 def append(x, token, value):
307     if value != "":
308         x.append("\t"+token+" "+value)
309
310 def remove_figinset(lines):
311     i = 0
312     while 1:
313         i = find_token(lines, "\\begin_inset Figure", i)
314         if i == -1:
315             break
316         j = find_token(lines, "\\end_inset", i+1)
317
318         lyxwidth = string.split(lines[i])[3]+"pt"
319         lyxheight = string.split(lines[i])[4]+"pt"
320
321         filename = get_value(lines, "file", i+1, j)
322
323         width = get_length(lines, "width", i+1, j)
324         # what does width=5 mean ?
325         height = get_length(lines, "height", i+1, j)
326         rotateAngle = get_value(lines, "angle", i+1, j)
327         if width == "" and height == "":
328             size_type = "0"
329         else:
330             size_type = "1"
331
332         flags = get_value(lines, "flags", i+1, j)
333         x = int(flags)%4
334         if x == 1:
335             display = "monochrome"
336         elif x == 2:
337             display = "gray"
338         else:
339             display = "color"
340
341         subcaptionText = get_value(lines, "subcaption", i+1, j)
342         if subcaptionText != "":
343             subcaptionText = '"'+subcaptionText+'"'
344         k = find_token(lines, "subfigure", i+1,j)
345         if k == -1:
346             subcaption = 0
347         else:
348             subcaption = 1
349
350         new = ["\\begin_inset Graphics FormatVersion 1"]
351         append(new, "filename", filename)
352         append(new, "display", display)
353         if subcaption:
354             new.append("\tsubcaption")
355         append(new, "subcaptionText", subcaptionText)
356         append(new, "size_type", size_type)
357         append(new, "width", width)
358         append(new, "height", height)
359         if rotateAngle != "":
360             new.append("\trotate")
361             append(new, "rotateAngle", rotateAngle)
362         new.append("\trotateOrigin center")
363         new.append("\tlyxsize_type 1")
364         append(new, "lyxwidth", lyxwidth)
365         append(new, "lyxheight", lyxheight)
366         new = new + ["\end_inset"]
367         lines[i:j+1] = new
368
369 def change_preamble(lines):
370     i = find_token(lines, "\\use_amsmath", 0)
371     if i == -1:
372         return
373     lines[i+1:i+1] = ["\\use_natbib 0",
374                       "\use_numerical_citations 0"]
375
376 def convert(header, body):
377     language = get_value(header, "\\language", 0)
378     if language == "":
379         language = "english"
380
381     change_preamble(header)
382     remove_oldert(body)
383     combine_ert(body)
384     remove_oldminipage(body)
385     remove_oldfloat(body, language)
386     remove_figinset(body)
387
388 if __name__ == "__main__":
389     pass