2002-08-23 19:04:43 +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-23 19:04:43 +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-23 19:04:43 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
"""Convert files to the file format generated by lyx 1.1.6, until fix2"""
|
2006-08-02 14:19:22 +00:00
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
import re
|
2024-06-18 09:25:52 +00:00
|
|
|
|
|
|
|
from parser_tools import check_token, find_re, find_token, find_tokens
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2002-08-23 19:04:43 +00:00
|
|
|
lyxtable_re = re.compile(r".*\\LyXTable$")
|
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 to version 1 (xml like syntax)."
|
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-23 19:04:43 +00:00
|
|
|
i = find_re(lines, lyxtable_re, i)
|
|
|
|
if i == -1:
|
|
|
|
break
|
2024-06-15 09:06:06 +00:00
|
|
|
prop_dict = {
|
|
|
|
"family": "default",
|
|
|
|
"series": "default",
|
|
|
|
"shape": "default",
|
|
|
|
"size": "default",
|
|
|
|
"emph": "default",
|
|
|
|
"bar": "default",
|
|
|
|
"noun": "default",
|
|
|
|
"latex": "default",
|
|
|
|
"color": "default",
|
|
|
|
}
|
2002-08-24 12:05:17 +00:00
|
|
|
|
2002-08-23 19:04:43 +00:00
|
|
|
# remove \LyXTable
|
|
|
|
lines[i] = lines[i][:-9]
|
|
|
|
i = i + 1
|
2024-06-15 09:06:06 +00:00
|
|
|
lines.insert(i, "")
|
2002-08-23 19:04:43 +00:00
|
|
|
i = i + 1
|
|
|
|
lines[i] = "\\begin_inset Tabular"
|
|
|
|
i = i + 1
|
2006-08-02 15:45:44 +00:00
|
|
|
head = lines[i].split()
|
2002-08-23 19:04:43 +00:00
|
|
|
rows = int(head[0])
|
|
|
|
columns = int(head[1])
|
|
|
|
|
2002-08-25 18:45:10 +00:00
|
|
|
tabular_line = i
|
2024-06-15 09:06:06 +00:00
|
|
|
i = i + 1
|
|
|
|
lines.insert(
|
|
|
|
i,
|
|
|
|
f'<Features rotate="{head[2]}" islongtable="{head[3]}" endhead="{head[4]}" endfirsthead="{head[5]}" endfoot="{head[6]}" endlastfoot="{head[7]}">',
|
|
|
|
)
|
2002-08-23 19:04:43 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
i = i + 1
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2002-08-23 19:04:43 +00:00
|
|
|
row_info = []
|
2002-08-26 18:28:13 +00:00
|
|
|
cont_row = []
|
2002-08-23 19:04:43 +00:00
|
|
|
for j in range(rows):
|
2006-08-02 15:45:44 +00:00
|
|
|
row_info.append(lines[i].split())
|
2024-06-15 09:06:06 +00:00
|
|
|
if lines[i].split()[2] == "1":
|
2002-08-26 18:28:13 +00:00
|
|
|
cont_row.append(j)
|
2002-08-23 19:04:43 +00:00
|
|
|
del lines[i]
|
2002-08-26 18:28:13 +00:00
|
|
|
|
2002-08-23 19:04:43 +00:00
|
|
|
column_info = []
|
2002-08-26 18:28:13 +00:00
|
|
|
col_info_re = re.compile(r'(\d) (\d) (\d) (".*") (".*")')
|
2002-08-23 19:04:43 +00:00
|
|
|
for j in range(columns):
|
2002-08-26 18:28:13 +00:00
|
|
|
column_info.append(col_info_re.match(lines[i]).groups())
|
2002-08-23 19:04:43 +00:00
|
|
|
del lines[i]
|
|
|
|
|
|
|
|
cell_info = []
|
2002-08-26 18:28:13 +00:00
|
|
|
cell_col = []
|
2002-08-23 19:04:43 +00:00
|
|
|
ncells = 0
|
2002-08-26 18:28:13 +00:00
|
|
|
cell_re = re.compile(r'(\d) (\d) (\d) (\d) (\d) (\d) (\d) (".*") (".*")')
|
2002-08-23 19:04:43 +00:00
|
|
|
for j in range(rows):
|
|
|
|
for k in range(columns):
|
2024-06-15 09:06:06 +00:00
|
|
|
# add column location to read properties
|
2002-08-26 18:28:13 +00:00
|
|
|
cell_info.append(cell_re.match(lines[i]).groups())
|
|
|
|
cell_col.append(k)
|
2002-08-23 19:04:43 +00:00
|
|
|
if lines[i][0] != "2":
|
|
|
|
ncells = ncells + 1
|
|
|
|
del lines[i]
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
lines[tabular_line] = (
|
|
|
|
f'<LyXTabular version="1" rows="{rows-len(cont_row)}" columns="{columns}">'
|
|
|
|
)
|
2002-08-23 19:04:43 +00:00
|
|
|
del lines[i]
|
|
|
|
if not lines[i]:
|
|
|
|
del lines[i]
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2002-08-26 18:28:13 +00:00
|
|
|
# Read cells
|
|
|
|
l = 0
|
|
|
|
cell_content = []
|
|
|
|
for j in range(rows):
|
|
|
|
cell_content.append([])
|
|
|
|
|
|
|
|
for j in range(rows):
|
|
|
|
for k in range(columns):
|
|
|
|
cell_content[j].append([])
|
|
|
|
|
|
|
|
for j in range(rows):
|
|
|
|
for k in range(columns):
|
2024-06-15 09:06:06 +00:00
|
|
|
m = j * columns + k
|
|
|
|
if cell_info[m][0] == "2":
|
2002-08-26 18:28:13 +00:00
|
|
|
continue
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if l == ncells - 1:
|
2006-08-02 14:19:22 +00:00
|
|
|
# the end variable refers to cell end, not to document end.
|
2024-06-15 09:06:06 +00:00
|
|
|
end = find_tokens(
|
|
|
|
lines,
|
|
|
|
["\\layout", "\\the_end", "\\end_deeper", "\\end_float"],
|
|
|
|
i,
|
|
|
|
)
|
2002-08-26 18:28:13 +00:00
|
|
|
else:
|
2024-06-15 09:06:06 +00:00
|
|
|
end = find_token(lines, "\\newline", i)
|
2002-08-26 18:28:13 +00:00
|
|
|
|
|
|
|
if end == -1:
|
2006-08-02 14:19:22 +00:00
|
|
|
document.error("Malformed LyX file.")
|
2002-08-26 18:28:13 +00:00
|
|
|
|
|
|
|
end = end - i
|
|
|
|
while end > 0:
|
|
|
|
cell_content[j][k].append(lines[i])
|
|
|
|
del lines[i]
|
2024-06-15 09:06:06 +00:00
|
|
|
end = end - 1
|
2002-08-26 18:28:13 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if lines[i].find("\\newline") != -1:
|
2002-08-26 18:28:13 +00:00
|
|
|
del lines[i]
|
|
|
|
l = l + 1
|
|
|
|
|
2002-08-23 19:04:43 +00:00
|
|
|
tmp = []
|
|
|
|
tmp.append("")
|
|
|
|
|
|
|
|
for j in range(rows):
|
2002-08-26 18:28:13 +00:00
|
|
|
if j in cont_row:
|
|
|
|
continue
|
2024-06-15 09:06:06 +00:00
|
|
|
tmp.append(
|
|
|
|
f'<Row topline="{row_info[j][0]}" bottomline="{row_info[j][1]}" newpage="{row_info[j][3]}">'
|
|
|
|
)
|
2002-08-23 19:04:43 +00:00
|
|
|
|
|
|
|
for k in range(columns):
|
|
|
|
if j:
|
2024-06-15 09:06:06 +00:00
|
|
|
tmp.append("<Column>")
|
2002-08-23 19:04:43 +00:00
|
|
|
else:
|
2024-06-15 09:06:06 +00:00
|
|
|
tmp.append(
|
|
|
|
f'<Column alignment="{column_info[k][0]}" valignment="0" leftline="{column_info[k][1]}" rightline="{column_info[k][2]}" width={column_info[k][3]} special={column_info[k][4]}>'
|
|
|
|
)
|
|
|
|
m = j * columns + k
|
2002-08-24 12:05:17 +00:00
|
|
|
|
2002-08-25 18:45:10 +00:00
|
|
|
leftline = int(column_info[k][1])
|
2024-06-15 09:06:06 +00:00
|
|
|
if cell_info[m][0] == "1":
|
2002-08-24 12:05:17 +00:00
|
|
|
n = m + 1
|
2024-06-15 09:06:06 +00:00
|
|
|
while n < rows * columns - 1 and cell_info[n][0] == "2":
|
2002-08-24 12:05:17 +00:00
|
|
|
n = n + 1
|
2024-06-15 09:06:06 +00:00
|
|
|
rightline = int(column_info[cell_col[n - 1]][2])
|
2002-08-24 12:05:17 +00:00
|
|
|
else:
|
|
|
|
# not a multicolumn main cell
|
|
|
|
rightline = int(column_info[k][2])
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
tmp.append(
|
|
|
|
'<Cell multicolumn="%s" alignment="%s" valignment="0" topline="%s" bottomline="%s" leftline="%d" rightline="%d" rotate="%s" usebox="%s" width=%s special=%s>'
|
|
|
|
% (
|
|
|
|
cell_info[m][0],
|
|
|
|
cell_info[m][1],
|
|
|
|
cell_info[m][2],
|
|
|
|
cell_info[m][3],
|
|
|
|
leftline,
|
|
|
|
rightline,
|
|
|
|
cell_info[m][5],
|
|
|
|
cell_info[m][6],
|
|
|
|
cell_info[m][7],
|
|
|
|
cell_info[m][8],
|
|
|
|
)
|
|
|
|
)
|
|
|
|
tmp.append("\\begin_inset Text")
|
|
|
|
tmp.append("")
|
|
|
|
tmp.append("\\layout %s" % document.default_layout)
|
|
|
|
tmp.append("")
|
|
|
|
|
|
|
|
if cell_info[m][0] != "2":
|
2002-08-23 19:04:43 +00:00
|
|
|
paragraph = []
|
2024-06-15 09:06:06 +00:00
|
|
|
if cell_info[m][4] == "1":
|
2002-08-26 18:28:13 +00:00
|
|
|
l = j
|
2002-08-28 13:07:50 +00:00
|
|
|
paragraph = paragraph + cell_content[j][k]
|
2024-06-15 09:06:06 +00:00
|
|
|
while cell_info[m][4] == "1":
|
2002-08-26 18:28:13 +00:00
|
|
|
m = m + columns
|
|
|
|
l = l + 1
|
2024-06-15 09:06:06 +00:00
|
|
|
if l >= rows:
|
|
|
|
break
|
2002-08-28 13:07:50 +00:00
|
|
|
paragraph = paragraph + cell_content[l][k]
|
2002-08-26 18:28:13 +00:00
|
|
|
else:
|
|
|
|
paragraph = cell_content[j][k]
|
2002-08-28 13:07:50 +00:00
|
|
|
tmp = tmp + set_paragraph_properties(paragraph, prop_dict)
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
tmp.append("\\end_inset ")
|
|
|
|
tmp.append("</Cell>")
|
|
|
|
tmp.append("</Column>")
|
|
|
|
tmp.append("</Row>")
|
2002-08-23 19:04:43 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
tmp.append("</LyXTabular>")
|
|
|
|
tmp.append("")
|
|
|
|
tmp.append("\\end_inset ")
|
|
|
|
tmp.append("")
|
|
|
|
tmp.append("")
|
2002-08-28 13:07:50 +00:00
|
|
|
lines[i:i] = tmp
|
2002-08-23 19:04:43 +00:00
|
|
|
|
|
|
|
i = i + len(tmp)
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2002-08-23 19:04:43 +00:00
|
|
|
prop_exp = re.compile(r"\\(\S*)\s*(\S*)")
|
2024-06-15 09:06:06 +00:00
|
|
|
|
|
|
|
|
2002-08-24 12:05:17 +00:00
|
|
|
def set_paragraph_properties(lines, prop_dict):
|
2024-06-15 09:06:06 +00:00
|
|
|
"Set paragraph properties."
|
2002-08-23 19:04:43 +00:00
|
|
|
# we need to preserve the order of options
|
2024-06-15 09:06:06 +00:00
|
|
|
properties = [
|
|
|
|
"family",
|
|
|
|
"series",
|
|
|
|
"shape",
|
|
|
|
"size",
|
|
|
|
"emph",
|
|
|
|
"bar",
|
|
|
|
"noun",
|
|
|
|
"latex",
|
|
|
|
"color",
|
|
|
|
]
|
|
|
|
prop_value = {
|
|
|
|
"family": "default",
|
|
|
|
"series": "medium",
|
|
|
|
"shape": "up",
|
|
|
|
"size": "normal",
|
|
|
|
"emph": "off",
|
|
|
|
"bar": "no",
|
|
|
|
"noun": "off",
|
|
|
|
"latex": "no_latex",
|
|
|
|
"color": "none",
|
|
|
|
}
|
2002-08-23 19:04:43 +00:00
|
|
|
|
|
|
|
start = 0
|
|
|
|
end = 0
|
|
|
|
i = 0
|
|
|
|
n = len(lines)
|
2002-08-24 12:05:17 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
# skip empty lines
|
|
|
|
while i < n and lines[i] == "":
|
2002-08-23 19:04:43 +00:00
|
|
|
i = i + 1
|
|
|
|
start = i
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
# catch open char properties
|
|
|
|
while i < n and lines[i][:1] == "\\":
|
2002-08-23 19:04:43 +00:00
|
|
|
result = prop_exp.match(lines[i])
|
2002-08-24 12:05:17 +00:00
|
|
|
# sys.stderr.write(lines[i]+"\n")
|
2002-08-23 19:04:43 +00:00
|
|
|
prop = result.group(1)
|
|
|
|
if prop not in properties:
|
2002-08-24 12:05:17 +00:00
|
|
|
break
|
2002-08-23 19:04:43 +00:00
|
|
|
else:
|
2002-08-24 12:05:17 +00:00
|
|
|
prop_dict[prop] = result.group(2)
|
2002-08-23 19:04:43 +00:00
|
|
|
i = i + 1
|
|
|
|
end = i
|
|
|
|
|
|
|
|
aux = []
|
2002-08-24 12:05:17 +00:00
|
|
|
insert = 0
|
2002-08-23 19:04:43 +00:00
|
|
|
for prop in properties:
|
2024-06-15 09:06:06 +00:00
|
|
|
if prop_dict[prop] != "default":
|
2002-08-24 12:05:17 +00:00
|
|
|
insert = 1
|
|
|
|
if prop == "color":
|
2024-06-10 09:55:40 +00:00
|
|
|
aux.append(f"\\{prop} {prop_dict[prop]}")
|
2002-08-24 12:05:17 +00:00
|
|
|
elif prop != "family" or prop_dict[prop] != "roman":
|
2024-06-15 09:06:06 +00:00
|
|
|
aux.append(f"\\{prop} {prop_dict[prop]} ")
|
2002-08-24 12:05:17 +00:00
|
|
|
|
|
|
|
# remove final char properties
|
|
|
|
n = len(lines)
|
2002-08-26 19:00:56 +00:00
|
|
|
changed_prop = []
|
|
|
|
|
2002-08-24 12:05:17 +00:00
|
|
|
while n:
|
|
|
|
n = n - 1
|
|
|
|
if not lines[n]:
|
|
|
|
del lines[n]
|
|
|
|
continue
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if lines[n][:1] == "\\":
|
2002-08-24 12:05:17 +00:00
|
|
|
result = prop_exp.match(lines[n])
|
|
|
|
prop = result.group(1)
|
|
|
|
if prop in properties:
|
2002-08-26 19:00:56 +00:00
|
|
|
changed_prop.append(prop)
|
2002-08-24 12:05:17 +00:00
|
|
|
prop_dict[prop] = result.group(2)
|
|
|
|
del lines[n]
|
|
|
|
continue
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if check_token(lines[n], "\\end_inset"):
|
2002-08-24 12:05:17 +00:00
|
|
|
# ensure proper newlines after inset end
|
2024-06-15 09:06:06 +00:00
|
|
|
lines.append("")
|
|
|
|
lines.append("")
|
2002-08-24 12:05:17 +00:00
|
|
|
break
|
|
|
|
|
2002-08-26 19:00:56 +00:00
|
|
|
for line in lines[end:]:
|
2024-06-15 09:06:06 +00:00
|
|
|
if line[:1] == "\\":
|
2002-08-26 19:00:56 +00:00
|
|
|
result = prop_exp.match(line)
|
|
|
|
prop = result.group(1)
|
|
|
|
if prop in properties and prop not in changed_prop:
|
|
|
|
prop_dict[prop] = result.group(2)
|
|
|
|
|
2002-08-24 12:05:17 +00:00
|
|
|
if not lines[start:] and not lines[end:]:
|
|
|
|
return []
|
2003-10-13 09:50:10 +00:00
|
|
|
|
2002-08-24 12:05:17 +00:00
|
|
|
result = lines[:start] + aux[:] + lines[end:]
|
2024-06-15 09:06:06 +00:00
|
|
|
if insert and result[0] != "":
|
|
|
|
return [""] + result[:]
|
2002-08-24 12:05:17 +00:00
|
|
|
|
|
|
|
return result[:]
|
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2006-08-02 14:19:22 +00:00
|
|
|
def update_language(document):
|
2024-06-15 09:06:06 +00:00
|
|
|
"""Update document language, if language is default convert it to
|
2006-08-02 14:23:45 +00:00
|
|
|
english."""
|
2006-08-02 14:19:22 +00:00
|
|
|
header = document.header
|
2002-08-24 12:05:17 +00:00
|
|
|
i = find_token(header, "\\language", 0)
|
|
|
|
if i == -1:
|
|
|
|
# no language, should emit a warning
|
2024-06-15 09:06:06 +00:00
|
|
|
header.append("\\language english")
|
2002-08-24 12:05:17 +00:00
|
|
|
return
|
2002-08-26 18:28:13 +00:00
|
|
|
# This is the lyx behaviour: defaults to english
|
2024-06-15 09:06:06 +00:00
|
|
|
if header[i].split()[1] == "default":
|
|
|
|
header[i] = "\\language english"
|
2002-08-24 12:05:17 +00:00
|
|
|
return
|
2002-08-23 19:04:43 +00:00
|
|
|
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
supported_versions = ["1.1.6", "1.1.6fix1", "1.1.6fix2", "1.1"]
|
2005-01-05 18:52:59 +00:00
|
|
|
convert = [[217, [update_tabular, update_language]]]
|
2024-06-15 09:06:06 +00:00
|
|
|
revert = []
|
2004-04-14 08:45:46 +00:00
|
|
|
|
2002-08-23 19:04:43 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|