1 # This file is part of lyx2lyx
2 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 """Convert files to the file format generated by lyx 1.1.6, fix3 and fix4"""
22 from parser_tools import find_re, find_token
26 "Convert 0, 1 to false, true."
29 # should emit a warning if item != "1"
33 align_vertical = {"0": "top", "1": "bottom", "2": "center"}
34 align_table = {"0": "top", "2": "left", "4": "right", "8": "center"}
35 use_table = {"0": "none", "1": "parbox"}
36 table_meta_re = re.compile(r'<LyXTabular version="?1"? rows="?(\d*)"? columns="?(\d*)"?>')
39 def update_tabular(document):
40 "Update tabular format to version 2 (xml like syntax)."
41 regexp = re.compile(r"^\\begin_inset\s+Tabular")
45 i = find_re(lines, regexp, i)
51 # scan table header meta-info
52 res = table_meta_re.match(lines[i])
55 lines[i] = '<lyxtabular version="2" rows="%s" columns="%s">' % val
57 j = find_token(lines, "</LyXTabular>", i) + 1
59 document.warning("Error: Bad lyx format i=%d j=%d" % (i, j))
62 new_table = table_update(lines[i:j])
63 lines[i:j] = new_table
64 i = i + len(new_table)
68 r'<column alignment="?(\d)"? valignment="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? width="(.*)" special="(.*)">'
71 r'<cell multicolumn="?(\d)"? alignment="?(\d)"? valignment="?(\d)"? topline="?(\d)"? bottomline="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? rotate="?(\d)"? usebox="?(\d)"? width="(.*)" special="(.*)">'
73 features_re = re.compile(
74 r'<features rotate="?(\d)"? islongtable="?(\d)"? endhead="?(-?\d)"? endfirsthead="?(-?\d)"? endfoot="?(-?\d)"? endlastfoot="?(-?\d)"?>'
76 row_re = re.compile(r'<row topline="?(\d)"? bottomline="?(\d)"? newpage="?(\d)"?>')
79 def table_update(lines):
80 "Update table's internal content to format 2."
81 lines[1] = lines[1].replace("<Features", "<features")
82 res = features_re.match(lines[1])
86 f'<features rotate="{bool_table(val[0])}" islongtable="{bool_table(val[1])}" endhead="{val[2]}" endfirsthead="{val[3]}" endfoot="{val[4]}" endlastfoot="{val[5]}">'
94 lines[i] = lines[i].replace("<Cell", "<cell")
95 lines[i] = lines[i].replace("</Cell", "</cell")
96 lines[i] = lines[i].replace("<Row", "<row")
97 lines[i] = lines[i].replace("</Row", "</row")
98 lines[i] = lines[i].replace("<Column", "<column")
99 lines[i] = lines[i].replace("</Column", "</column")
100 lines[i] = lines[i].replace("</LyXTabular", "</lyxtabular")
101 k = lines[i].find("<column ")
103 col_info.append(lines[i])
107 if lines[i] == "</column>" or lines[i] == "<column>":
111 res = cell_re.match(lines[i])
115 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]}">'
118 res = row_re.match(lines[i])
122 f'<row topline="{bool_table(val[0])}" bottomline="{bool_table(val[1])}" newpage="{bool_table(val[2])}">'
129 res = col_re.match(col_info[i])
133 '<column alignment="%s" valignment="%s" leftline="%s" rightline="%s" width="%s" special="%s">'
136 align_vertical[val[1]],
144 return lines[:2] + col_info + lines[2:]
147 supported_versions = ["1.1.6fix3", "1.1.6fix4", "1.1"]
148 convert = [[218, [update_tabular]]]
152 if __name__ == "__main__":