1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
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.
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.
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.
21 from parser_tools import find_token, find_token_backwards, find_re, get_layout
24 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
26 def replace_protected_separator(file):
30 i = find_token(lines, "\\protected_separator", i)
33 j = find_token_backwards(lines, "\\layout", i)
34 #if j == -1: print error
35 layout = get_layout(lines[j], file.default_layout)
37 if layout == "LyX-Code":
39 while lines[i] == "\\protected_separator ":
43 lines[i-1] = lines[i-1] + result + lines[i]
45 lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
50 def merge_formula_inset(file):
54 i = find_token(lines, "\\begin_inset Formula", i)
56 if lines[i+1] in math_env:
57 lines[i] = lines[i] + lines[i+1]
62 # Update from tabular format 4 to 5 if necessary
63 def update_tabular(file):
65 lyxtable_re = re.compile(r".*\\LyXTable$")
68 i = find_re(lines, lyxtable_re, i)
78 rows = int(string.split(lines[i])[0])
79 columns = int(string.split(lines[i])[1])
82 for j in range(columns):
83 col_info = string.split(lines[i])
84 if len(col_info) == 3:
85 lines[i] = lines[i] + '"" ""'
87 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
91 lines[i] = lines[i] + ' "" ""'
99 i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
102 lines[i] = lines[i] + '{}'
106 def remove_cursor(file):
108 i = find_token(lines, '\\cursor', 0)
113 def remove_vcid(file):
115 i = find_token(lines, '\\lyxvcid', 0)
118 i = find_token(lines, '\\lyxrcsid', 0)
123 def first_layout(file):
125 while (lines[0] == ""):
127 if lines[0][:7] != "\\layout":
128 lines[:0] = ['\\layout %s' % file.default_layout, '']
131 def remove_space_in_units(file):
133 margins = ["\\topmargin","\\rightmargin",
134 "\\leftmargin","\\bottommargin"]
136 unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
138 for margin in margins:
141 i = find_token(lines, margin, i)
145 result = unit_rexp.search(lines[i])
147 lines[i] = margin + " " + result.group(1) + result.group(2)
151 def latexdel_getargs(file, i):
154 # play safe, clean empty lines
160 j = find_token(lines, '\\end_inset', i)
165 file.warning("Unexpected end of inset.")
166 j = find_token(lines, '\\begin_inset LatexDel }{', i)
168 ref = string.join(lines[i:j])
171 # play safe, clean empty lines
177 j = find_token(lines, '\\end_inset', i - 1)
181 file.warning("Unexpected end of inset.")
182 j = find_token(lines, '\\begin_inset LatexDel }', i)
183 label = string.join(lines[i:j])
189 def update_ref(file):
193 i = find_token(lines, '\\begin_inset LatexCommand', i)
197 if string.split(lines[i])[-1] == "\\ref{":
199 ref, label = latexdel_getargs(file, i)
200 lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label)
205 def update_latexdel(file):
208 latexdel_re = re.compile(r".*\\begin_inset LatexDel")
210 i = find_re(lines, latexdel_re, i)
213 lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
215 j = string.find(lines[i],'\\begin_inset')
216 lines.insert(i+1, lines[i][j:])
217 lines[i] = string.strip(lines[i][:j])
220 if string.split(lines[i])[-1] in ("\\url{", "\\htmlurl{"):
223 ref, label = latexdel_getargs(file, i)
224 lines[i -1] = "%s[%s]{%s}" % (lines[i-1][:-1], label, ref)
229 convert = [[216, [first_layout, remove_vcid, remove_cursor, update_toc,
230 replace_protected_separator, merge_formula_inset,
231 update_tabular, remove_space_in_units, update_ref, update_latexdel]]]
234 if __name__ == "__main__":