]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/lyx_2_5.py
Reformat lyx2lyx code using ruff
[lyx.git] / lib / lyx2lyx / lyx_2_5.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2024 The LyX team
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17
18 """Convert files to the file format generated by lyx 2.5"""
19
20 import re
21
22 # Uncomment only what you need to import, please.
23
24 from parser_tools import find_end_of_inset, find_token, find_re, get_value
25 #    count_pars_in_inset, del_complete_lines, del_token, find_end_of,
26 #    find_end_of_layout,
27 #    find_token_backwards, find_token_exact, get_bool_value,
28 #    get_containing_inset, get_containing_layout, get_option_value,
29 #    get_quoted_value, is_in_inset,
30 #    del_value,
31 #    find_complete_lines,
32 #    find_re, find_substring,
33 #    set_bool_value
34 #    find_tokens, check_token
35
36 from lyx2lyx_tools import add_to_preamble, latex_length
37 #  put_cmd_in_ert, insert_to_preamble, lyx2latex,
38 #  revert_language, revert_flex_inset, str2bool,
39 #  revert_font_attrs,
40 #  get_ert, lyx2verbatim, length_in_bp, convert_info_insets
41 #  revert_flex_inset, hex2ratio
42
43 ####################################################################
44 # Private helper functions
45
46
47 ###############################################################################
48 ###
49 ### Conversion and reversion routines
50 ###
51 ###############################################################################
52
53
54 def convert_url_escapes(document):
55     """Unescape # and % in URLs with hyperref."""
56
57     hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
58     beamer = document.textclass in [
59         "beamer",
60         "scrarticle-beamer",
61         "beamerposter",
62         "article-beamer",
63     ]
64
65     if not hyperref and not beamer:
66         return
67
68     rurl = re.compile(r"^[%#].*")
69     i = 0
70     while True:
71         i = find_token(document.body, "\\begin_inset Flex URL", i)
72         if i == -1:
73             return
74         j = find_end_of_inset(document.body, i)
75         if j == -1:
76             document.warning("Malformed LyX document: Could not find end of URL inset.")
77             i += 1
78             continue
79         while True:
80             surl = find_re(document.body, rurl, i, j)
81             if surl == -1:
82                 i = j
83                 break
84             if document.body[surl - 1] == "\\backslash":
85                 del document.body[surl - 1]
86             i = surl
87
88
89 def revert_url_escapes(document):
90     """Unescape # and % in URLs with hyperref."""
91
92     hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
93     beamer = document.textclass in [
94         "beamer",
95         "scrarticle-beamer",
96         "beamerposter",
97         "article-beamer",
98     ]
99
100     if not hyperref and not beamer:
101         return
102
103     rurl = re.compile(r"^(.*)([%#].*)")
104     i = 0
105     while True:
106         i = find_token(document.body, "\\begin_inset Flex URL", i)
107         if i == -1:
108             return
109         j = find_end_of_inset(document.body, i)
110         if j == -1:
111             document.warning("Malformed LyX document: Could not find end of URL inset.")
112             i += 1
113             continue
114         while True:
115             surl = find_re(document.body, rurl, i, j)
116             if surl == -1:
117                 i = j
118                 break
119             m = rurl.match(document.body[surl])
120             if m:
121                 if m.group(1) == "" and document.body[surl - 1] == "\\backslash":
122                     break
123                 document.body[surl : surl + 1] = [m.group(1), "\\backslash", m.group(2)]
124             i = surl
125
126
127 def convert_url_escapes2(document):
128     """Unescape backslashes in URLs with hyperref."""
129
130     i = find_token(document.header, "\\use_hyperref true", 0)
131
132     if i == -1 and document.textclass not in [
133         "beamer",
134         "scrarticle-beamer",
135         "beamerposter",
136         "article-beamer",
137     ]:
138         return
139
140     i = 0
141     while True:
142         i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
143         if i == -1:
144             return
145         j = find_end_of_inset(document.body, i)
146         if j == -1:
147             document.warning("Malformed LyX document: Could not find end of URL inset.")
148             i += 1
149             continue
150         while True:
151             bs = find_token(document.body, "\\backslash", i, j)
152             if bs == -1:
153                 break
154             if document.body[bs + 2] == "\\backslash":
155                 del document.body[bs + 2]
156             i = bs + 1
157
158
159 def revert_url_escapes2(document):
160     """Escape backslashes in URLs with hyperref."""
161
162     i = find_token(document.header, "\\use_hyperref true", 0)
163
164     if i == -1 and document.textclass not in [
165         "beamer",
166         "scrarticle-beamer",
167         "beamerposter",
168         "article-beamer",
169     ]:
170         return
171
172     i = 0
173     while True:
174         i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
175         if i == -1:
176             return
177         j = find_end_of_inset(document.body, i)
178         if j == -1:
179             document.warning("Malformed LyX document: Could not find end of URL inset.")
180             i += 1
181             continue
182         while True:
183             bs = find_token(document.body, "\\backslash", i, j)
184             if bs == -1:
185                 break
186             document.body[bs] = "\\backslash\\backslash"
187             i = bs + 1
188
189
190 def revert_glue_parskip(document):
191     """Revert parskip with glue length to user preamble."""
192
193     i = find_token(document.header, "\\paragraph_separation skip", 0)
194     if i == -1:
195         return
196
197     j = find_token(document.header, "\\defskip", 0)
198     if j == -1:
199         document.warning("Malformed LyX document! Missing \\defskip.")
200         return
201
202     val = get_value(document.header, "\\defskip", j)
203
204     if val.find("+") == -1 and val.find("-", 1) == -1:
205         # not a glue length
206         return
207
208     add_to_preamble(document, ["\\usepackage[skip={" + latex_length(val)[1] + "}]{parskip}"])
209
210     document.header[i] = "\\paragraph_separation indent"
211     document.header[j] = "\\paragraph_indentation default"
212
213
214 def convert_he_letter(document):
215     """Convert hebrew letter to letter document class"""
216
217     if document.textclass == "heb-letter":
218         document.textclass = "letter"
219
220
221 ##
222 # Conversion hub
223 #
224
225 supported_versions = ["2.5.0", "2.5"]
226 convert = [
227     [621, [convert_url_escapes, convert_url_escapes2]],
228     [622, []],
229     [623, [convert_he_letter]],
230 ]
231
232
233 revert = [
234     [622, []],
235     [621, [revert_glue_parskip]],
236     [620, [revert_url_escapes2, revert_url_escapes]],
237 ]
238
239
240 if __name__ == "__main__":
241     pass