1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2004 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 """ Convert files to the file format generated by lyx 1.1.6"""
23 from parser_tools import find_re, find_tokens, find_token, check_token
25 lyxtable_re = re.compile(r".*\\LyXTable$")
26 def update_tabular(document):
27 " Update tabular to version 1 (xml like syntax). "
31 i = find_re(lines, lyxtable_re, i)
34 prop_dict = {"family" : "default", "series" : "default",
35 "shape" : "default", "size" : "default",
36 "emph" : "default", "bar" : "default",
37 "noun" : "default", "latex" : "default", "color" : "default"}
40 lines[i] = lines[i][:-9]
44 lines[i] = "\\begin_inset Tabular"
46 head = string.split(lines[i])
48 columns = int(head[1])
52 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]))
59 row_info.append(string.split(lines[i]))
60 if string.split(lines[i])[2] == '1':
65 col_info_re = re.compile(r'(\d) (\d) (\d) (".*") (".*")')
66 for j in range(columns):
67 column_info.append(col_info_re.match(lines[i]).groups())
73 cell_re = re.compile(r'(\d) (\d) (\d) (\d) (\d) (\d) (\d) (".*") (".*")')
75 for k in range(columns):
76 #add column location to read properties
77 cell_info.append(cell_re.match(lines[i]).groups())
79 if lines[i][0] != "2":
83 lines[tabular_line] = '<LyXTabular version="1" rows="%s" columns="%s">' % (rows-len(cont_row),columns)
92 cell_content.append([])
95 for k in range(columns):
96 cell_content[j].append([])
99 for k in range(columns):
101 if cell_info[m][0] == '2':
105 # the end variable refers to cell end, not to document end.
106 end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i)
108 end = find_token(lines, '\\newline', i)
111 document.error("Malformed LyX file.")
115 cell_content[j][k].append(lines[i])
119 if string.find(lines[i],'\\newline') != -1:
126 for j in range(rows):
129 tmp.append('<Row topline="%s" bottomline="%s" newpage="%s">' % (row_info[j][0],row_info[j][1],row_info[j][3]))
131 for k in range(columns):
133 tmp.append('<Column>')
135 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]))
138 leftline = int(column_info[k][1])
139 if cell_info[m][0] == '1':
141 while n < rows * columns - 1 and cell_info[n][0] == '2':
143 rightline = int(column_info[cell_col[n-1]][2])
145 # not a multicolumn main cell
146 rightline = int(column_info[k][2])
148 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]))
149 tmp.append('\\begin_inset Text')
151 tmp.append('\\layout %s' % document.default_layout)
154 if cell_info[m][0] != '2':
156 if cell_info[m][4] == '1':
158 paragraph = paragraph + cell_content[j][k]
159 while cell_info[m][4] == '1':
163 paragraph = paragraph + cell_content[l][k]
165 paragraph = cell_content[j][k]
166 tmp = tmp + set_paragraph_properties(paragraph, prop_dict)
168 tmp.append('\\end_inset ')
169 tmp.append('</Cell>')
170 tmp.append('</Column>')
173 tmp.append('</LyXTabular>')
175 tmp.append('\\end_inset ')
183 prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
184 def set_paragraph_properties(lines, prop_dict):
185 " Set paragraph properties."
186 # we need to preserve the order of options
187 properties = ["family","series","shape","size",
188 "emph","bar","noun","latex","color"]
189 prop_value = {"family" : "default", "series" : "medium",
190 "shape" : "up", "size" : "normal",
191 "emph" : "off", "bar" : "no",
192 "noun" : "off", "latex" : "no_latex", "color" : "none"}
200 while i<n and lines[i] == "":
204 #catch open char properties
205 while i<n and lines[i][:1] == "\\":
206 result = prop_exp.match(lines[i])
207 # sys.stderr.write(lines[i]+"\n")
208 prop = result.group(1)
209 if prop not in properties:
212 prop_dict[prop] = result.group(2)
218 for prop in properties:
219 if prop_dict[prop] != 'default':
222 aux.append("\\%s %s" % (prop, prop_dict[prop]))
223 elif prop != "family" or prop_dict[prop] != "roman":
224 aux.append("\\%s %s " % (prop, prop_dict[prop]))
226 # remove final char properties
236 if lines[n][:1] == '\\':
237 result = prop_exp.match(lines[n])
238 prop = result.group(1)
239 if prop in properties:
240 changed_prop.append(prop)
241 prop_dict[prop] = result.group(2)
245 if check_token(lines[n],'\\end_inset'):
246 # ensure proper newlines after inset end
251 for line in lines[end:]:
253 result = prop_exp.match(line)
254 prop = result.group(1)
255 if prop in properties and prop not in changed_prop:
256 prop_dict[prop] = result.group(2)
258 if not lines[start:] and not lines[end:]:
261 result = lines[:start] + aux[:] + lines[end:]
262 if insert and result[0] != '':
263 return [''] + result[:]
268 def update_language(document):
269 " Update document language, if language is default convert it to
271 header = document.header
272 i = find_token(header, "\\language", 0)
274 # no language, should emit a warning
275 header.append('\\language english')
277 # This is the lyx behaviour: defaults to english
278 if string.split(header[i])[1] == 'default':
279 header[i] = '\\language english'
283 supported_versions = ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]
284 convert = [[217, [update_tabular, update_language]]]
288 if __name__ == "__main__":