1 # This file is part of lyx2lyx
2 # -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2002 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.
20 from parser_tools import *
22 layout_exp = re.compile(r"\\layout (\S*)")
24 math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
26 def replace_protected_separator(lines):
29 i = find_token(lines, "\\protected_separator", i)
32 j = find_token_backwards(lines, "\\layout", i)
33 #if j == -1: print error
34 layout = layout_exp.match(lines[j]).group(1)
36 if layout == "LyX-Code":
38 while lines[i] == "\\protected_separator ":
42 lines[i-1] = lines[i-1] + result + lines[i]
44 lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
48 def merge_formula_inset(lines):
51 i = find_token(lines, "\\begin_inset Formula", i)
53 if lines[i+1] in math_env:
54 lines[i] = lines[i] + lines[i+1]
58 # Update from tabular format 4 to 5 if necessary
59 def update_tabular(lines):
60 lyxtable_re = re.compile(r".*\\LyXTable$")
63 i = find_re(lines, lyxtable_re, i)
73 rows = int(string.split(lines[i])[0])
74 columns = int(string.split(lines[i])[1])
77 for j in range(columns):
78 col_info = string.split(lines[i])
79 if len(col_info) == 3:
80 lines[i] = lines[i] + '"" ""'
82 lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
86 lines[i] = lines[i] + ' "" ""'
89 def update_toc(lines):
92 i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
95 lines[i] = lines[i] + '{}'
98 def remove_cursor(lines):
99 i = find_token(lines, '\\cursor', 0)
103 def remove_vcid(lines):
104 i = find_token(lines, '\\lyxvcid', 0)
107 i = find_token(lines, '\\lyxrcsid', 0)
111 def first_layout(lines):
112 while (lines[0] == ""):
114 if lines[0][:7] != "\\layout":
115 lines[:0] = ["\\layout Standard"]
117 def remove_space_in_units(lines):
118 margins = ["\\topmargin","\\rightmargin",
119 "\\leftmargin","\\bottommargin"]
121 unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
123 begin_preamble = find_token(lines,"\\begin_preamble", 0)
124 end_preamble = find_token(lines, "\\end_preamble", 0)
125 for margin in margins:
128 i = find_token(lines, margin, i)
132 if i > begin_preamble and i < end_preamble:
136 result = unit_rexp.search(lines[i])
138 lines[i] = margin + " " + result.group(1) + result.group(2)
141 def convert(header,body):
146 replace_protected_separator(body)
147 merge_formula_inset(body)
149 remove_space_in_units(header)
151 if __name__ == "__main__":