]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_1_6_0.py
77443c2364f0dd6ce84836453d9816f269d918bb
[lyx.git] / lib / lyx2lyx / lyx_1_1_6_0.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2002-2004 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 """ Convert files to the file format generated by lyx 1.1.6"""
20
21 import re
22 import string
23 from parser_tools import find_re, find_tokens, find_token, check_token
24
25 lyxtable_re = re.compile(r".*\\LyXTable$")
26 def update_tabular(document):
27     " Update tabular to version 1 (xml like syntax). "
28     lines = document.body
29     i=0
30     while 1:
31         i = find_re(lines, lyxtable_re, i)
32         if i == -1:
33             break
34         prop_dict = {"family" : "default", "series" : "default",
35                       "shape" : "default", "size" : "default",
36                       "emph" : "default", "bar" : "default",
37                       "noun" : "default", "latex" : "default", "color" : "default"}
38
39         # remove \LyXTable
40         lines[i] = lines[i][:-9]
41         i = i + 1
42         lines.insert(i,'')
43         i = i + 1
44         lines[i] = "\\begin_inset  Tabular"
45         i = i + 1
46         head = string.split(lines[i])
47         rows = int(head[0])
48         columns = int(head[1])
49
50         tabular_line = i
51         i = i +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]))
53
54         i = i +1
55
56         row_info = []
57         cont_row = []
58         for j in range(rows):
59             row_info.append(string.split(lines[i]))
60             if string.split(lines[i])[2] == '1':
61                 cont_row.append(j)
62             del lines[i]
63
64         column_info = []
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())
68             del lines[i]
69
70         cell_info = []
71         cell_col = []
72         ncells = 0
73         cell_re = re.compile(r'(\d) (\d) (\d) (\d) (\d) (\d) (\d) (".*") (".*")')
74         for j in range(rows):
75             for k in range(columns):
76                 #add column location to read properties
77                 cell_info.append(cell_re.match(lines[i]).groups())
78                 cell_col.append(k)
79                 if lines[i][0] != "2":
80                     ncells = ncells + 1
81                 del lines[i]
82
83         lines[tabular_line] = '<LyXTabular version="1" rows="%s" columns="%s">' % (rows-len(cont_row),columns)
84         del lines[i]
85         if not lines[i]:
86             del lines[i]
87
88         # Read cells
89         l = 0
90         cell_content = []
91         for j in range(rows):
92             cell_content.append([])
93
94         for j in range(rows):
95             for k in range(columns):
96                 cell_content[j].append([])
97
98         for j in range(rows):
99             for k in range(columns):
100                 m = j*columns + k
101                 if cell_info[m][0] == '2':
102                     continue
103
104                 if l == ncells -1:
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)
107                 else:
108                     end = find_token(lines, '\\newline', i)
109
110                 if end == -1:
111                     document.error("Malformed LyX file.")
112
113                 end = end - i
114                 while end > 0:
115                     cell_content[j][k].append(lines[i])
116                     del lines[i]
117                     end = end -1
118
119                 if string.find(lines[i],'\\newline') != -1:
120                     del lines[i]
121                 l = l + 1
122
123         tmp = []
124         tmp.append("")
125
126         for j in range(rows):
127             if j in cont_row:
128                 continue
129             tmp.append('<Row topline="%s" bottomline="%s" newpage="%s">' % (row_info[j][0],row_info[j][1],row_info[j][3]))
130
131             for k in range(columns):
132                 if j:
133                     tmp.append('<Column>')
134                 else:
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]))
136                 m = j*columns + k
137
138                 leftline = int(column_info[k][1])
139                 if cell_info[m][0] == '1':
140                     n = m + 1
141                     while n < rows * columns - 1 and cell_info[n][0] == '2':
142                         n = n + 1
143                     rightline = int(column_info[cell_col[n-1]][2])
144                 else:
145                     # not a multicolumn main cell
146                     rightline = int(column_info[k][2])
147
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')
150                 tmp.append('')
151                 tmp.append('\\layout %s' % document.default_layout)
152                 tmp.append('')
153
154                 if cell_info[m][0] != '2':
155                     paragraph = []
156                     if cell_info[m][4] == '1':
157                         l = j
158                         paragraph = paragraph + cell_content[j][k]
159                         while cell_info[m][4] == '1':
160                             m = m + columns
161                             l = l + 1
162                             if l >= rows: break
163                             paragraph = paragraph + cell_content[l][k]
164                     else:
165                         paragraph = cell_content[j][k]
166                     tmp = tmp + set_paragraph_properties(paragraph, prop_dict)
167
168                 tmp.append('\\end_inset ')
169                 tmp.append('</Cell>')
170                 tmp.append('</Column>')
171             tmp.append('</Row>')
172
173         tmp.append('</LyXTabular>')
174         tmp.append('')
175         tmp.append('\\end_inset ')
176         tmp.append('')
177         tmp.append('')
178         lines[i:i] = tmp
179
180         i = i + len(tmp)
181
182
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"}
193
194     start = 0
195     end = 0
196     i = 0
197     n = len(lines)
198
199     #skip empty lines
200     while i<n and lines[i] == "":
201         i = i + 1
202     start = i
203
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:
210             break
211         else:
212             prop_dict[prop] = result.group(2)
213         i = i + 1
214     end = i
215
216     aux = []
217     insert = 0
218     for prop in properties:
219         if prop_dict[prop] != 'default':
220             insert = 1
221             if prop == "color":
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]))
225
226     # remove final char properties
227     n = len(lines)
228     changed_prop = []
229
230     while n:
231         n = n - 1
232         if not lines[n]:
233             del lines[n]
234             continue
235
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)
242                 del lines[n]
243                 continue
244
245             if check_token(lines[n],'\\end_inset'):
246                 # ensure proper newlines after inset end
247                 lines.append('')
248                 lines.append('')
249         break
250
251     for line in lines[end:]:
252         if line[:1] == '\\':
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)
257
258     if not lines[start:] and not lines[end:]:
259         return []
260
261     result = lines[:start] + aux[:] + lines[end:]
262     if insert and result[0] != '':
263         return [''] + result[:]
264
265     return result[:]
266
267
268 def update_language(document):
269     " Update document language, if language is default convert it to
270     english."
271     header = document.header
272     i = find_token(header, "\\language", 0)
273     if i == -1:
274         # no language, should emit a warning
275         header.append('\\language english')
276         return
277     # This is the lyx behaviour: defaults to english
278     if string.split(header[i])[1] == 'default':
279         header[i] = '\\language english'
280     return
281
282
283 supported_versions = ["1.1.6","1.1.6fix1","1.1.6fix2","1.1"]
284 convert = [[217, [update_tabular, update_language]]]
285 revert  = []
286
287
288 if __name__ == "__main__":
289     pass