]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_216.py
Remove FormatVersion
[lyx.git] / lib / lyx2lyx / lyxconvert_216.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 José Matos <jamatos@lyx.org>
3 #
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.
8 #
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.
13 #
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.
17
18 import re, string, sys
19 from parser_tools import *
20
21 lyxtable_re = re.compile(r".*\\LyXTable$")
22 def update_tabular(lines):
23     i=0
24     while 1:
25         i = find_re(lines, lyxtable_re, i)
26         if i == -1:
27             break
28         prop_dict = {"family" : "default", "series" : "default",
29                       "shape" : "default", "size" : "default",
30                       "emph" : "default", "bar" : "default",
31                       "noun" : "default", "latex" : "default", "color" : "default"}
32
33         # remove \LyXTable
34         lines[i] = lines[i][:-9]
35         i = i + 1
36         lines.insert(i,'')
37         i = i + 1
38         lines[i] = "\\begin_inset  Tabular"
39         i = i + 1
40         head = string.split(lines[i])
41         rows = int(head[0])
42         columns = int(head[1])
43
44         lines[i] = '<LyXTabular version="1" rows="%s" columns="%s">' % (head[0],head[1])
45         i = i +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]))
47
48         i = i +1
49         
50         row_info = []
51         for j in range(rows):
52             row_info.append(string.split(lines[i]))
53             del lines[i]
54         column_info = []
55         for j in range(columns):
56             column_info.append(string.split(lines[i]))
57             del lines[i]
58
59         cell_info = []
60         ncells = 0
61         for j in range(rows):
62             for k in range(columns):
63                 cell_info.append(string.split(lines[i]))
64                 if lines[i][0] != "2":
65                     ncells = ncells + 1
66                 del lines[i]
67
68         del lines[i]
69         if not lines[i]:
70             del lines[i]
71         
72         tmp = []
73         tmp.append("")
74
75         l = 0
76         for j in range(rows):
77             tmp.append('<Row topline="%s" bottomline="%s" newpage="%s">' % (row_info[j][0],row_info[j][1],row_info[j][2]))
78
79             for k in range(columns):
80                 if j:
81                     tmp.append('<Column>')
82                 else:
83                     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]))
84                 m = j*columns + k
85
86                 leftline = int(cell_info[m][4]) or int(column_info[k][1])
87                 if cell_info[m][0] == '1':
88                     n = m + 1
89                     while n < rows * columns - 1 and cell_info[n][0] == '2':
90                         n = n + 1
91                     rightline = int(cell_info[n][5]) or int(column_info[k][2])
92                 else:
93                     # not a multicolumn main cell
94                     # rightline = int(cell_info[m][5]) or int(column_info[k][2])
95                     rightline = int(column_info[k][2])
96
97                 tmp.append('<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%d" rightline="%d" rotate="%s" usebox="0" 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][6],cell_info[m][7],cell_info[m][8]))
98                 tmp.append('\\begin_inset Text')
99                 tmp.append('')
100                 tmp.append('\\layout Standard')
101                 tmp.append('')
102
103                 if cell_info[m][0] == '2':
104                     tmp.append('\\end_inset ')
105                     tmp.append('</Cell>')
106                     tmp.append('</Column>')
107                     continue
108
109                 if l == ncells -1:
110                     end = find_tokens(lines, ['\\layout','\\the_end','\\end_deeper','\\end_float'], i)
111                     if end == -1:
112                         sys.stderr.write("Malformed lyx file\n")
113                     else:
114                         end = end - i
115                         paragraph = []
116                         while end > 0:
117                             paragraph.append(lines[i])
118                             del lines[i]
119                             end = end -1
120                         tmp.extend(set_paragraph_properties(paragraph, prop_dict))
121                 else:
122                     paragraph = []
123                     while lines[i] != '\\newline ':
124                         paragraph.append(lines[i])
125                         del lines[i]
126                     del lines[i]
127                     tmp.extend(set_paragraph_properties(paragraph, prop_dict))
128                 
129                 tmp.append('\\end_inset ')
130                 tmp.append('</Cell>')
131                 tmp.append('</Column>')
132                 l = l + 1
133             tmp.append('</Row>')
134
135         tmp.append('</LyXTabular>')
136         tmp.append('')
137         tmp.append('\\end_inset ')
138         tmp.append('')
139         tmp.append('')
140         tail = lines[i:]
141         lines[i:] = []
142         lines.extend(tmp)
143         lines.extend(tail)
144
145         i = i + len(tmp)
146
147 prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
148 def set_paragraph_properties(lines, prop_dict):
149     # we need to preserve the order of options
150     properties = ["family","series","shape","size",
151                   "emph","bar","noun","latex","color"]
152     prop_value = {"family" : "default", "series" : "medium",
153                    "shape" : "up", "size" : "normal",
154                    "emph" : "off", "bar" : "no",
155                    "noun" : "off", "latex" : "no_latex", "color" : "none"}
156
157     start = 0
158     end = 0
159     i = 0
160     n = len(lines)
161
162     #skip empty lines
163     while i<n and lines[i] == "":
164         i = i + 1
165     start = i
166
167     #catch open char properties
168     while i<n and lines[i][:1] == "\\":
169         result = prop_exp.match(lines[i])
170         # sys.stderr.write(lines[i]+"\n")
171         prop = result.group(1)
172         if prop not in properties:
173             break
174         else:
175             prop_dict[prop] = result.group(2)
176         i = i + 1
177     end = i
178
179     aux = []
180     insert = 0
181     for prop in properties:
182         if prop_dict[prop] != 'default':
183             insert = 1
184             if prop == "color":
185                 aux.append("\\%s %s" % (prop, prop_dict[prop]))
186             elif prop != "family" or prop_dict[prop] != "roman":
187                     aux.append("\\%s %s " % (prop, prop_dict[prop]))
188
189     # remove final char properties
190     n = len(lines)
191     while n:
192         n = n - 1
193         if not lines[n]:
194             del lines[n]
195             continue
196
197         if lines[n][:1] == '\\':
198             result = prop_exp.match(lines[n])
199             prop = result.group(1)
200             if prop in properties:
201                 prop_dict[prop] = result.group(2)
202                 del lines[n]
203                 continue
204
205             if check_token(lines[n],'\\end_inset'):
206                 # ensure proper newlines after inset end
207                 lines.append('')
208                 lines.append('')
209         break
210
211     #debug_list('*' * 10 + ' begin ' + '*' * 10, lines[start:])
212     if not lines[start:] and not lines[end:]:
213         return []
214     
215     result = lines[:start] + aux[:] + lines[end:]
216     if insert and result[0] != '':
217         return [''] + result[:]
218
219     return result[:]
220
221 def debug_list(title, list):
222     sys.stderr.write(title+'\n')
223     for line in list:
224         sys.stderr.write(line+'\n')
225
226 def update_language(header):
227     i = find_token(header, "\\language", 0)
228     if i == -1:
229         # no language, should emit a warning
230         header.append('\\language english')
231         return
232     # FIXME: find the document default language in user preferences
233     header[i] = '\\language english'
234     return
235
236 def convert(header,body):
237     sys.stderr.write("%d\n" % len(body))
238     update_tabular(body)
239     sys.stderr.write("%d\n" % len(body))
240     update_language(header)
241
242 if __name__ == "__main__":
243     pass
244