]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_217.py
Convert tabular version 2 -> version 3
[lyx.git] / lib / lyx2lyx / lyxconvert_217.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 José Matos <jamatos@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 import re, string, sys
19 from parser_tools import *
20
21 bool_table =  {"0": "false", "1":"true" }
22 align_table = {"0": "top", "2": "left", "4": "right", "8": "center"}
23 use_table = {"0": "none", "1": "parbox"}
24
25 #table_meta_re = re.compile(r'<LyXTabular version="1" rows="(\d*)" columns="(\d*)">')
26 def update_tabular(lines):
27     i=0
28     while 1:
29         i = find_token(lines, '\\begin_inset  Tabular', i)
30         if i == -1:
31             break
32
33         i = i +1
34
35         # scan table header meta-info
36         lines[i] = string.replace(lines[i], 'LyXTabular version="1"', 'lyxtabular version="2"')
37
38         j = find_token(lines, '</LyXTabular>', i) + 1
39         if j == 0:
40             sys.stderr.write( "Error: Bad lyx format i=%d j=%d\n" % (i,j))
41             break
42
43         new_table = table_update(lines[i:j])
44         tail = lines[j:]
45         lines[i:] = []
46         lines.extend(new_table)
47         lines.extend(tail)
48         i = i + len(new_table)
49
50 col_re = re.compile(r'<column alignment="(\d)" valignment="(\d)" leftline="(\d)" rightline="(\d)" width="(.*)" special="(.*)">')
51 cell_re = re.compile(r'<cell multicolumn="(\d)" alignment="(\d)" valignment="(\d)" topline="(\d)" bottomline="(\d)" leftline="(\d)" rightline="(\d)" rotate="(\d)" usebox="(\d)" width="(.*)" special="(.*)">')
52 features_re = re.compile(r'<features rotate="(\d)" islongtable="(\d)" endhead="(\d)" endfirsthead="(\d)" endfoot="(\d)" endlastfoot="(\d)">')
53 row_re = re.compile(r'<row topline="(\d)" bottomline="(\d)" newpage="(\d)">')
54
55 def table_update(lines):
56     lines[1] = string.replace(lines[1], '<Features', '<features')
57     res = features_re.match( lines[1] )
58     if res:
59         val = res.groups()
60         lines[1] = '<features rotate="%s" islongtable="%s" endhead="%s" endfirsthead="%s" endfoot="%s" endlastfoot="%s">' % (bool_table[val[0]], bool_table[val[1]], val[2], val[3], val[4], val[5])
61         
62     if lines[2]=="":
63         del lines[2]
64     i = 2
65     col_info = []
66     while i < len(lines):
67         lines[i] = string.replace(lines[i], '<Cell', '<cell')
68         lines[i] = string.replace(lines[i], '</Cell', '</cell')
69         lines[i] = string.replace(lines[i], '<Row', '<row')
70         lines[i] = string.replace(lines[i], '</Row', '</row')
71         lines[i] = string.replace(lines[i], '<Column', '<column')
72         lines[i] = string.replace(lines[i], '</Column', '</column')
73         lines[i] = string.replace(lines[i], '</LyXTabular', '</lyxtabular')
74         k = string.find (lines[i], '<column ')
75         if k != -1:
76             col_info.append(lines[i])
77             del lines[i]
78             continue
79         
80         if lines[i] == '</column>' or lines[i] == '<column>':
81             del lines[i]
82             continue
83
84         res = cell_re.match(lines[i])
85         if res:
86             val = res.groups()
87             lines[i] = '<cell multicolumn="%s" alignment="%s" valignment="%s" topline="%s" bottomline="%s" leftline="%s" rightline="%s" rotate="%s" usebox="%s" width="%s" special="%s">' % ( val[0], align_table[val[1]], align_table[val[2]], bool_table[val[3]], bool_table[val[4]], bool_table[val[5]], bool_table[val[6]], bool_table[val[7]], use_table[val[8]], val[9], val[10])
88
89         res = row_re.match(lines[i])
90         if res:
91             val = res.groups()
92             lines[i] = '<row topline="%s" bottomline="%s" newpage="%s">' % (bool_table[val[0]], bool_table[val[1]], bool_table[val[2]])
93
94         i = i + 1
95
96     j = len(col_info)
97     for i in range(j):
98         res = col_re.match(col_info[i])
99         if res:
100             val = res.groups()
101             col_info[i] = '<column alignment="%s" valignment="%s" leftline="%s" rightline="%s" width="%s" special="%s">' \
102                           % ( align_table[val[0]], align_table[val[1]], bool_table[val[2]], bool_table[val[3]], val[4],val[5])
103
104     return lines[:2] + col_info + lines[2:]
105
106 def convert(header,body):
107     update_tabular(body)
108
109 if __name__ == "__main__":
110     pass
111