2003-08-22 16:15:26 +00:00
|
|
|
# This file is part of lyx2lyx
|
2006-08-02 14:19:22 +00:00
|
|
|
# Copyright (C) 2003-2004 José Matos <jamatos@lyx.org>
|
2003-08-22 16:15:26 +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
|
2003-08-22 16:15:26 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
"""Convert files to the file format generated by lyx 0.12"""
|
2006-08-02 14:19:22 +00:00
|
|
|
|
2003-08-22 16:15:26 +00:00
|
|
|
import re
|
2024-06-18 09:25:52 +00:00
|
|
|
|
|
|
|
from parser_tools import check_token, find_re, find_token
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2003-08-22 16:15:26 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def space_before_layout(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove empty line before \\layout."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2024-06-15 09:06:06 +00:00
|
|
|
i = 2 # skip first layout
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\layout", i)
|
2003-08-22 16:15:26 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
prot_space = lines[i - 2].find("\\protected_separator")
|
|
|
|
if lines[i - 1] == "" and prot_space == -1:
|
|
|
|
del lines[i - 1]
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def formula_inset_space_eat(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove space after inset formula."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2003-08-22 16:15:26 +00:00
|
|
|
i = find_token(lines, "\\begin_inset Formula", i)
|
2006-08-02 14:19:22 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
2003-08-22 16:15:26 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if len(lines[i]) > 22 and lines[i][21] == " ":
|
2003-08-22 16:15:26 +00:00
|
|
|
lines[i] = lines[i][:20] + lines[i][21:]
|
|
|
|
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 1 or 2 to 4."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2003-08-22 16:15:26 +00:00
|
|
|
lyxtable_re = re.compile(r".*\\LyXTable$")
|
2006-08-02 14:19:22 +00:00
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2003-08-22 16:15:26 +00:00
|
|
|
i = find_re(lines, lyxtable_re, i)
|
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
i = i + 1
|
2005-07-08 09:13:51 +00:00
|
|
|
format = lines[i][8:]
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = "multicol4"
|
2003-08-22 16:15:26 +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])
|
2003-08-22 16:15:26 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i] + " 0 0 -1 -1 -1 -1"
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
for j in range(rows):
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i] + " 0 0"
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
for j in range(columns):
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i] + " "
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2006-08-02 15:45:44 +00:00
|
|
|
while lines[i].strip():
|
2005-07-08 09:13:51 +00:00
|
|
|
if not format:
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i] + " 1 1"
|
|
|
|
lines[i] = lines[i] + " 0 0 0"
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2006-08-02 15:45:44 +00:00
|
|
|
lines[i] = lines[i].strip()
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
|
|
|
|
def final_dot(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Merge lines if the dot is the final character."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2003-08-22 16:15:26 +00:00
|
|
|
i = 0
|
|
|
|
while i < len(lines):
|
2024-06-15 09:06:06 +00:00
|
|
|
if (
|
|
|
|
lines[i][-1:] == "."
|
|
|
|
and lines[i + 1][:1] != "\\"
|
|
|
|
and lines[i + 1][:1] != " "
|
|
|
|
and len(lines[i]) + len(lines[i + 1]) <= 72
|
|
|
|
and lines[i + 1] != ""
|
|
|
|
):
|
|
|
|
lines[i] = lines[i] + lines[i + 1]
|
|
|
|
del lines[i + 1]
|
2003-08-22 16:15:26 +00:00
|
|
|
else:
|
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_inset_label(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update inset Label."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2003-08-22 16:15:26 +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 Label", i)
|
2003-08-22 16:15:26 +00:00
|
|
|
if i == -1:
|
|
|
|
return
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = "\\begin_inset LatexCommand \\label{" + lines[i][19:] + "}"
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_latexdel(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update inset LatexDel."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2003-08-22 16:15:26 +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 LatexDel", i)
|
2003-08-22 16:15:26 +00:00
|
|
|
if i == -1:
|
|
|
|
return
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i].replace("\\begin_inset LatexDel", "\\begin_inset LatexCommand")
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_vfill(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update fill_top and fill_bottom."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2003-08-22 16:15:26 +00:00
|
|
|
for i in range(len(lines)):
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i].replace("\\fill_top", "\\added_space_top vfill")
|
|
|
|
lines[i] = lines[i].replace("\\fill_bottom", "\\added_space_bottom vfill")
|
2003-08-22 16:15:26 +00:00
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_space_units(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update space units."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2024-06-15 09:06:06 +00:00
|
|
|
added_space_bottom = re.compile(r"\\added_space_bottom ([^ ]*)")
|
|
|
|
added_space_top = re.compile(r"\\added_space_top ([^ ]*)")
|
2003-08-22 16:15:26 +00:00
|
|
|
for i in range(len(lines)):
|
|
|
|
result = added_space_bottom.search(lines[i])
|
|
|
|
if result:
|
2024-06-15 09:06:06 +00:00
|
|
|
old = "\\added_space_bottom " + result.group(1)
|
|
|
|
new = "\\added_space_bottom " + str(float(result.group(1))) + "cm"
|
2006-08-02 15:45:44 +00:00
|
|
|
lines[i] = lines[i].replace(old, new)
|
2003-08-22 16:15:26 +00:00
|
|
|
|
|
|
|
result = added_space_top.search(lines[i])
|
|
|
|
if result:
|
2024-06-15 09:06:06 +00:00
|
|
|
old = "\\added_space_top " + result.group(1)
|
|
|
|
new = "\\added_space_top " + str(float(result.group(1))) + "cm"
|
2006-08-02 15:45:44 +00:00
|
|
|
lines[i] = lines[i].replace(old, new)
|
2003-08-22 16:15:26 +00:00
|
|
|
|
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, it is not saved on the file anymore."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2003-08-22 16:15:26 +00:00
|
|
|
i = 0
|
2024-06-15 09:06:06 +00:00
|
|
|
cursor_re = re.compile(r".*(\\cursor \d*)")
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2004-02-04 18:11:44 +00:00
|
|
|
i = find_re(lines, cursor_re, i)
|
2003-08-22 16:15:26 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
2004-02-04 18:11:44 +00:00
|
|
|
cursor = cursor_re.search(lines[i]).group(1)
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i].replace(cursor, "")
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def remove_empty_insets(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove empty insets."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2004-02-04 18:11:44 +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 ", i)
|
2004-02-04 18:11:44 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
2024-06-15 09:06:06 +00:00
|
|
|
if lines[i] == "\\begin_inset " and lines[i + 1] == "\\end_inset ":
|
2004-02-04 18:11:44 +00:00
|
|
|
del lines[i]
|
|
|
|
del lines[i]
|
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def remove_formula_latex(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Remove formula latex."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2004-02-04 18:11:44 +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, "\\latex formula_latex ", i)
|
2004-02-04 18:11:44 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
del lines[i]
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\latex default", i)
|
2004-02-04 18:11:44 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
del lines[i]
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def add_end_document(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Add \\the_end to the end of the document."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.body
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(lines, "\\the_end", 0)
|
2004-04-14 08:45:46 +00:00
|
|
|
if i == -1:
|
2024-06-15 09:06:06 +00:00
|
|
|
lines.append("\\the_end")
|
2003-08-22 16:15:26 +00:00
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def header_update(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update document header."
|
2006-08-02 14:19:22 +00:00
|
|
|
lines = document.header
|
2003-08-22 16:15:26 +00:00
|
|
|
i = 0
|
|
|
|
l = len(lines)
|
|
|
|
while i < l:
|
2024-06-15 09:06:06 +00:00
|
|
|
if lines[i][-1:] == " ":
|
2003-08-22 16:15:26 +00:00
|
|
|
lines[i] = lines[i][:-1]
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if check_token(lines[i], "\\epsfig"):
|
|
|
|
lines[i] = lines[i].replace("\\epsfig", "\\graphics")
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
continue
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if check_token(lines[i], "\\papersize"):
|
2006-08-02 15:45:44 +00:00
|
|
|
size = lines[i].split()[1]
|
2004-02-04 18:11:44 +00:00
|
|
|
new_size = size
|
|
|
|
paperpackage = ""
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if size == "usletter":
|
|
|
|
new_size = "letterpaper"
|
|
|
|
if size == "a4wide":
|
|
|
|
new_size = "Default"
|
2004-02-04 18:11:44 +00:00
|
|
|
paperpackage = "widemarginsa4"
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = "\\papersize " + new_size
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
2004-02-04 18:11:44 +00:00
|
|
|
if paperpackage:
|
2024-06-15 09:06:06 +00:00
|
|
|
lines.insert(i, "\\paperpackage " + paperpackage)
|
2004-02-04 18:11:44 +00:00
|
|
|
i = i + 1
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
lines.insert(i, "\\use_geometry 0")
|
|
|
|
lines.insert(i + 1, "\\use_amsmath 0")
|
2004-02-04 18:11:44 +00:00
|
|
|
i = i + 2
|
2003-08-22 16:15:26 +00:00
|
|
|
continue
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if check_token(lines[i], "\\baselinestretch"):
|
2006-08-02 15:45:44 +00:00
|
|
|
size = lines[i].split()[1]
|
2024-06-15 09:06:06 +00:00
|
|
|
if size == "1.00":
|
|
|
|
name = "single"
|
|
|
|
elif size == "1.50":
|
|
|
|
name = "onehalf"
|
|
|
|
elif size == "2.00":
|
|
|
|
name = "double"
|
2004-02-04 18:11:44 +00:00
|
|
|
else:
|
2024-06-15 09:06:06 +00:00
|
|
|
name = "other " + size
|
|
|
|
lines[i] = "\\spacing %s " % name
|
2003-08-22 16:15:26 +00:00
|
|
|
i = i + 1
|
|
|
|
continue
|
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_latexaccents(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update latex accent insets."
|
2006-08-02 14:19:22 +00:00
|
|
|
body = document.body
|
2004-12-02 12:29:21 +00:00
|
|
|
i = 1
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(body, "\\i ", i)
|
2004-12-02 12:29:21 +00:00
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
|
2006-08-02 15:45:44 +00:00
|
|
|
contents = body[i][2:].strip()
|
2004-12-02 12:29:21 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if contents.find("{") != -1 and contents.find("}") != -1:
|
2004-12-03 18:39:51 +00:00
|
|
|
i = i + 1
|
|
|
|
continue
|
|
|
|
|
2004-12-02 12:29:21 +00:00
|
|
|
if len(contents) == 2:
|
2024-06-15 09:06:06 +00:00
|
|
|
contents = contents + "{}"
|
2004-12-02 12:29:21 +00:00
|
|
|
elif len(contents) == 3:
|
2024-06-15 09:06:06 +00:00
|
|
|
contents = contents[:2] + "{" + contents[2] + "}"
|
2004-12-02 12:29:21 +00:00
|
|
|
elif len(contents) == 4:
|
2024-06-15 09:06:06 +00:00
|
|
|
if contents[2] == " ":
|
|
|
|
contents = contents[:2] + "{" + contents[3] + "}"
|
|
|
|
elif contents[2:4] == "\\i" or contents[2:4] == "\\j":
|
|
|
|
contents = contents[:2] + "{" + contents[2:] + "}"
|
2004-12-02 12:29:21 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
body[i] = "\\i " + contents
|
2004-12-02 12:29:21 +00:00
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def obsolete_latex_title(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Replace layout Latex_Title with Title."
|
2006-08-02 14:19:22 +00:00
|
|
|
body = document.body
|
2005-07-05 09:48:10 +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(body, "\\layout", i)
|
2005-07-05 09:48:10 +00:00
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if body[i].lower().find("latex_title") != -1:
|
|
|
|
body[i] = "\\layout Title"
|
2005-07-05 09:48:10 +00:00
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
|
2006-08-07 14:10:41 +00:00
|
|
|
def remove_inset_latex(document):
|
|
|
|
"Replace inset latex with layout LaTeX"
|
|
|
|
body = document.body
|
|
|
|
|
|
|
|
i = 0
|
2016-06-25 21:37:13 +00:00
|
|
|
while True:
|
2024-06-15 09:06:06 +00:00
|
|
|
i = find_token(body, "\\begin_inset Latex", i)
|
2006-08-07 14:10:41 +00:00
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
body[i] = body[i].replace("\\begin_inset Latex", "\\layout LaTeX")
|
|
|
|
i = find_token(body, "\\end_inset", i)
|
2006-08-07 14:10:41 +00:00
|
|
|
if i == -1:
|
2024-06-15 09:06:06 +00:00
|
|
|
# this should not happen
|
2006-08-07 14:10:41 +00:00
|
|
|
return
|
|
|
|
del body[i]
|
2024-06-15 09:06:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
supported_versions = ["0.12.0", "0.12.1", "0.12"]
|
|
|
|
convert = [
|
|
|
|
[
|
|
|
|
215,
|
|
|
|
[
|
|
|
|
header_update,
|
|
|
|
add_end_document,
|
|
|
|
remove_cursor,
|
|
|
|
final_dot,
|
|
|
|
update_inset_label,
|
|
|
|
update_latexdel,
|
|
|
|
update_space_units,
|
|
|
|
space_before_layout,
|
|
|
|
formula_inset_space_eat,
|
|
|
|
update_tabular,
|
|
|
|
update_vfill,
|
|
|
|
remove_empty_insets,
|
|
|
|
remove_formula_latex,
|
|
|
|
update_latexaccents,
|
|
|
|
obsolete_latex_title,
|
|
|
|
remove_inset_latex,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
]
|
|
|
|
revert = []
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2003-08-22 16:15:26 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|