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