2002-08-21 17:25:47 +00:00
|
|
|
# This file is part of lyx2lyx
|
2006-08-02 14:19:22 +00:00
|
|
|
# Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
|
2002-08-21 17:25:47 +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-21 17:25:47 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
"""Convert files to the file format generated by lyx 1.1.6, fix3 and fix4"""
|
2006-08-02 14:19:22 +00:00
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
import re
|
2005-02-17 19:38:40 +00:00
|
|
|
from parser_tools import find_token, find_re
|
2002-08-21 17:25:47 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2003-01-07 14:39:54 +00:00
|
|
|
def bool_table(item):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Convert 0, 1 to false, true."
|
2003-01-07 14:39:54 +00:00
|
|
|
if item == "0":
|
|
|
|
return "false"
|
|
|
|
# should emit a warning if item != "1"
|
|
|
|
return "true"
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2005-09-09 15:23:09 +00:00
|
|
|
align_vertical = {"0": "top", "1": "bottom", "2": "center"}
|
2002-08-21 17:25:47 +00:00
|
|
|
align_table = {"0": "top", "2": "left", "4": "right", "8": "center"}
|
|
|
|
use_table = {"0": "none", "1": "parbox"}
|
2003-01-07 15:55:03 +00:00
|
|
|
table_meta_re = re.compile(r'<LyXTabular version="?1"? rows="?(\d*)"? columns="?(\d*)"?>')
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_tabular(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update tabular format to version 2 (xml like syntax)."
|
|
|
|
regexp = re.compile(r"^\\begin_inset\s+Tabular")
|
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:
|
2005-02-17 19:38:40 +00:00
|
|
|
i = find_re(lines, regexp, i)
|
2002-08-21 17:25:47 +00:00
|
|
|
if i == -1:
|
|
|
|
break
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
i = i + 1
|
2002-08-21 17:25:47 +00:00
|
|
|
|
|
|
|
# scan table header meta-info
|
2024-06-15 09:06:06 +00:00
|
|
|
res = table_meta_re.match(lines[i])
|
2003-01-07 15:55:03 +00:00
|
|
|
if res:
|
|
|
|
val = res.groups()
|
|
|
|
lines[i] = '<lyxtabular version="2" rows="%s" columns="%s">' % val
|
2002-08-21 17:25:47 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
j = find_token(lines, "</LyXTabular>", i) + 1
|
2002-08-21 17:25:47 +00:00
|
|
|
if j == 0:
|
2024-06-15 09:06:06 +00:00
|
|
|
document.warning("Error: Bad lyx format i=%d j=%d" % (i, j))
|
2002-08-21 17:25:47 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
new_table = table_update(lines[i:j])
|
2006-07-01 19:16:09 +00:00
|
|
|
lines[i:j] = new_table
|
2002-08-21 17:25:47 +00:00
|
|
|
i = i + len(new_table)
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
col_re = re.compile(
|
|
|
|
r'<column alignment="?(\d)"? valignment="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? width="(.*)" special="(.*)">'
|
|
|
|
)
|
|
|
|
cell_re = re.compile(
|
|
|
|
r'<cell multicolumn="?(\d)"? alignment="?(\d)"? valignment="?(\d)"? topline="?(\d)"? bottomline="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? rotate="?(\d)"? usebox="?(\d)"? width="(.*)" special="(.*)">'
|
|
|
|
)
|
|
|
|
features_re = re.compile(
|
|
|
|
r'<features rotate="?(\d)"? islongtable="?(\d)"? endhead="?(-?\d)"? endfirsthead="?(-?\d)"? endfoot="?(-?\d)"? endlastfoot="?(-?\d)"?>'
|
|
|
|
)
|
2003-01-07 15:55:03 +00:00
|
|
|
row_re = re.compile(r'<row topline="?(\d)"? bottomline="?(\d)"? newpage="?(\d)"?>')
|
2002-08-21 17:25:47 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2002-08-21 17:25:47 +00:00
|
|
|
def table_update(lines):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Update table's internal content to format 2."
|
|
|
|
lines[1] = lines[1].replace("<Features", "<features")
|
|
|
|
res = features_re.match(lines[1])
|
2002-08-21 17:25:47 +00:00
|
|
|
if res:
|
|
|
|
val = res.groups()
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[1] = (
|
|
|
|
f'<features rotate="{bool_table(val[0])}" islongtable="{bool_table(val[1])}" endhead="{val[2]}" endfirsthead="{val[3]}" endfoot="{val[4]}" endlastfoot="{val[5]}">'
|
|
|
|
)
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if lines[2] == "":
|
2002-08-21 17:25:47 +00:00
|
|
|
del lines[2]
|
|
|
|
i = 2
|
|
|
|
col_info = []
|
|
|
|
while i < len(lines):
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = lines[i].replace("<Cell", "<cell")
|
|
|
|
lines[i] = lines[i].replace("</Cell", "</cell")
|
|
|
|
lines[i] = lines[i].replace("<Row", "<row")
|
|
|
|
lines[i] = lines[i].replace("</Row", "</row")
|
|
|
|
lines[i] = lines[i].replace("<Column", "<column")
|
|
|
|
lines[i] = lines[i].replace("</Column", "</column")
|
|
|
|
lines[i] = lines[i].replace("</LyXTabular", "</lyxtabular")
|
|
|
|
k = lines[i].find("<column ")
|
2002-08-21 17:25:47 +00:00
|
|
|
if k != -1:
|
|
|
|
col_info.append(lines[i])
|
|
|
|
del lines[i]
|
|
|
|
continue
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if lines[i] == "</column>" or lines[i] == "<column>":
|
2002-08-21 17:25:47 +00:00
|
|
|
del lines[i]
|
|
|
|
continue
|
|
|
|
|
|
|
|
res = cell_re.match(lines[i])
|
|
|
|
if res:
|
|
|
|
val = res.groups()
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = (
|
|
|
|
f'<cell multicolumn="{val[0]}" alignment="{align_table[val[1]]}" valignment="{align_vertical[val[2]]}" topline="{bool_table(val[3])}" bottomline="{bool_table(val[4])}" leftline="{bool_table(val[5])}" rightline="{bool_table(val[6])}" rotate="{bool_table(val[7])}" usebox="{use_table[val[8]]}" width="{val[9]}" special="{val[10]}">'
|
|
|
|
)
|
2002-08-21 17:25:47 +00:00
|
|
|
|
|
|
|
res = row_re.match(lines[i])
|
|
|
|
if res:
|
|
|
|
val = res.groups()
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[i] = (
|
|
|
|
f'<row topline="{bool_table(val[0])}" bottomline="{bool_table(val[1])}" newpage="{bool_table(val[2])}">'
|
|
|
|
)
|
2002-08-21 17:25:47 +00:00
|
|
|
|
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
j = len(col_info)
|
|
|
|
for i in range(j):
|
|
|
|
res = col_re.match(col_info[i])
|
|
|
|
if res:
|
|
|
|
val = res.groups()
|
2024-06-15 09:06:06 +00:00
|
|
|
col_info[i] = (
|
|
|
|
'<column alignment="%s" valignment="%s" leftline="%s" rightline="%s" width="%s" special="%s">'
|
|
|
|
% (
|
|
|
|
align_table[val[0]],
|
|
|
|
align_vertical[val[1]],
|
|
|
|
bool_table(val[2]),
|
|
|
|
bool_table(val[3]),
|
|
|
|
val[4],
|
|
|
|
val[5],
|
|
|
|
)
|
|
|
|
)
|
2002-08-21 17:25:47 +00:00
|
|
|
|
|
|
|
return lines[:2] + col_info + lines[2:]
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
supported_versions = ["1.1.6fix3", "1.1.6fix4", "1.1"]
|
2005-01-05 18:52:59 +00:00
|
|
|
convert = [[218, [update_tabular]]]
|
2024-06-15 09:06:06 +00:00
|
|
|
revert = []
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2002-08-21 17:25:47 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|