]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_218.py
remove noload/don't typeset
[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                  "wide false",
29                  "collapsed false"],
30     "tab":      ["\\begin_inset Float table",
31                  "wide false",
32                  "collapsed false"],
33     "alg":      ["\\begin_inset Float algorithm",
34                  "wide false",
35                  "collapsed false"],
36     "wide-fig": ["\\begin_inset Float figure",
37                  "wide true",
38                  "collapsed false"],
39     "wide-tab": ["\\begin_inset Float table",
40                  "wide true",
41                  "collapsed false"]
42 }
43
44 font_tokens = ["\\family", "\\series", "\\shape", "\\size", "\\emph",
45                "\\bar", "\\noun", "\\color", "\\lang", "\\latex"]
46
47 #
48 # Change \begin_float .. \end_float into \begin_inset Float .. \end_inset
49 #
50
51 def remove_oldfloat(lines, language):
52     i = 0
53     while 1:
54         i = find_token(lines, "\\begin_float", i)
55         if i == -1:
56             break
57         # There are no nested floats, so finding the end of the float is simple
58         j = find_token(lines, "\\end_float", i+1)
59
60         floattype = string.split(lines[i])[1]
61         if not floats.has_key(floattype):
62             sys.stderr.write("Error! Unknown float type "+floattype+"\n")
63             floattype = "fig"
64
65         # skip \end_deeper tokens
66         i2 = i+1
67         while check_token(lines[i2], "\\end_deeper"):
68             i2 = i2+1
69         if i2 > i+1:
70             j2 = get_next_paragraph(lines, j+1)
71             lines[j2:j2] = ["\\end_deeper "]*(i2-(i+1))
72
73         new = floats[floattype]+[""]
74         new = new+lines[i2:j]+["\\end_inset ", ""]
75
76         # After a float, all font attributes are reseted.
77         # We need to output '\foo default' for every attribute foo
78         # whose value is not default before the float.
79         # The check here is not accurate, but it doesn't matter
80         # as extra '\foo default' commands are ignored.
81         # In fact, it might be safer to output '\foo default' for all 
82         # font attributes.
83         k = get_paragraph(lines, i)
84         flag = 0
85         for token in font_tokens:
86             if find_token(lines, token, k, i) != -1:
87                 if not flag:
88                     # This is not necessary, but we want the output to be
89                     # as similar as posible to the lyx format
90                     flag = 1
91                     new.append("")
92                 if token == "\\lang":
93                     new.append(token+" "+language)
94                 else:
95                     new.append(token+" default ")
96
97         lines[i:j+1] = new
98         i = i+1
99
100 def remove_oldminipage(lines):
101     i = 0
102     flag = 0
103     while 1:
104         i = find_token(lines, "\\pextra_type 2", i)
105         if i == -1:
106             break
107         hfill = 0
108         line = string.split(lines[i])
109         if line[4] == "\\pextra_hfill":
110             hfill = 1
111             line[4:6] = []
112         if line[4] == "\\pextra_start_minipage":
113             # We just ignore this
114             line[4:6] = []
115
116         position = line[3]
117         width = line[5]
118         if line[4] == "\\pextra_widthp":
119             width = line[5]+"col%"
120         
121
122         start = ["\\begin_inset Minipage",
123                  "position " + position,
124                  "inner_position 0",
125                  'height "0pt"',
126                  'width "%s"' % width,
127                  "collapsed false"
128                  ]
129         if flag:
130             flag = 0
131             if hfill:
132                 start = ["","\hfill",""]+start
133         else:
134             start = ["\\layout Standard"] + start
135
136         j = find_token_backwards(lines,"\\layout", i-1)
137         j0 = j
138         mid = lines[j:i]
139
140         j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
141         # j can be -1, but this is still ok
142         mid = mid+lines[i+1:j]
143
144         count = 0
145         while 1:
146             # collect more paragraphs to the minipage
147             count = count+1
148             if j == -1 or not check_token(lines[j], "\\layout"):
149                 break
150             i = find_tokens(lines, ["\\layout", "\\pextra_type"], j+1)
151             if i == -1 or not check_token(lines[i], "\\pextra_type"):
152                 break
153             line = string.split(lines[i])
154             if line[4] == "\\pextra_hfill":
155                 line[4:6] = []
156             if line[4] == "\\pextra_start_minipage" and line[5] == "1":
157                 flag = 1
158                 break
159             j = find_token_backwards(lines,"\\layout", i-1)
160             mid = mid+lines[j:i]
161             j = find_tokens(lines, ["\\layout", "\\end_float"], i+1)
162             mid = mid+lines[i+1:j]
163
164         end = ["\\end_inset "]
165
166         lines[j0:j] = start+mid+end
167         i = i+1
168
169 def is_empty(lines):
170     return filter(is_nonempty_line, lines) == []
171
172 font_rexp =  re.compile(r"\\(family|series|shape|size|emph|numeric|bar|noun)")
173 ert_rexp = re.compile(r"\\begin_inset|.*\\SpecialChar")
174 spchar_rexp = re.compile(r"(.*)(\\SpecialChar.*)")
175 ert_begin = ["\\begin_inset ERT",
176              "status Collapsed",
177              "",
178              "\\layout Standard"]
179
180 def remove_oldert(lines):
181     i = 0
182     while 1:
183         i = find_tokens(lines, ["\\latex latex", "\\layout LaTeX"], i)
184         if i == -1:
185             break
186         j = i+1
187         while 1:
188             j = find_tokens(lines, ["\\latex default", "\\begin_inset", "\\layout", "\\end_float", "\\the_end"],
189                             j)
190             if check_token(lines[j], "\\begin_inset"):
191                 j = find_end_of_inset(lines, j)
192             else:
193                 break
194
195         if check_token(lines[j], "\\layout"):
196             while j-1 >= 0 and check_token(lines[j-1], "\\begin_deeper"):
197                 j = j-1
198
199         # We need to remove insets, special chars & font commands from ERT text
200         new = []
201         new2 = []
202         if check_token(lines[i], "\\layout LaTeX"):
203             new = ["\layout Standard", "", ""]
204             # We have a problem with classes in which Standard is not the default layout!
205
206         k = i+1
207         while 1:
208             k2 = find_re(lines, ert_rexp, k, j)
209             inset = specialchar = 0
210             if k2 == -1:
211                 k2 = j
212             elif check_token(lines[k2], "\\begin_inset"):
213                 inset = 1
214             else:
215                 specialchar = 1
216                 mo = spchar_rexp.match(lines[k2])
217                 lines[k2] = mo.group(1)
218                 specialchar_str = mo.group(2)
219                 k2 = k2+1
220
221             tmp = []
222             for line in lines[k:k2]:
223                 if font_rexp.match(line):
224                     if new2 == []:
225                         # This is not necessary, but we want the output to be
226                         # as similar as posible to the lyx format
227                         new2 = [""]
228                     new2.append(line)
229                 elif not check_token(line, "\\latex"):
230                     tmp.append(line)
231
232             if is_empty(tmp):
233                 if filter(lambda x:x != "", tmp) != []:
234                     if new == []:
235                         # This is not necessary, but we want the output to be
236                         # as similar as posible to the lyx format
237                         lines[i-1] = lines[i-1]+" "
238                     else:
239                         new = new+[" "]
240             else:
241                 new = new+ert_begin+tmp+["\\end_inset ", ""]
242
243             if inset:
244                 k3 = find_end_of_inset(lines, k2)
245                 new = new+[""]+lines[k2:k3+1]+[""] # Put an empty line after \end_inset
246                 k = k3+1
247                 # Skip the empty line after \end_inset
248                 if not is_nonempty_line(lines[k]):
249                     k = k+1
250                     new.append("")
251             elif specialchar:
252                 if new == []:
253                     # This is not necessary, but we want the output to be
254                     # as similar as posible to the lyx format
255                     lines[i-1] = lines[i-1]+specialchar_str
256                     new = [""]
257                 else:
258                     new = new+[specialchar_str, ""]
259                 k = k2
260             else:
261                 break
262
263         new = new+new2
264         if not check_token(lines[j], "\\latex "):
265             new = new+[""]+[lines[j]]
266         lines[i:j+1] = new
267         i = i+1
268
269     i = 0
270     while 1:
271         i = find_token(lines, "\\latex ", i)
272         if i == -1:
273             break
274         del lines[i]
275
276
277 def remove_oldertinset(lines):
278     i = 0
279     while 1:
280         i = find_token(lines, "\\begin_inset ERT", i)
281         if i == -1:
282             break
283         j = find_end_of_inset(lines, i)
284         k = find_token(lines, "\\layout", i+1)
285         l = get_paragraph(lines, i)
286         if lines[k] == lines[l]: # same layout
287             k = k+1
288         new = lines[k:j]
289         lines[i:j+1] = new
290         i = i+1
291
292 def is_ert_paragraph(lines, i):
293     i = find_nonempty_line(lines, i+1)
294     if not check_token(lines[i], "\\begin_inset ERT"):
295         return 0
296     j = find_end_of_inset(lines, i)
297     k = find_nonempty_line(lines, j+1)
298     return check_token(lines[k], "\\layout")
299
300 def combine_ert(lines):
301     i = 0
302     while 1:
303         i = find_token(lines, "\\begin_inset ERT", i)
304         if i == -1:
305             break
306         j = find_token_backwards(lines,"\\layout", i-1)
307         count = 0
308         text = []
309         while is_ert_paragraph(lines, j):
310
311             count = count+1
312             i2 = find_token(lines, "\\layout", j+1)
313             k = find_token(lines, "\\end_inset", i2+1)
314             text = text+lines[i2:k]
315             j = find_token(lines, "\\layout", k+1)
316             if j == -1:
317                 break
318
319         if count >= 2:
320             j = find_token(lines, "\\layout", i+1)
321             lines[j:k] = text
322
323         i = i+1
324         
325 oldunits = ["pt", "cm", "in", "text%", "col%"]
326
327 def get_length(lines, name, start, end):
328     i = find_token(lines, name, start, end)
329     if i == -1:
330         return ""
331     x = string.split(lines[i])
332     return x[2]+oldunits[int(x[1])]
333
334 def write_attribute(x, token, value):
335     if value != "":
336         x.append("\t"+token+" "+value)
337
338 def remove_figinset(lines):
339     i = 0
340     while 1:
341         i = find_token(lines, "\\begin_inset Figure", i)
342         if i == -1:
343             break
344         j = find_end_of_inset(lines, i)
345
346         lyxwidth = string.split(lines[i])[3]+"pt"
347         lyxheight = string.split(lines[i])[4]+"pt"
348
349         filename = get_value(lines, "file", i+1, j)
350
351         width = get_length(lines, "width", i+1, j)
352         # what does width=5 mean ?
353         height = get_length(lines, "height", i+1, j)
354         rotateAngle = get_value(lines, "angle", i+1, j)
355         if width == "" and height == "":
356             size_type = "0"
357         else:
358             size_type = "1"
359
360         flags = get_value(lines, "flags", i+1, j)
361         x = int(flags)%4
362         if x == 1:
363             display = "monochrome"
364         elif x == 2:
365             display = "gray"
366         else:
367             display = "color"
368
369         subcaptionText = get_value(lines, "subcaption", i+1, j)
370         if subcaptionText != "":
371             subcaptionText = '"'+subcaptionText+'"'
372         k = find_token(lines, "subfigure", i+1,j)
373         if k == -1:
374             subcaption = 0
375         else:
376             subcaption = 1
377
378         new = ["\\begin_inset Graphics FormatVersion 1"]
379         write_attribute(new, "filename", filename)
380         write_attribute(new, "display", display)
381         if subcaption:
382             new.append("\tsubcaption")
383         write_attribute(new, "subcaptionText", subcaptionText)
384         write_attribute(new, "size_type", size_type)
385         write_attribute(new, "width", width)
386         write_attribute(new, "height", height)
387         if rotateAngle != "":
388             new.append("\trotate")
389             write_attribute(new, "rotateAngle", rotateAngle)
390         write_attribute(new, "rotateOrigin", "leftBaseline")
391         write_attribute(new, "lyxsize_type", "1")
392         write_attribute(new, "lyxwidth", lyxwidth)
393         write_attribute(new, "lyxheight", lyxheight)
394         new = new + ["\end_inset"]
395         lines[i:j+1] = new
396
397 attr_re = re.compile(r' \w*="(false|0|)"')
398 line_re = re.compile(r'<(features|column|row|cell)')
399
400 def update_tabular(lines):
401     i = 0
402     while 1:
403         i = find_token(lines, '\\begin_inset  Tabular', i)
404         if i == -1:
405             break
406
407         for k in get_tabular_lines(lines, i):
408             if check_token(lines[k], "<lyxtabular"):
409                 lines[k] = string.replace(lines[k], 'version="2"', 'version="3"')
410             elif check_token(lines[k], "<column"):
411                 lines[k] = string.replace(lines[k], 'width=""', 'width="0pt"')
412
413             if line_re.match(lines[k]):
414                 lines[k] = re.sub(attr_re, "", lines[k])
415
416         i = i+1
417
418 def change_preamble(lines):
419     i = find_token(lines, "\\use_amsmath", 0)
420     if i == -1:
421         return
422     lines[i+1:i+1] = ["\\use_natbib 0",
423                       "\use_numerical_citations 0"]
424
425 def convert(header, body):
426     language = get_value(header, "\\language", 0)
427     if language == "":
428         language = "english"
429
430     change_preamble(header)
431     update_tabular(body)
432     remove_oldminipage(body)
433     remove_oldfloat(body, language)
434     remove_figinset(body)
435     remove_oldertinset(body)
436     remove_oldert(body)
437     combine_ert(body)
438
439 if __name__ == "__main__":
440     pass