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