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