1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 José Matos <jamatos@lyx.org>
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.
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.
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.
19 import re, string, sys
20 from parser_tools import *
22 lyxtable_re = re.compile(r".*\\LyXTable$")
23 def update_tabular(lines):
26 i = find_re(lines, lyxtable_re, i)
29 prop_dict = {"family" : "default", "series" : "default",
30 "shape" : "default", "size" : "default",
31 "emph" : "default", "bar" : "default",
32 "noun" : "default", "latex" : "default", "color" : "default"}
35 lines[i] = lines[i][:-9]
39 lines[i] = "\\begin_inset Tabular"
41 head = string.split(lines[i])
43 columns = int(head[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]))
54 row_info.append(string.split(lines[i]))
55 if string.split(lines[i])[2] == '1':
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())
68 cell_re = re.compile(r'(\d) (\d) (\d) (\d) (\d) (\d) (\d) (".*") (".*")')
70 for k in range(columns):
71 #add column location to read properties
72 cell_info.append(cell_re.match(lines[i]).groups())
74 if lines[i][0] != "2":
78 lines[tabular_line] = '<LyXTabular version="1" rows="%s" columns="%s">' % (rows-len(cont_row),columns)
87 cell_content.append([])
90 for k in range(columns):
91 cell_content[j].append([])
94 for k in range(columns):
96 if cell_info[m][0] == '2':
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)
103 end = find_token(lines, '\\newline', i)
106 sys.stderr.write("Malformed lyx file\n")
111 cell_content[j][k].append(lines[i])
115 if string.find(lines[i],'\\newline') != -1:
122 for j in range(rows):
125 tmp.append('<Row topline="%s" bottomline="%s" newpage="%s">' % (row_info[j][0],row_info[j][1],row_info[j][3]))
127 for k in range(columns):
129 tmp.append('<Column>')
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]))
134 leftline = int(column_info[k][1])
135 if cell_info[m][0] == '1':
137 while n < rows * columns - 1 and cell_info[n][0] == '2':
139 rightline = int(column_info[cell_col[n-1]][2])
141 # not a multicolumn main cell
142 rightline = int(column_info[k][2])
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')
147 tmp.append('\\layout Standard')
150 if cell_info[m][0] != '2':
152 if cell_info[m][4] == '1':
154 paragraph = paragraph + cell_content[j][k]
155 while cell_info[m][4] == '1':
158 paragraph = paragraph + cell_content[l][k]
160 paragraph = cell_content[j][k]
161 tmp = tmp + set_paragraph_properties(paragraph, prop_dict)
163 tmp.append('\\end_inset ')
164 tmp.append('</Cell>')
165 tmp.append('</Column>')
168 tmp.append('</LyXTabular>')
170 tmp.append('\\end_inset ')
177 prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
178 def set_paragraph_properties(lines, prop_dict):
179 # we need to preserve the order of options
180 properties = ["family","series","shape","size",
181 "emph","bar","noun","latex","color"]
182 prop_value = {"family" : "default", "series" : "medium",
183 "shape" : "up", "size" : "normal",
184 "emph" : "off", "bar" : "no",
185 "noun" : "off", "latex" : "no_latex", "color" : "none"}
193 while i<n and lines[i] == "":
197 #catch open char properties
198 while i<n and lines[i][:1] == "\\":
199 result = prop_exp.match(lines[i])
200 # sys.stderr.write(lines[i]+"\n")
201 prop = result.group(1)
202 if prop not in properties:
205 prop_dict[prop] = result.group(2)
211 for prop in properties:
212 if prop_dict[prop] != 'default':
215 aux.append("\\%s %s" % (prop, prop_dict[prop]))
216 elif prop != "family" or prop_dict[prop] != "roman":
217 aux.append("\\%s %s " % (prop, prop_dict[prop]))
219 # remove final char properties
229 if lines[n][:1] == '\\':
230 result = prop_exp.match(lines[n])
231 prop = result.group(1)
232 if prop in properties:
233 changed_prop.append(prop)
234 prop_dict[prop] = result.group(2)
238 if check_token(lines[n],'\\end_inset'):
239 # ensure proper newlines after inset end
244 for line in lines[end:]:
246 result = prop_exp.match(line)
247 prop = result.group(1)
248 if prop in properties and prop not in changed_prop:
249 prop_dict[prop] = result.group(2)
251 if not lines[start:] and not lines[end:]:
254 result = lines[:start] + aux[:] + lines[end:]
255 if insert and result[0] != '':
256 return [''] + result[:]
260 def update_language(header):
261 i = find_token(header, "\\language", 0)
263 # no language, should emit a warning
264 header.append('\\language english')
266 # This is the lyx behaviour: defaults to english
267 if string.split(header[i])[1] == 'default':
268 header[i] = '\\language english'
271 def convert(header,body):
273 update_language(header)
275 if __name__ == "__main__":