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