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
23 ####################################################################
24 # Private helper functions
26 def get_layout(line, default_layout):
27 tokens = string.split(line)
33 ####################################################################
35 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
37 def replace_protected_separator(file):
41 i = find_token(lines, "\\protected_separator", i)
44 j = find_token_backwards(lines, "\\layout", i)
45 #if j == -1: print error
46 layout = get_layout(lines[j], file.default_layout)
48 if layout == "LyX-Code":
50 while lines[i] == "\\protected_separator ":
54 lines[i-1] = lines[i-1] + result + lines[i]
56 lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
61 def merge_formula_inset(file):
65 i = find_token(lines, "\\begin_inset Formula", i)
67 if lines[i+1] in math_env:
68 lines[i] = lines[i] + lines[i+1]
73 # Update from tabular format 4 to 5 if necessary
74 def update_tabular(file):
76 lyxtable_re = re.compile(r".*\\LyXTable$")
79 i = find_re(lines, lyxtable_re, i)
89 rows = int(string.split(lines[i])[0])
90 columns = int(string.split(lines[i])[1])
93 for j in range(columns):
94 col_info = string.split(lines[i])
95 if len(col_info) == 3:
96 lines[i] = lines[i] + '"" ""'
98 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
102 lines[i] = lines[i] + ' "" ""'
106 def update_toc(file):
110 i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
113 lines[i] = lines[i] + '{}'
117 def remove_cursor(file):
119 i = find_token(lines, '\\cursor', 0)
124 def remove_vcid(file):
126 i = find_token(lines, '\\lyxvcid', 0)
129 i = find_token(lines, '\\lyxrcsid', 0)
134 def first_layout(file):
136 while (lines[0] == ""):
138 if lines[0][:7] != "\\layout":
139 lines[:0] = ['\\layout %s' % file.default_layout, '']
142 def remove_space_in_units(file):
144 margins = ["\\topmargin","\\rightmargin",
145 "\\leftmargin","\\bottommargin"]
147 unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
149 for margin in margins:
152 i = find_token(lines, margin, i)
156 result = unit_rexp.search(lines[i])
158 lines[i] = margin + " " + result.group(1) + result.group(2)
162 def latexdel_getargs(file, i):
165 # play safe, clean empty lines
171 j = find_token(lines, '\\end_inset', i)
176 file.warning("Unexpected end of inset.")
177 j = find_token(lines, '\\begin_inset LatexDel }{', i)
179 ref = string.join(lines[i:j])
182 # play safe, clean empty lines
188 j = find_token(lines, '\\end_inset', i - 1)
192 file.warning("Unexpected end of inset.")
193 j = find_token(lines, '\\begin_inset LatexDel }', i)
194 label = string.join(lines[i:j])
200 def update_ref(file):
204 i = find_token(lines, '\\begin_inset LatexCommand', i)
208 if string.split(lines[i])[-1] == "\\ref{":
210 ref, label = latexdel_getargs(file, i)
211 lines[i - 1] = "%s[%s]{%s}" % (lines[i - 1][:-1], ref, label)
216 def update_latexdel(file):
219 latexdel_re = re.compile(r".*\\begin_inset LatexDel")
221 i = find_re(lines, latexdel_re, i)
224 lines[i] = string.replace(lines[i],'\\begin_inset LatexDel', '\\begin_inset LatexCommand')
226 j = string.find(lines[i],'\\begin_inset')
227 lines.insert(i+1, lines[i][j:])
228 lines[i] = string.strip(lines[i][:j])
231 if string.split(lines[i])[-1] in ("\\url{", "\\htmlurl{"):
234 ref, label = latexdel_getargs(file, i)
235 lines[i -1] = "%s[%s]{%s}" % (lines[i-1][:-1], label, ref)
240 convert = [[216, [first_layout, remove_vcid, remove_cursor, update_toc,
241 replace_protected_separator, merge_formula_inset,
242 update_tabular, remove_space_in_units, update_ref, update_latexdel]]]
245 if __name__ == "__main__":