lyx_mirror/lib/lyx2lyx/lyx_1_3.py

135 lines
4.2 KiB
Python
Raw Normal View History

# This file is part of lyx2lyx
# Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
# Copyright (C) 2004 José Matos <jamatos@lyx.org>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
2024-06-15 09:06:06 +00:00
"""Convert files to the file format generated by lyx 1.3"""
import re
from parser_tools import find_end_of, find_token, find_token_exact, get_value
####################################################################
# Private helper functions
2024-06-15 09:06:06 +00:00
def find_end_of_inset(lines, i):
r"Finds the matching \end_inset"
return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
def del_token(lines, token, start, end):
2024-06-15 09:06:06 +00:00
"""del_token(lines, token, start, end) -> int
Find the lower line in lines where token is the first element and
delete that line.
Returns the number of lines remaining."""
k = find_token_exact(lines, token, start, end)
if k == -1:
return end
else:
del lines[k]
return end - 1
2024-06-15 09:06:06 +00:00
# End of helper functions
####################################################################
def change_insetgraphics(document):
2024-06-15 09:06:06 +00:00
"Change inset Graphics."
lines = document.body
i = 0
2016-06-25 21:37:13 +00:00
while True:
i = find_token(lines, "\\begin_inset Graphics", i)
if i == -1:
break
j = find_end_of_inset(lines, i)
lines[i] = "\\begin_inset Graphics"
if get_value(lines, "display", i, j) == "default":
j = del_token(lines, "display", i, j)
if get_value(lines, "rotateOrigin", i, j) == "leftBaseline":
j = del_token(lines, "rotateOrigin", i, j)
k = find_token_exact(lines, "rotate", i, j)
if k != -1:
del lines[k]
2024-06-15 09:06:06 +00:00
j = j - 1
else:
j = del_token(lines, "rotateAngle", i, j)
k = find_token_exact(lines, "size_type", i, j)
if k == -1:
k = find_token_exact(lines, "size_kind", i, j)
if k != -1:
size_type = lines[k].split()[1]
del lines[k]
2024-06-15 09:06:06 +00:00
j = j - 1
if size_type in ["0", "original"]:
j = del_token(lines, "width", i, j)
j = del_token(lines, "height", i, j)
j = del_token(lines, "scale", i, j)
elif size_type in ["2", "scale"]:
j = del_token(lines, "width", i, j)
j = del_token(lines, "height", i, j)
if get_value(lines, "scale", i, j) == "100":
j = del_token(lines, "scale", i, j)
else:
j = del_token(lines, "scale", i, j)
k = find_token_exact(lines, "lyxsize_type", i, j)
if k == -1:
k = find_token_exact(lines, "lyxsize_kind", i, j)
if k != -1:
lyxsize_type = lines[k].split()[1]
del lines[k]
2024-06-15 09:06:06 +00:00
j = j - 1
j = del_token(lines, "lyxwidth", i, j)
j = del_token(lines, "lyxheight", i, j)
2024-06-15 09:06:06 +00:00
if (
lyxsize_type not in ["2", "scale"]
or get_value(lines, "lyxscale", i, j) == "100"
):
j = del_token(lines, "lyxscale", i, j)
2024-06-15 09:06:06 +00:00
i = i + 1
def change_tabular(document):
2024-06-15 09:06:06 +00:00
"Change tabular."
lines = document.body
i = 0
2016-06-25 21:37:13 +00:00
while True:
i = find_token(lines, "<column", i)
if i == -1:
break
if not re.search('width="0pt"', lines[i]):
2024-06-15 09:06:06 +00:00
lines[i] = re.sub(' alignment=".*?"', ' alignment="block"', lines[i])
i = i + 1
supported_versions = ["1.3.%d" % i for i in range(8)] + ["1.3"]
convert = [[221, [change_insetgraphics, change_tabular]]]
2024-06-15 09:06:06 +00:00
revert = []
if __name__ == "__main__":
pass