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