2006-08-02 14:19:22 +00:00
|
|
|
# This document is part of lyx2lyx
|
|
|
|
# Copyright (C) 2002-2004 José 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
|
2011-08-25 23:10:36 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-08-01 15:26:32 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
"""Convert files to the file format generated by lyx 1.1.5"""
|
2006-08-02 14:19:22 +00:00
|
|
|
|
2002-08-01 15:26:32 +00:00
|
|
|
import re
|
2024-06-18 09:25:52 +00:00
|
|
|
|
|
|
|
from parser_tools import find_re, find_token, find_token_backwards
|
2002-08-01 15:26:32 +00:00
|
|
|
|
2006-07-27 18:30:13 +00:00
|
|
|
####################################################################
|
|
|
|
# Private helper functions
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2006-07-27 18:30:13 +00:00
|
|
|
def get_layout(line, default_layout):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Get the line layout, beware of the empty layout."
|
2006-08-02 15:45:44 +00:00
|
|
|
tokens = line.split()
|
2006-07-27 18:30:13 +00:00
|
|
|
if len(tokens) > 1:
|
|
|
|
return tokens[1]
|
|
|
|
return default_layout
|
|
|
|
|
|
|
|
|
|
|
|
####################################################################
|
2002-08-01 15:26:32 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
math_env = ["\\[", "\\begin{eqnarray*}", "\\begin{eqnarray}", "\\begin{equation}"]
|
|
|
|
|
2002-08-01 15:26:32 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def replace_protected_separator(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Replace protected separator."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2024-06-15 09:06:06 +00:00
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2002-08-01 15:26:32 +00:00
|
|
|
i = find_token(lines, "\\protected_separator", i)
|
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
j = find_token_backwards(lines, "\\layout", i)
|
2024-06-15 09:06:06 +00:00
|
|
|
# if j == -1: print error
|
2006-08-02 14:19:22 +00:00
|
|
|
layout = get_layout(lines[j], document.default_layout)
|
2002-08-01 15:26:32 +00:00
|
|
|
|
|
|
|
if layout == "LyX-Code":
|
|
|
|
result = ""
|
|
|
|
while lines[i] == "\\protected_separator ":
|
|
|
|
result = result + " "
|
|
|
|
del lines[i]
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i - 1] = lines[i - 1] + result + lines[i]
|
2002-08-01 15:26:32 +00:00
|
|
|
else:
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i - 1] = lines[i - 1] + "\\SpecialChar ~"
|
2002-08-01 15:26:32 +00:00
|
|
|
|
2002-08-26 18:28:13 +00:00
|
|
|
del lines[i]
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def merge_formula_inset(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Merge formula insets."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2024-06-15 09:06:06 +00:00
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2002-08-01 15:26:32 +00:00
|
|
|
i = find_token(lines, "\\begin_inset Formula", i)
|
2024-06-15 09:06:06 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
if lines[i + 1] in math_env:
|
|
|
|
lines[i] = lines[i] + lines[i + 1]
|
|
|
|
del lines[i + 1]
|
2002-08-01 15:26:32 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_tabular(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update from tabular format 4 to 5 if necessary."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2002-08-27 15:23:52 +00:00
|
|
|
lyxtable_re = re.compile(r".*\\LyXTable$")
|
2024-06-15 09:06:06 +00:00
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2002-08-27 15:23:52 +00:00
|
|
|
i = find_re(lines, lyxtable_re, i)
|
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
i = i + 1
|
|
|
|
format = lines[i][8]
|
2024-06-15 09:06:06 +00:00
|
|
|
if format != "4":
|
2002-08-27 15:23:52 +00:00
|
|
|
continue
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = "multicol5"
|
2002-08-27 15:23:52 +00:00
|
|
|
i = i + 1
|
2006-08-02 15:45:44 +00:00
|
|
|
rows = int(lines[i].split()[0])
|
|
|
|
columns = int(lines[i].split()[1])
|
2002-08-27 15:23:52 +00:00
|
|
|
|
|
|
|
i = i + rows + 1
|
|
|
|
for j in range(columns):
|
2006-08-02 15:45:44 +00:00
|
|
|
col_info = lines[i].split()
|
2002-08-27 15:23:52 +00:00
|
|
|
if len(col_info) == 3:
|
|
|
|
lines[i] = lines[i] + '"" ""'
|
|
|
|
else:
|
2006-08-02 16:13:01 +00:00
|
|
|
lines[i] = " ".join(col_info[:3]) + ' "%s" ""' % col_info[3]
|
2002-08-27 15:23:52 +00:00
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
while lines[i]:
|
|
|
|
lines[i] = lines[i] + ' "" ""'
|
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_toc(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update table of contents."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2002-08-27 15:23:52 +00:00
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\begin_inset LatexCommand \\tableofcontents", i)
|
2002-08-27 15:23:52 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i] + "{}"
|
2002-08-27 15:23:52 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def remove_cursor(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove cursor."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\cursor", 0)
|
2003-01-05 22:08:22 +00:00
|
|
|
if i != -1:
|
|
|
|
del lines[i]
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def remove_vcid(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove \\lyxvcid and \\lyxrcsid."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.header
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\lyxvcid", 0)
|
2003-01-05 22:08:22 +00:00
|
|
|
if i != -1:
|
|
|
|
del lines[i]
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\lyxrcsid", 0)
|
2003-01-05 22:08:22 +00:00
|
|
|
if i != -1:
|
|
|
|
del lines[i]
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def first_layout(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Fix first layout, if empty use the default layout."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2024-06-15 09:06:06 +00:00
|
|
|
while lines[0] == "":
|
2003-06-09 08:15:45 +00:00
|
|
|
del lines[0]
|
|
|
|
if lines[0][:7] != "\\layout":
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[:0] = ["\\layout %s" % document.default_layout, ""]
|
2003-06-09 08:15:45 +00:00
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def remove_space_in_units(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove space in units."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.header
|
2024-06-15 09:06:06 +00:00
|
|
|
margins = ["\\topmargin", "\\rightmargin", "\\leftmargin", "\\bottommargin"]
|
2003-06-06 15:48:35 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
unit_rexp = re.compile(r"[^ ]* (.*) (.*)")
|
2003-06-06 15:48:35 +00:00
|
|
|
|
|
|
|
for margin in margins:
|
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2003-06-06 15:48:35 +00:00
|
|
|
i = find_token(lines, margin, i)
|
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
|
|
|
|
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
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def latexdel_getargs(document, i):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Get arguments from latexdel insets."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2005-07-08 08:49:29 +00:00
|
|
|
|
|
|
|
# play safe, clean empty lines
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2005-07-08 08:49:29 +00:00
|
|
|
if lines[i]:
|
|
|
|
break
|
|
|
|
del lines[i]
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
j = find_token(lines, "\\end_inset", i)
|
2005-07-08 08:49:29 +00:00
|
|
|
|
|
|
|
if i == j:
|
|
|
|
del lines[i]
|
|
|
|
else:
|
2006-08-02 14:19:22 +00:00
|
|
|
document.warning("Unexpected end of inset.")
|
2024-06-15 09:06:06 +00:00
|
|
|
j = find_token(lines, "\\begin_inset LatexDel }{", i)
|
2005-07-08 08:49:29 +00:00
|
|
|
|
2006-08-02 16:13:01 +00:00
|
|
|
ref = " ".join(lines[i:j])
|
2024-06-15 09:06:06 +00:00
|
|
|
del lines[i : j + 1]
|
2005-07-08 08:49:29 +00:00
|
|
|
|
|
|
|
# play safe, clean empty lines
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2005-07-08 08:49:29 +00:00
|
|
|
if lines[i]:
|
|
|
|
break
|
|
|
|
del lines[i]
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
j = find_token(lines, "\\end_inset", i - 1)
|
2005-07-08 08:49:29 +00:00
|
|
|
if i == j:
|
|
|
|
del lines[i]
|
|
|
|
else:
|
2006-08-02 14:19:22 +00:00
|
|
|
document.warning("Unexpected end of inset.")
|
2024-06-15 09:06:06 +00:00
|
|
|
j = find_token(lines, "\\begin_inset LatexDel }", i)
|
2006-08-02 16:13:01 +00:00
|
|
|
label = " ".join(lines[i:j])
|
2024-06-15 09:06:06 +00:00
|
|
|
del lines[i : j + 1]
|
2005-07-08 08:49:29 +00:00
|
|
|
|
|
|
|
return ref, label
|
|
|
|
|
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_ref(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update reference inset."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2005-07-08 08:49:29 +00:00
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\begin_inset LatexCommand", i)
|
2005-07-08 08:49:29 +00:00
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
|
2006-08-02 15:45:44 +00:00
|
|
|
if lines[i].split()[-1] == "\\ref{":
|
2005-07-08 08:49:29 +00:00
|
|
|
i = i + 1
|
2006-08-02 14:19:22 +00:00
|
|
|
ref, label = latexdel_getargs(document, i)
|
2024-06-10 09:55:40 +00:00
|
|
|
lines[i - 1] = f"{lines[i - 1][:-1]}[{ref}]{{{label}}}"
|
2005-07-08 08:49:29 +00:00
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_latexdel(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove latexdel insets."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2005-07-08 08:49:29 +00:00
|
|
|
i = 0
|
|
|
|
latexdel_re = re.compile(r".*\\begin_inset LatexDel")
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2005-07-08 08:49:29 +00:00
|
|
|
i = find_re(lines, latexdel_re, i)
|
|
|
|
if i == -1:
|
|
|
|
return
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
|
2005-07-08 08:49:29 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
j = lines[i].find("\\begin_inset")
|
|
|
|
lines.insert(i + 1, lines[i][j:])
|
2006-08-02 15:45:44 +00:00
|
|
|
lines[i] = lines[i][:j].strip()
|
2005-07-08 08:49:29 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2006-08-02 15:45:44 +00:00
|
|
|
if lines[i].split()[-1] in ("\\url{", "\\htmlurl{"):
|
2005-07-08 08:49:29 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
ref, label = latexdel_getargs(document, i)
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i - 1] = f"{lines[i-1][:-1]}[{label}]{{{ref}}}"
|
2005-07-08 08:49:29 +00:00
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
supported_versions = ["1.1.5", "1.1.5fix1", "1.1.5fix2", "1.1"]
|
|
|
|
convert = [
|
|
|
|
[
|
|
|
|
216,
|
|
|
|
[
|
|
|
|
first_layout,
|
|
|
|
remove_vcid,
|
|
|
|
remove_cursor,
|
|
|
|
update_toc,
|
|
|
|
replace_protected_separator,
|
|
|
|
merge_formula_inset,
|
|
|
|
update_tabular,
|
|
|
|
remove_space_in_units,
|
|
|
|
update_ref,
|
|
|
|
update_latexdel,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
revert = []
|
2002-08-01 15:26:32 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|