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
24 layout_exp = re.compile(r"\\layout (\S*)")
25 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
27 def replace_protected_separator(lines):
30 i = find_token(lines, "\\protected_separator", i)
33 j = find_token_backwards(lines, "\\layout", i)
34 #if j == -1: print error
35 layout = layout_exp.match(lines[j]).group(1)
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(lines):
53 i = find_token(lines, "\\begin_inset Formula", i)
55 if lines[i+1] in math_env:
56 lines[i] = lines[i] + lines[i+1]
61 # Update from tabular format 4 to 5 if necessary
62 def update_tabular(lines):
63 lyxtable_re = re.compile(r".*\\LyXTable$")
66 i = find_re(lines, lyxtable_re, i)
76 rows = int(string.split(lines[i])[0])
77 columns = int(string.split(lines[i])[1])
80 for j in range(columns):
81 col_info = string.split(lines[i])
82 if len(col_info) == 3:
83 lines[i] = lines[i] + '"" ""'
85 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
89 lines[i] = lines[i] + ' "" ""'
93 def update_toc(lines):
96 i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
99 lines[i] = lines[i] + '{}'
103 def remove_cursor(lines):
104 i = find_token(lines, '\\cursor', 0)
109 def remove_vcid(lines):
110 i = find_token(lines, '\\lyxvcid', 0)
113 i = find_token(lines, '\\lyxrcsid', 0)
118 def first_layout(lines):
119 while (lines[0] == ""):
121 if lines[0][:7] != "\\layout":
122 lines[:0] = ["\\layout Standard"]
125 def remove_space_in_units(lines):
126 margins = ["\\topmargin","\\rightmargin",
127 "\\leftmargin","\\bottommargin"]
129 unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
131 begin_preamble = find_token(lines,"\\begin_preamble", 0)
132 end_preamble = find_token(lines, "\\end_preamble", 0)
133 for margin in margins:
136 i = find_token(lines, margin, i)
140 if i > begin_preamble and i < end_preamble:
144 result = unit_rexp.search(lines[i])
146 lines[i] = margin + " " + result.group(1) + result.group(2)
151 first_layout(file.body)
152 remove_vcid(file.header)
153 remove_cursor(file.body)
154 update_toc(file.body)
155 replace_protected_separator(file.body)
156 merge_formula_inset(file.body)
157 update_tabular(file.body)
158 remove_space_in_units(file.header)
163 file.error("The convertion to an older format (%s) is not implemented." % file.format)
165 if __name__ == "__main__":