]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_1_6_3.py
Move DrawStrategy enum to update_flags.h.
[lyx.git] / lib / lyx2lyx / lyx_1_1_6_3.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002-2004 José Matos <jamatos@lyx.org>
3 #
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.
8 #
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.
13 #
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
17
18 """Convert files to the file format generated by lyx 1.1.6, fix3 and fix4"""
19
20 import re
21
22 from parser_tools import find_re, find_token
23
24
25 def bool_table(item):
26     "Convert 0, 1 to false, true."
27     if item == "0":
28         return "false"
29     # should emit a warning if item != "1"
30     return "true"
31
32
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*)"?>')
37
38
39 def update_tabular(document):
40     "Update tabular format to version 2 (xml like syntax)."
41     regexp = re.compile(r"^\\begin_inset\s+Tabular")
42     lines = document.body
43     i = 0
44     while True:
45         i = find_re(lines, regexp, i)
46         if i == -1:
47             break
48
49         i = i + 1
50
51         # scan table header meta-info
52         res = table_meta_re.match(lines[i])
53         if res:
54             val = res.groups()
55             lines[i] = '<lyxtabular version="2" rows="%s" columns="%s">' % val
56
57         j = find_token(lines, "</LyXTabular>", i) + 1
58         if j == 0:
59             document.warning("Error: Bad lyx format i=%d j=%d" % (i, j))
60             break
61
62         new_table = table_update(lines[i:j])
63         lines[i:j] = new_table
64         i = i + len(new_table)
65
66
67 col_re = re.compile(
68     r'<column alignment="?(\d)"? valignment="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? width="(.*)" special="(.*)">'
69 )
70 cell_re = re.compile(
71     r'<cell multicolumn="?(\d)"? alignment="?(\d)"? valignment="?(\d)"? topline="?(\d)"? bottomline="?(\d)"? leftline="?(\d)"? rightline="?(\d)"? rotate="?(\d)"? usebox="?(\d)"? width="(.*)" special="(.*)">'
72 )
73 features_re = re.compile(
74     r'<features rotate="?(\d)"? islongtable="?(\d)"? endhead="?(-?\d)"? endfirsthead="?(-?\d)"? endfoot="?(-?\d)"? endlastfoot="?(-?\d)"?>'
75 )
76 row_re = re.compile(r'<row topline="?(\d)"? bottomline="?(\d)"? newpage="?(\d)"?>')
77
78
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])
83     if res:
84         val = res.groups()
85         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]}">'
87         )
88
89     if lines[2] == "":
90         del lines[2]
91     i = 2
92     col_info = []
93     while i < len(lines):
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 ")
102         if k != -1:
103             col_info.append(lines[i])
104             del lines[i]
105             continue
106
107         if lines[i] == "</column>" or lines[i] == "<column>":
108             del lines[i]
109             continue
110
111         res = cell_re.match(lines[i])
112         if res:
113             val = res.groups()
114             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]}">'
116             )
117
118         res = row_re.match(lines[i])
119         if res:
120             val = res.groups()
121             lines[i] = (
122                 f'<row topline="{bool_table(val[0])}" bottomline="{bool_table(val[1])}" newpage="{bool_table(val[2])}">'
123             )
124
125         i = i + 1
126
127     j = len(col_info)
128     for i in range(j):
129         res = col_re.match(col_info[i])
130         if res:
131             val = res.groups()
132             col_info[i] = (
133                 '<column alignment="%s" valignment="%s" leftline="%s" rightline="%s" width="%s" special="%s">'
134                 % (
135                     align_table[val[0]],
136                     align_vertical[val[1]],
137                     bool_table(val[2]),
138                     bool_table(val[3]),
139                     val[4],
140                     val[5],
141                 )
142             )
143
144     return lines[:2] + col_info + lines[2:]
145
146
147 supported_versions = ["1.1.6fix3", "1.1.6fix4", "1.1"]
148 convert = [[218, [update_tabular]]]
149 revert = []
150
151
152 if __name__ == "__main__":
153     pass