]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_216.py
Start to take care of the tables with continuation rows
[lyx.git] / lib / lyx2lyx / lyxconvert_216.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 lyxtable_re = re.compile(r".*\\LyXTable$")
22 def update_tabular(lines):
23     i=0
24     while 1:
25         i = find_re(lines, lyxtable_re, i)
26         if i == -1:
27             break
28         prop_dict = {"family" : "default", "series" : "default",
29                       "shape" : "default", "size" : "default",
30                       "emph" : "default", "bar" : "default",
31                       "noun" : "default", "latex" : "default", "color" : "default"}
32
33         # remove \LyXTable
34         lines[i] = lines[i][:-9]
35         i = i + 1
36         lines.insert(i,'')
37         i = i + 1
38         lines[i] = "\\begin_inset  Tabular"
39         i = i + 1
40         head = string.split(lines[i])
41         rows = int(head[0])
42         columns = int(head[1])
43
44         tabular_line = i
45         i = i +1
46         lines.insert(i, '<Features rotate="%s" islongtable="%s" endhead="%s" endfirsthead="%s" endfoot="%s" endlastfoot="%s">' % (head[2],head[3],head[4],head[5],head[6],head[7]))
47
48         i = i +1
49         
50         row_info = []
51         for j in range(rows):
52             row_info.append(string.split(lines[i]))
53             del lines[i]
54         column_info = []
55         for j in range(columns):
56             column_info.append(string.split(lines[i]))
57             del lines[i]
58
59         cell_info = []
60         ncells = 0
61         cont_row = []
62         for j in range(rows):
63             c_row = 0
64             for k in range(columns):
65                 cell_info.append(string.split(lines[i]))
66                 if cell_info[len(cell_info)-1][6] == '1':
67                     c_row = 1
68                 if lines[i][0] != "2":
69                     ncells = ncells + 1
70                 del lines[i]
71             if c_row:
72                 cont_row.append(j+1)
73
74         lines[tabular_line] = '<LyXTabular version="1" rows="%s" columns="%s">' % (rows-len(cont_row),columns)
75         del lines[i]
76         if not lines[i]:
77             del lines[i]
78         
79         tmp = []
80         tmp.append("")
81
82         l = 0
83         for j in range(rows):
84             tmp.append('<Row topline="%s" bottomline="%s" newpage="%s">' % (row_info[j][0],row_info[j][1],row_info[j][2]))
85
86             for k in range(columns):
87                 if j:
88                     tmp.append('<Column>')
89                 else:
90                     tmp.append('<Column alignment="%s" valignment="0" leftline="%s" rightline="%s" width=%s special=%s>' % (column_info[k][0],column_info[k][1], column_info[k][2], column_info[k][3], column_info[k][4]))
91                 m = j*columns + k
92
93                 leftline = int(column_info[k][1])
94                 if cell_info[m][0] == '1':
95                     n = m + 1
96                     while n < rows * columns - 1 and cell_info[n][0] == '2':
97                         n = n + 1
98                     rightline = int(column_info[k][2])
99                 else:
100                     # not a multicolumn main cell
101                     # rightline = int(cell_info[m][5]) or int(column_info[k][2])
102                     rightline = int(column_info[k][2])
103
104                 tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%d" rightline="%d" rotate="%s" usebox="%s" width=%s special=%s>' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],leftline,rightline,cell_info[m][5],cell_info[m][6],cell_info[m][7],cell_info[m][8]))
105                 tmp.append('\\begin_inset Text')
106                 tmp.append('')
107                 tmp.append('\\layout Standard')
108                 tmp.append('')
109
110                 if cell_info[m][0] == '2':
111                     tmp.append('\\end_inset ')
112                     tmp.append('</Cell>')
113                     tmp.append('</Column>')
114                     continue
115
116                 if l == ncells -1:
117                     end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i)
118                     if end == -1:
119                         sys.stderr.write("Malformed lyx file\n")
120                     else:
121                         end = end - i
122                         paragraph = []
123                         while end > 0:
124                             paragraph.append(lines[i])
125                             del lines[i]
126                             end = end -1
127                         tmp.extend(set_paragraph_properties(paragraph, prop_dict))
128                 else:
129                     paragraph = []
130                     while lines[i] != '\\newline ':
131                         paragraph.append(lines[i])
132                         del lines[i]
133                     del lines[i]
134                     tmp.extend(set_paragraph_properties(paragraph, prop_dict))
135                 
136                 tmp.append('\\end_inset ')
137                 tmp.append('</Cell>')
138                 tmp.append('</Column>')
139                 l = l + 1
140             tmp.append('</Row>')
141
142         tmp.append('</LyXTabular>')
143         tmp.append('')
144         tmp.append('\\end_inset ')
145         tmp.append('')
146         tmp.append('')
147         tail = lines[i:]
148         lines[i:] = []
149         lines.extend(tmp)
150         lines.extend(tail)
151
152         i = i + len(tmp)
153
154 prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
155 def set_paragraph_properties(lines, prop_dict):
156     # we need to preserve the order of options
157     properties = ["family","series","shape","size",
158                   "emph","bar","noun","latex","color"]
159     prop_value = {"family" : "default", "series" : "medium",
160                    "shape" : "up", "size" : "normal",
161                    "emph" : "off", "bar" : "no",
162                    "noun" : "off", "latex" : "no_latex", "color" : "none"}
163
164     start = 0
165     end = 0
166     i = 0
167     n = len(lines)
168
169     #skip empty lines
170     while i<n and lines[i] == "":
171         i = i + 1
172     start = i
173
174     #catch open char properties
175     while i<n and lines[i][:1] == "\\":
176         result = prop_exp.match(lines[i])
177         # sys.stderr.write(lines[i]+"\n")
178         prop = result.group(1)
179         if prop not in properties:
180             break
181         else:
182             prop_dict[prop] = result.group(2)
183         i = i + 1
184     end = i
185
186     aux = []
187     insert = 0
188     for prop in properties:
189         if prop_dict[prop] != 'default':
190             insert = 1
191             if prop == "color":
192                 aux.append("\\%s %s" % (prop, prop_dict[prop]))
193             elif prop != "family" or prop_dict[prop] != "roman":
194                     aux.append("\\%s %s " % (prop, prop_dict[prop]))
195
196     # remove final char properties
197     n = len(lines)
198     while n:
199         n = n - 1
200         if not lines[n]:
201             del lines[n]
202             continue
203
204         if lines[n][:1] == '\\':
205             result = prop_exp.match(lines[n])
206             prop = result.group(1)
207             if prop in properties:
208                 prop_dict[prop] = result.group(2)
209                 del lines[n]
210                 continue
211
212             if check_token(lines[n],'\\end_inset'):
213                 # ensure proper newlines after inset end
214                 lines.append('')
215                 lines.append('')
216         break
217
218     #debug_list('*' * 10 + ' begin ' + '*' * 10, lines[start:])
219     if not lines[start:] and not lines[end:]:
220         return []
221     
222     result = lines[:start] + aux[:] + lines[end:]
223     if insert and result[0] != '':
224         return [''] + result[:]
225
226     return result[:]
227
228 def debug_list(title, list):
229     sys.stderr.write(title+'\n')
230     for line in list:
231         sys.stderr.write(line+'\n')
232
233 def update_language(header):
234     i = find_token(header, "\\language", 0)
235     if i == -1:
236         # no language, should emit a warning
237         header.append('\\language english')
238         return
239     # FIXME: find the document default language in user preferences
240     header[i] = '\\language english'
241     return
242
243 def convert(header,body):
244     sys.stderr.write("%d\n" % len(body))
245     update_tabular(body)
246     sys.stderr.write("%d\n" % len(body))
247     update_language(header)
248
249 if __name__ == "__main__":
250     pass
251