1 # This file is part of lyx2lyx
2 # Copyright (C) 2024 The LyX team
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 """Convert files to the file format generated by lyx 2.5"""
22 # Uncomment only what you need to import, please.
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,
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,
31 # find_complete_lines,
32 # find_re, find_substring,
34 # find_tokens, check_token
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,
40 # get_ert, lyx2verbatim, length_in_bp, convert_info_insets
41 # revert_flex_inset, hex2ratio
43 ####################################################################
44 # Private helper functions
47 ###############################################################################
49 ### Conversion and reversion routines
51 ###############################################################################
54 def convert_url_escapes(document):
55 """Unescape # and % in URLs with hyperref."""
57 hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
58 beamer = document.textclass in [
65 if not hyperref and not beamer:
68 rurl = re.compile(r"^[%#].*")
71 i = find_token(document.body, "\\begin_inset Flex URL", i)
74 j = find_end_of_inset(document.body, i)
76 document.warning("Malformed LyX document: Could not find end of URL inset.")
80 surl = find_re(document.body, rurl, i, j)
84 if document.body[surl - 1] == "\\backslash":
85 del document.body[surl - 1]
89 def revert_url_escapes(document):
90 """Unescape # and % in URLs with hyperref."""
92 hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
93 beamer = document.textclass in [
100 if not hyperref and not beamer:
103 rurl = re.compile(r"^(.*)([%#].*)")
106 i = find_token(document.body, "\\begin_inset Flex URL", i)
109 j = find_end_of_inset(document.body, i)
111 document.warning("Malformed LyX document: Could not find end of URL inset.")
115 surl = find_re(document.body, rurl, i, j)
119 m = rurl.match(document.body[surl])
121 if m.group(1) == "" and document.body[surl - 1] == "\\backslash":
123 document.body[surl : surl + 1] = [m.group(1), "\\backslash", m.group(2)]
127 def convert_url_escapes2(document):
128 """Unescape backslashes in URLs with hyperref."""
130 i = find_token(document.header, "\\use_hyperref true", 0)
132 if i == -1 and document.textclass not in [
142 i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
145 j = find_end_of_inset(document.body, i)
147 document.warning("Malformed LyX document: Could not find end of URL inset.")
151 bs = find_token(document.body, "\\backslash", i, j)
154 if document.body[bs + 2] == "\\backslash":
155 del document.body[bs + 2]
159 def revert_url_escapes2(document):
160 """Escape backslashes in URLs with hyperref."""
162 i = find_token(document.header, "\\use_hyperref true", 0)
164 if i == -1 and document.textclass not in [
174 i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
177 j = find_end_of_inset(document.body, i)
179 document.warning("Malformed LyX document: Could not find end of URL inset.")
183 bs = find_token(document.body, "\\backslash", i, j)
186 document.body[bs] = "\\backslash\\backslash"
190 def revert_glue_parskip(document):
191 """Revert parskip with glue length to user preamble."""
193 i = find_token(document.header, "\\paragraph_separation skip", 0)
197 j = find_token(document.header, "\\defskip", 0)
199 document.warning("Malformed LyX document! Missing \\defskip.")
202 val = get_value(document.header, "\\defskip", j)
204 if val.find("+") == -1 and val.find("-", 1) == -1:
208 add_to_preamble(document, ["\\usepackage[skip={" + latex_length(val)[1] + "}]{parskip}"])
210 document.header[i] = "\\paragraph_separation indent"
211 document.header[j] = "\\paragraph_indentation default"
214 def convert_he_letter(document):
215 """Convert hebrew letter to letter document class"""
217 if document.textclass == "heb-letter":
218 document.textclass = "letter"
225 supported_versions = ["2.5.0", "2.5"]
227 [621, [convert_url_escapes, convert_url_escapes2]],
229 [623, [convert_he_letter]],
235 [621, [revert_glue_parskip]],
236 [620, [revert_url_escapes2, revert_url_escapes]],
240 if __name__ == "__main__":