1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 José Matos <jamatos@lyx.org>
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.
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.
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.
18 import re, string, sys
19 from parser_tools import *
21 lyxtable_re = re.compile(r".*\\LyXTable$")
22 def update_tabular(lines):
25 i = find_re(lines, lyxtable_re, i)
28 prop_dict = {"family" : "default", "series" : "default",
29 "shape" : "default", "size" : "default",
30 "emph" : "default", "bar" : "default",
31 "noun" : "default", "latex" : "default", "color" : "default"}
34 lines[i] = lines[i][:-9]
38 lines[i] = "\\begin_inset Tabular"
40 head = string.split(lines[i])
42 columns = int(head[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]))
53 row_info.append(string.split(lines[i]))
54 if string.split(lines[i])[2] == '1':
59 col_info_re = re.compile(r'(\d) (\d) (\d) (".*") (".*")')
60 for j in range(columns):
61 column_info.append(col_info_re.match(lines[i]).groups())
67 cell_re = re.compile(r'(\d) (\d) (\d) (\d) (\d) (\d) (\d) (".*") (".*")')
69 for k in range(columns):
70 #add column location to read properties
71 cell_info.append(cell_re.match(lines[i]).groups())
73 if lines[i][0] != "2":
77 lines[tabular_line] = '<LyXTabular version="1" rows="%s" columns="%s">' % (rows-len(cont_row),columns)
86 cell_content.append([])
89 for k in range(columns):
90 cell_content[j].append([])
93 for k in range(columns):
95 if cell_info[m][0] == '2':
99 # the end variable refers to cell end, not to file end.
100 end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i)
102 end = find_token(lines, '\\newline ', i)
105 sys.stderr.write("Malformed lyx file\n")
110 cell_content[j][k].append(lines[i])
114 if lines[i] == '\\newline ':
121 for j in range(rows):
124 tmp.append('<Row topline="%s" bottomline="%s" newpage="%s">' % (row_info[j][0],row_info[j][1],row_info[j][3]))
126 for k in range(columns):
128 tmp.append('<Column>')
130 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]))
133 leftline = int(column_info[k][1])
134 if cell_info[m][0] == '1':
136 while n < rows * columns - 1 and cell_info[n][0] == '2':
138 rightline = int(column_info[cell_col[n-1]][2])
140 # not a multicolumn main cell
141 rightline = int(column_info[k][2])
143 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]))
144 tmp.append('\\begin_inset Text')
146 tmp.append('\\layout Standard')
149 if cell_info[m][0] != '2':
151 if cell_info[m][4] == '1':
153 paragraph.extend(cell_content[j][k])
154 while cell_info[m][4] == '1':
157 paragraph.extend(cell_content[l][k])
159 paragraph = cell_content[j][k]
160 tmp.extend(set_paragraph_properties(paragraph, prop_dict))
162 tmp.append('\\end_inset ')
163 tmp.append('</Cell>')
164 tmp.append('</Column>')
167 tmp.append('</LyXTabular>')
169 tmp.append('\\end_inset ')
179 prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
180 def set_paragraph_properties(lines, prop_dict):
181 # we need to preserve the order of options
182 properties = ["family","series","shape","size",
183 "emph","bar","noun","latex","color"]
184 prop_value = {"family" : "default", "series" : "medium",
185 "shape" : "up", "size" : "normal",
186 "emph" : "off", "bar" : "no",
187 "noun" : "off", "latex" : "no_latex", "color" : "none"}
195 while i<n and lines[i] == "":
199 #catch open char properties
200 while i<n and lines[i][:1] == "\\":
201 result = prop_exp.match(lines[i])
202 # sys.stderr.write(lines[i]+"\n")
203 prop = result.group(1)
204 if prop not in properties:
207 prop_dict[prop] = result.group(2)
213 for prop in properties:
214 if prop_dict[prop] != 'default':
217 aux.append("\\%s %s" % (prop, prop_dict[prop]))
218 elif prop != "family" or prop_dict[prop] != "roman":
219 aux.append("\\%s %s " % (prop, prop_dict[prop]))
221 # remove final char properties
231 if lines[n][:1] == '\\':
232 result = prop_exp.match(lines[n])
233 prop = result.group(1)
234 if prop in properties:
235 changed_prop.append(prop)
236 prop_dict[prop] = result.group(2)
240 if check_token(lines[n],'\\end_inset'):
241 # ensure proper newlines after inset end
246 for line in lines[end:]:
248 result = prop_exp.match(line)
249 prop = result.group(1)
250 if prop in properties and prop not in changed_prop:
251 prop_dict[prop] = result.group(2)
253 if not lines[start:] and not lines[end:]:
256 result = lines[:start] + aux[:] + lines[end:]
257 if insert and result[0] != '':
258 return [''] + result[:]
262 def update_language(header):
263 i = find_token(header, "\\language", 0)
265 # no language, should emit a warning
266 header.append('\\language english')
268 # This is the lyx behaviour: defaults to english
269 header[i] = '\\language english'
272 def convert(header,body):
274 update_language(header)
276 if __name__ == "__main__":