]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyxconvert_224.py
src/support/ChangeLog
[lyx.git] / lib / lyx2lyx / lyxconvert_224.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2003 José Matos <jamatos@fep.up.pt>
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
19 from parser_tools import find_token, find_tokens, find_end_of_inset
20 from sys import stderr
21 from string import replace, split, strip
22 import re
23
24 def add_end_layout(lines):
25     i = find_token(lines, '\\layout', 0)
26
27     if i == -1:
28         return
29
30     i = i + 1
31     struct_stack = ["\\layout"]
32     while 1:
33         i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
34                                 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
35
36         token = split(lines[i])[0]
37
38         if token == "\\begin_inset":
39             struct_stack.append(token)
40             i = i + 1
41             continue
42
43         if token == "\\end_inset":
44             tail = struct_stack.pop()
45             if tail == "\\layout":
46                 lines.insert(i,"\\end_layout")
47                 i = i + 1
48                 #Check if it is the correct tag
49                 struct_stack.pop()
50             i = i + 1
51             continue
52
53         if token == "\\layout":
54             tail = struct_stack.pop()
55             if tail == token:
56                 lines.insert(i,"\\end_layout")
57                 i = i + 2
58             else:
59                 struct_stack.append(tail)
60                 i = i + 1
61             struct_stack.append(token)
62             continue
63
64         if token == "\\begin_deeper" or token == "\\end_deeper":
65             lines.insert(i,"\\end_layout")
66             i = i + 2
67             continue
68         
69         #case \end_document
70         lines.insert(i, "\\end_layout")
71         return
72
73 def layout2begin_layout(lines):
74     i = 0
75     while 1:
76         i = find_token(lines, '\\layout', i)
77         if i == -1:
78             return
79
80         lines[i] = replace(lines[i], '\\layout', '\\begin_layout')
81         i = i + 1
82
83 def valignment_middle(lines, start, end):
84     for i in range(start, end):
85         if re.search('^<(column|cell) .*valignment="center".*>$', lines[i]):
86             lines[i] = replace(lines[i], 'valignment="center"', 'valignment="middle"')
87
88 def table_valignment_middle(lines):
89     i = 0
90     while 1:
91         i = find_token(lines, '\\begin_inset  Tabular', i)
92         if i == -1:
93             return
94         j = find_end_of_inset(lines, i + 1)
95         if j == -1:
96             #this should not happen
97             valignment_middle(lines, i + 1, len(lines))
98             return
99         valignment_middle(lines, i + 1, j)
100         i = j + 1
101
102 def end_document(lines):
103     i = find_token(lines, "\\the_end", 0)
104     if i == -1:
105         lines.append("\\end_document")
106         return
107     lines[i] = "\\end_document"
108
109 def convert_bibtex(lines):
110     bibtex_header = "\\begin_inset LatexCommand \\bibtex"
111     i = 0
112     while 1:
113         i = find_token(lines, bibtex_header, i)
114         if i == -1:
115             break
116         # We've found a bibtex inset.
117         # I'd like to strip bibtex_header from the front of lines[i]
118         lines[i] = replace(lines[i], bibtex_header, "")
119
120         # Trim any space at extremes
121         lines[i] = strip(lines[i])
122
123         # Does the thing have an opt arg?
124         optarg_rexp = re.compile(r'^\[([^]]*)\]')
125         optarg = optarg_rexp.search(lines[i])
126         optarg_contents = ''
127         if optarg:
128                 optarg_contents = optarg.group(1)
129                 # strip [<optarg_contents>] from the front of lines[i]
130                 lines[i] = replace (lines[i], '[' + optarg.group(0) + ']', '')
131
132         # lines[i] should now contain "{<list of databases>}"
133         mainarg_rexp = re.compile(r'{([^}]*)}')
134         mainarg = mainarg_rexp.search(lines[i])
135         mainarg_contents = ''
136         if mainarg:
137                 mainarg_contents = mainarg.group(1)
138         else:
139                 # complain about a mal-formed lyx file.
140                 stderr.write("Mal-formed bibitem\n")
141
142         # optarg will contain either
143         #       "bibtotoc,<style>"
144         # or
145         #       "<style>"
146         # ie, these are a comma-separated list of arguments.
147         optarg_list = split(optarg_contents, ',')
148         if len(optarg_list) == 0:
149             bibtotoc, style = '',''
150         elif len(optarg_list) == 1:
151             bibtotoc, style = '',optarg_list[0]
152         else:
153             bibtotoc, style = 'true',optarg_list[1]
154         
155         # mainarg will contain a comma-separated list of files.
156         mainarg_list = split( mainarg_contents, ',')
157
158         new_syntax = ['\\begin_inset Bibtex']
159         for file in mainarg_list:
160             new_syntax.append('\t' + 'filename ' + file)
161
162         if style:
163             new_syntax.append('\t' + 'style ' + style)
164
165         if bibtotoc == 'true':
166             new_syntax.append('\t' + 'bibtotoc ' + bibtotoc)
167
168         # Replace old syntax with new
169         lines[i:i+1] = new_syntax
170
171         i = i + len(new_syntax) + 1
172
173         
174 def convert(header, body):
175     add_end_layout(body)
176     layout2begin_layout(body)
177     end_document(body)
178     table_valignment_middle(body)
179     convert_bibtex(body)
180
181 if __name__ == "__main__":
182     pass