]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_1_3.py
Reformat lyx2lyx code using ruff
[lyx.git] / lib / lyx2lyx / lyx_1_3.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2002 Dekel Tsur <dekel@lyx.org>
3 # Copyright (C) 2004 José Matos <jamatos@lyx.org>
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
19 """Convert files to the file format generated by lyx 1.3"""
20
21 import re
22 from parser_tools import find_token, find_end_of, get_value, find_token_exact
23
24 ####################################################################
25 # Private helper functions
26
27
28 def find_end_of_inset(lines, i):
29     r"Finds the matching \end_inset"
30     return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
31
32
33 def del_token(lines, token, start, end):
34     """del_token(lines, token, start, end) -> int
35
36     Find the lower line in lines where token is the first element and
37     delete that line.
38
39     Returns the number of lines remaining."""
40
41     k = find_token_exact(lines, token, start, end)
42     if k == -1:
43         return end
44     else:
45         del lines[k]
46         return end - 1
47
48
49 # End of helper functions
50 ####################################################################
51
52
53 def change_insetgraphics(document):
54     "Change inset Graphics."
55     lines = document.body
56     i = 0
57     while True:
58         i = find_token(lines, "\\begin_inset Graphics", i)
59         if i == -1:
60             break
61         j = find_end_of_inset(lines, i)
62
63         lines[i] = "\\begin_inset Graphics"
64
65         if get_value(lines, "display", i, j) == "default":
66             j = del_token(lines, "display", i, j)
67         if get_value(lines, "rotateOrigin", i, j) == "leftBaseline":
68             j = del_token(lines, "rotateOrigin", i, j)
69
70         k = find_token_exact(lines, "rotate", i, j)
71         if k != -1:
72             del lines[k]
73             j = j - 1
74         else:
75             j = del_token(lines, "rotateAngle", i, j)
76
77         k = find_token_exact(lines, "size_type", i, j)
78         if k == -1:
79             k = find_token_exact(lines, "size_kind", i, j)
80         if k != -1:
81             size_type = lines[k].split()[1]
82             del lines[k]
83             j = j - 1
84             if size_type in ["0", "original"]:
85                 j = del_token(lines, "width", i, j)
86                 j = del_token(lines, "height", i, j)
87                 j = del_token(lines, "scale", i, j)
88             elif size_type in ["2", "scale"]:
89                 j = del_token(lines, "width", i, j)
90                 j = del_token(lines, "height", i, j)
91                 if get_value(lines, "scale", i, j) == "100":
92                     j = del_token(lines, "scale", i, j)
93             else:
94                 j = del_token(lines, "scale", i, j)
95
96         k = find_token_exact(lines, "lyxsize_type", i, j)
97         if k == -1:
98             k = find_token_exact(lines, "lyxsize_kind", i, j)
99         if k != -1:
100             lyxsize_type = lines[k].split()[1]
101             del lines[k]
102             j = j - 1
103             j = del_token(lines, "lyxwidth", i, j)
104             j = del_token(lines, "lyxheight", i, j)
105             if (
106                 lyxsize_type not in ["2", "scale"]
107                 or get_value(lines, "lyxscale", i, j) == "100"
108             ):
109                 j = del_token(lines, "lyxscale", i, j)
110
111         i = i + 1
112
113
114 def change_tabular(document):
115     "Change tabular."
116     lines = document.body
117     i = 0
118     while True:
119         i = find_token(lines, "<column", i)
120         if i == -1:
121             break
122         if not re.search('width="0pt"', lines[i]):
123             lines[i] = re.sub(' alignment=".*?"', ' alignment="block"', lines[i])
124         i = i + 1
125
126
127 supported_versions = ["1.3.%d" % i for i in range(8)] + ["1.3"]
128 convert = [[221, [change_insetgraphics, change_tabular]]]
129 revert = []
130
131
132 if __name__ == "__main__":
133     pass