1 # This file is part of lyx2lyx
2 # Copyright (C) 2003 José Matos <jamatos@fep.up.pt>
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.
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.
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.
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
24 def add_end_layout(lines):
25 i = find_token(lines, '\\layout', 0)
31 struct_stack = ["\\layout"]
33 i = find_tokens(lines, ["\\begin_inset", "\\end_inset", "\\layout",
34 "\\begin_deeper", "\\end_deeper", "\\the_end"], i)
36 token = split(lines[i])[0]
38 if token == "\\begin_inset":
39 struct_stack.append(token)
43 if token == "\\end_inset":
44 tail = struct_stack.pop()
45 if tail == "\\layout":
46 lines.insert(i,"\\end_layout")
48 #Check if it is the correct tag
53 if token == "\\layout":
54 tail = struct_stack.pop()
56 lines.insert(i,"\\end_layout")
59 struct_stack.append(tail)
61 struct_stack.append(token)
64 if token == "\\begin_deeper" or token == "\\end_deeper":
65 lines.insert(i,"\\end_layout")
70 lines.insert(i, "\\end_layout")
73 def layout2begin_layout(lines):
76 i = find_token(lines, '\\layout', i)
80 lines[i] = replace(lines[i], '\\layout', '\\begin_layout')
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"')
88 def table_valignment_middle(lines):
91 i = find_token(lines, '\\begin_inset Tabular', i)
94 j = find_end_of_inset(lines, i + 1)
96 #this should not happen
97 valignment_middle(lines, i + 1, len(lines))
99 valignment_middle(lines, i + 1, j)
102 def end_document(lines):
103 i = find_token(lines, "\\the_end", 0)
105 lines.append("\\end_document")
107 lines[i] = "\\end_document"
109 def convert_bibtex(lines):
110 bibtex_header = "\\begin_inset LatexCommand \\bibtex"
113 i = find_token(lines, bibtex_header, i)
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, "")
120 # Trim any space at extremes
121 lines[i] = strip(lines[i])
123 # Does the thing have an opt arg?
124 optarg_rexp = re.compile(r'^\[([^]]*)\]')
125 optarg = optarg_rexp.search(lines[i])
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) + ']', '')
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 = ''
137 mainarg_contents = mainarg.group(1)
139 # complain about a mal-formed lyx file.
140 stderr.write("Mal-formed bibitem\n")
142 # optarg will contain either
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]
153 bibtotoc, style = 'true',optarg_list[1]
155 # mainarg will contain a comma-separated list of files.
156 mainarg_list = split( mainarg_contents, ',')
158 new_syntax = ['\\begin_inset Bibtex']
159 for file in mainarg_list:
160 new_syntax.append('\t' + 'filename ' + file)
163 new_syntax.append('\t' + 'style ' + style)
165 if bibtotoc == 'true':
166 new_syntax.append('\t' + 'bibtotoc ' + bibtotoc)
168 # Replace old syntax with new
169 lines[i:i+1] = new_syntax
171 i = i + len(new_syntax) + 1
174 def convert(header, body):
176 layout2begin_layout(body)
178 table_valignment_middle(body)
181 if __name__ == "__main__":