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