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(file):
31 i = find_token(lines, "\\protected_separator", i)
34 j = find_token_backwards(lines, "\\layout", i)
35 #if j == -1: print error
36 layout = layout_exp.match(lines[j]).group(1)
38 if layout == "LyX-Code":
40 while lines[i] == "\\protected_separator ":
44 lines[i-1] = lines[i-1] + result + lines[i]
46 lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
51 def merge_formula_inset(file):
55 i = find_token(lines, "\\begin_inset Formula", i)
57 if lines[i+1] in math_env:
58 lines[i] = lines[i] + lines[i+1]
63 # Update from tabular format 4 to 5 if necessary
64 def update_tabular(file):
66 lyxtable_re = re.compile(r".*\\LyXTable$")
69 i = find_re(lines, lyxtable_re, i)
79 rows = int(string.split(lines[i])[0])
80 columns = int(string.split(lines[i])[1])
83 for j in range(columns):
84 col_info = string.split(lines[i])
85 if len(col_info) == 3:
86 lines[i] = lines[i] + '"" ""'
88 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
92 lines[i] = lines[i] + ' "" ""'
100 i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
103 lines[i] = lines[i] + '{}'
107 def remove_cursor(file):
109 i = find_token(lines, '\\cursor', 0)
114 def remove_vcid(file):
116 i = find_token(lines, '\\lyxvcid', 0)
119 i = find_token(lines, '\\lyxrcsid', 0)
124 def first_layout(file):
126 while (lines[0] == ""):
128 if lines[0][:7] != "\\layout":
129 lines[:0] = ["\\layout Standard"]
132 def remove_space_in_units(file):
134 margins = ["\\topmargin","\\rightmargin",
135 "\\leftmargin","\\bottommargin"]
137 unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
139 begin_preamble = find_token(lines,"\\begin_preamble", 0)
140 end_preamble = find_token(lines, "\\end_preamble", 0)
141 for margin in margins:
144 i = find_token(lines, margin, i)
148 if i > begin_preamble and i < end_preamble:
152 result = unit_rexp.search(lines[i])
154 lines[i] = margin + " " + result.group(1) + result.group(2)
158 convert = [[216, [first_layout, remove_vcid, remove_cursor, update_toc,
159 replace_protected_separator, merge_formula_inset,
160 update_tabular, remove_space_in_units]]]
163 if __name__ == "__main__":