]> git.lyx.org Git - features.git/blob - lib/lyx2lyx/lyxconvert_216.py
86f76ca6f4aff1faa38acfd456aa2c7a5e59c344
[features.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         # remove \LyXTable
29         lines[i] = lines[i][:-9]
30         i = i + 1
31         lines.insert(i,'')
32         i = i + 1
33         lines[i] = "\\begin_inset  Tabular"
34         i = i + 1
35         head = string.split(lines[i])
36         rows = int(head[0])
37         columns = int(head[1])
38
39         lines[i] = '<LyXTabular version="1" rows="%s" columns="%s">' % (head[0],head[1])
40         i = i +1
41         lines.insert(i, '<Features rotate="%s" islongtable="%s" endhead="0" endfirsthead="0" endfoot="0" endlastfoot="0">' % (head[2], head[3]))
42
43         i = i +1
44         
45         row_info = []
46         for j in range(rows):
47             row_info.append(string.split(lines[i]))
48             del lines[i]
49         column_info = []
50         for j in range(columns):
51             column_info.append(string.split(lines[i]))
52             del lines[i]
53
54         cell_info = []
55         cell_location = []
56         ncells = 0
57         l = 0
58         for j in range(rows):
59             tmp = []
60             for k in range(columns):
61                 cell_info.append(string.split(lines[i]))
62                 if lines[i][0] != "2":
63                     tmp.append(l)
64                     ncells = ncells + 1
65                 del lines[i]
66                 l = l + 1
67             cell_location.append(tmp)
68
69         for j in range(rows):
70             for k in cell_location[j]:
71                 sys.stderr.write("%d\t" % (k))
72             sys.stderr.write("\n")
73
74         del lines[i]
75         if not lines[i]:
76             del lines[i]
77         
78         tmp = []
79         tmp.append("")
80
81         l = 0
82         for j in range(rows):
83             tmp.append('<Row topline="%s" bottomline="%s" newpage="%s">' % (row_info[j][0],row_info[j][1],row_info[j][2]))
84
85             for k in range(columns):
86                 if j:
87                     tmp.append('<Column>')
88                 else:
89                     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]))
90                 m = j*columns + k
91                 sys.stderr.write("%d\t%d\t%d\n" % (j,k,m))
92                 tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%s" rightline="%s" rotate="%s" usebox="0" width=%s special=%s>' % (cell_info[m][0],cell_info[m][1],cell_info[m][2],cell_info[m][3],cell_info[m][4],cell_info[m][5],cell_info[m][6],cell_info[m][7],cell_info[m][8]))
93                 tmp.append('\\begin_inset Text')
94                 tmp.append('')
95                 tmp.append('\\layout Standard')
96                 tmp.append('')
97
98                 if m not in cell_location[j]:
99                     tmp.append('\\end_inset ')
100                     tmp.append('</Cell>')
101                     tmp.append('</Column>')
102                     continue
103
104                 if l == ncells -1:
105                     end = find_tokens(lines, ['\\layout','\\the_end'], i)
106                     if end == -1:
107                         sys.stderr.write("Malformed lyx file\n")
108                     else:
109                         end = end - i
110                         paragraph = []
111                         while end > 0:
112                             paragraph.append(lines[i])
113                             del lines[i]
114                             end = end -1
115                         tmp.extend(set_paragraph_properties(paragraph))
116                 else:
117                     paragraph = []
118                     while lines[i] != '\\newline ':
119                         paragraph.append(lines[i])
120                         del lines[i]
121                     del lines[i]
122                     tmp.extend(set_paragraph_properties(paragraph))
123                 
124                 tmp.append('\\end_inset ')
125                 tmp.append('</Cell>')
126                 tmp.append('</Column>')
127                 l = l + 1
128             tmp.append('</Row>')
129
130         tmp.append('</LyXTabular>')
131         tmp.append('')
132         tmp.append('\\end_inset ')
133         tail = lines[i:]
134         lines[i:] = []
135         lines.extend(tmp)
136         lines.extend(tail)
137
138         i = i + len(tmp)
139
140 prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
141 def set_paragraph_properties(lines):
142     # we need to preserve the order of options
143     properties = ["family","series","shape","size",
144                   "emph","bar","noun","latex","color"]
145     prop_value = {"family":"roman", "series" : "medium",
146                    "shape" : "up", "size" : "normal",
147                    "emph" : "off", "bar" : "no",
148                    "noun" : "off", "latex" : "no_latex", "color" : "none"}
149
150     start = 0
151     end = 0
152     i = 0
153     n = len(lines)
154     
155     while i<n and lines[i] == "":
156         i = i + 1
157     start = i
158
159     while i<n and lines[i][:1] == "\\":
160         result = prop_exp.match(lines[i])
161         sys.stderr.write(lines[i]+"\n")
162         prop = result.group(1)
163         if prop not in properties:
164             sys.stderr.write("Unknown property: %s\n" % (prop))
165         else:
166             prop_value[prop] = result.group(2)
167         i = i + 1
168     end = i
169
170     aux = []
171     for prop in properties:
172         if prop == "color":
173             aux.append("\\%s %s" % (prop, prop_value[prop]))
174         elif prop == "family":
175             if prop_value[prop] != "roman":
176                 aux.append("\\%s %s " % (prop, prop_value[prop]))
177         else:
178             aux.append("\\%s %s " % (prop, prop_value[prop]))
179         
180
181     return lines[:start] + aux[:] + lines[end:]
182
183 def convert(header,body):
184     sys.stderr.write("%d\n" % len(body))
185     update_tabular(body)
186     sys.stderr.write("%d\n" % len(body))
187
188 if __name__ == "__main__":
189     pass
190