2002-08-01 15:26:32 +00:00
|
|
|
|
# This file is part of lyx2lyx
|
2004-02-02 21:39:17 +00:00
|
|
|
|
# -*- coding: iso-8859-1 -*-
|
2004-04-14 08:45:46 +00:00
|
|
|
|
# Copyright (C) 2002-2004 Jos<6F> Matos <jamatos@lyx.org>
|
2002-08-01 15:26:32 +00:00
|
|
|
|
#
|
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
|
|
import re
|
2004-04-14 08:45:46 +00:00
|
|
|
|
import string
|
|
|
|
|
from parser_tools import find_token, find_token_backwards, find_re
|
2002-08-01 15:26:32 +00:00
|
|
|
|
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
layout_exp = re.compile(r"\\layout (\S*)")
|
2002-08-01 15:26:32 +00:00
|
|
|
|
math_env = ["\\[","\\begin{eqnarray*}","\\begin{eqnarray}","\\begin{equation}"]
|
|
|
|
|
|
|
|
|
|
def replace_protected_separator(lines):
|
|
|
|
|
i=0
|
|
|
|
|
while 1:
|
|
|
|
|
i = find_token(lines, "\\protected_separator", i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
j = find_token_backwards(lines, "\\layout", i)
|
|
|
|
|
#if j == -1: print error
|
|
|
|
|
layout = layout_exp.match(lines[j]).group(1)
|
|
|
|
|
|
|
|
|
|
if layout == "LyX-Code":
|
|
|
|
|
result = ""
|
|
|
|
|
while lines[i] == "\\protected_separator ":
|
|
|
|
|
result = result + " "
|
|
|
|
|
del lines[i]
|
|
|
|
|
|
2002-08-26 18:28:13 +00:00
|
|
|
|
lines[i-1] = lines[i-1] + result + lines[i]
|
2002-08-01 15:26:32 +00:00
|
|
|
|
else:
|
|
|
|
|
lines[i-1] = lines[i-1]+ "\\SpecialChar ~"
|
|
|
|
|
|
2002-08-26 18:28:13 +00:00
|
|
|
|
del lines[i]
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2002-08-01 15:26:32 +00:00
|
|
|
|
def merge_formula_inset(lines):
|
|
|
|
|
i=0
|
|
|
|
|
while 1:
|
|
|
|
|
i = find_token(lines, "\\begin_inset Formula", i)
|
|
|
|
|
if i == -1: break
|
|
|
|
|
if lines[i+1] in math_env:
|
|
|
|
|
lines[i] = lines[i] + lines[i+1]
|
|
|
|
|
del lines[i+1]
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2002-08-27 15:23:52 +00:00
|
|
|
|
# Update from tabular format 4 to 5 if necessary
|
|
|
|
|
def update_tabular(lines):
|
|
|
|
|
lyxtable_re = re.compile(r".*\\LyXTable$")
|
|
|
|
|
i=0
|
|
|
|
|
while 1:
|
|
|
|
|
i = find_re(lines, lyxtable_re, i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
i = i + 1
|
|
|
|
|
format = lines[i][8]
|
|
|
|
|
if format != '4':
|
|
|
|
|
continue
|
2003-10-13 09:50:10 +00:00
|
|
|
|
|
2002-08-27 15:23:52 +00:00
|
|
|
|
lines[i]='multicol5'
|
|
|
|
|
i = i + 1
|
|
|
|
|
rows = int(string.split(lines[i])[0])
|
|
|
|
|
columns = int(string.split(lines[i])[1])
|
|
|
|
|
|
|
|
|
|
i = i + rows + 1
|
|
|
|
|
for j in range(columns):
|
|
|
|
|
col_info = string.split(lines[i])
|
|
|
|
|
if len(col_info) == 3:
|
|
|
|
|
lines[i] = lines[i] + '"" ""'
|
|
|
|
|
else:
|
|
|
|
|
lines[i] = string.join(col_info[:3]) + ' "%s" ""' % col_info[3]
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
|
while lines[i]:
|
|
|
|
|
lines[i] = lines[i] + ' "" ""'
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2002-08-27 15:23:52 +00:00
|
|
|
|
def update_toc(lines):
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
|
|
|
|
i = find_token(lines, '\\begin_inset LatexCommand \\tableofcontents', i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
lines[i] = lines[i] + '{}'
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2003-01-05 22:08:22 +00:00
|
|
|
|
def remove_cursor(lines):
|
|
|
|
|
i = find_token(lines, '\\cursor', 0)
|
|
|
|
|
if i != -1:
|
|
|
|
|
del lines[i]
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2003-01-05 22:08:22 +00:00
|
|
|
|
def remove_vcid(lines):
|
|
|
|
|
i = find_token(lines, '\\lyxvcid', 0)
|
|
|
|
|
if i != -1:
|
|
|
|
|
del lines[i]
|
|
|
|
|
i = find_token(lines, '\\lyxrcsid', 0)
|
|
|
|
|
if i != -1:
|
|
|
|
|
del lines[i]
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2003-06-09 08:15:45 +00:00
|
|
|
|
def first_layout(lines):
|
|
|
|
|
while (lines[0] == ""):
|
|
|
|
|
del lines[0]
|
|
|
|
|
if lines[0][:7] != "\\layout":
|
|
|
|
|
lines[:0] = ["\\layout Standard"]
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2003-06-06 15:48:35 +00:00
|
|
|
|
def remove_space_in_units(lines):
|
|
|
|
|
margins = ["\\topmargin","\\rightmargin",
|
|
|
|
|
"\\leftmargin","\\bottommargin"]
|
|
|
|
|
|
|
|
|
|
unit_rexp = re.compile(r'[^ ]* (.*) (.*)')
|
|
|
|
|
|
|
|
|
|
begin_preamble = find_token(lines,"\\begin_preamble", 0)
|
|
|
|
|
end_preamble = find_token(lines, "\\end_preamble", 0)
|
|
|
|
|
for margin in margins:
|
|
|
|
|
i = 0
|
|
|
|
|
while 1:
|
|
|
|
|
i = find_token(lines, margin, i)
|
|
|
|
|
if i == -1:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
if i > begin_preamble and i < end_preamble:
|
|
|
|
|
i = i + 1
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
result = unit_rexp.search(lines[i])
|
|
|
|
|
if result:
|
|
|
|
|
lines[i] = margin + " " + result.group(1) + result.group(2)
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
|
2004-08-15 16:29:04 +00:00
|
|
|
|
def convert(file):
|
|
|
|
|
first_layout(file.body)
|
|
|
|
|
remove_vcid(file.header)
|
|
|
|
|
remove_cursor(file.body)
|
|
|
|
|
update_toc(file.body)
|
|
|
|
|
replace_protected_separator(file.body)
|
|
|
|
|
merge_formula_inset(file.body)
|
|
|
|
|
update_tabular(file.body)
|
|
|
|
|
remove_space_in_units(file.header)
|
|
|
|
|
file.format = 216
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def revert(file):
|
|
|
|
|
file.error("The convertion to an older format (%s) is not implemented." % file.format)
|
2002-08-01 15:26:32 +00:00
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
pass
|