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 (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
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
38 ####################################################################
39 # Private helper functions
42 ###############################################################################
44 ### Conversion and reversion routines
46 ###############################################################################
49 def convert_url_escapes(document):
50 """Unescape # and % in URLs with hyperref."""
52 hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
53 beamer = document.textclass in [
60 if not hyperref and not beamer:
63 rurl = re.compile(r"^[%#].*")
66 i = find_token(document.body, "\\begin_inset Flex URL", i)
69 j = find_end_of_inset(document.body, i)
71 document.warning("Malformed LyX document: Could not find end of URL inset.")
75 surl = find_re(document.body, rurl, i, j)
79 if document.body[surl - 1] == "\\backslash":
80 del document.body[surl - 1]
84 def revert_url_escapes(document):
85 """Unescape # and % in URLs with hyperref."""
87 hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
88 beamer = document.textclass in [
95 if not hyperref and not beamer:
98 rurl = re.compile(r"^(.*)([%#].*)")
101 i = find_token(document.body, "\\begin_inset Flex URL", i)
104 j = find_end_of_inset(document.body, i)
106 document.warning("Malformed LyX document: Could not find end of URL inset.")
110 surl = find_re(document.body, rurl, i, j)
114 m = rurl.match(document.body[surl])
116 if m.group(1) == "" and document.body[surl - 1] == "\\backslash":
118 document.body[surl : surl + 1] = [m.group(1), "\\backslash", m.group(2)]
122 def convert_url_escapes2(document):
123 """Unescape backslashes in URLs with hyperref."""
125 i = find_token(document.header, "\\use_hyperref true", 0)
127 if i == -1 and document.textclass not in [
137 i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
140 j = find_end_of_inset(document.body, i)
142 document.warning("Malformed LyX document: Could not find end of URL inset.")
146 bs = find_token(document.body, "\\backslash", i, j)
149 if document.body[bs + 2] == "\\backslash":
150 del document.body[bs + 2]
154 def revert_url_escapes2(document):
155 """Escape backslashes in URLs with hyperref."""
157 i = find_token(document.header, "\\use_hyperref true", 0)
159 if i == -1 and document.textclass not in [
169 i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
172 j = find_end_of_inset(document.body, i)
174 document.warning("Malformed LyX document: Could not find end of URL inset.")
178 bs = find_token(document.body, "\\backslash", i, j)
181 document.body[bs] = "\\backslash\\backslash"
185 def revert_glue_parskip(document):
186 """Revert parskip with glue length to user preamble."""
188 i = find_token(document.header, "\\paragraph_separation skip", 0)
192 j = find_token(document.header, "\\defskip", 0)
194 document.warning("Malformed LyX document! Missing \\defskip.")
197 val = get_value(document.header, "\\defskip", j)
199 if val.find("+") == -1 and val.find("-", 1) == -1:
203 add_to_preamble(document, ["\\usepackage[skip={" + latex_length(val)[1] + "}]{parskip}"])
205 document.header[i] = "\\paragraph_separation indent"
206 document.header[j] = "\\paragraph_indentation default"
209 def convert_he_letter(document):
210 """Convert hebrew letter to letter document class"""
212 if document.textclass == "heb-letter":
213 document.textclass = "letter"
220 supported_versions = ["2.5.0", "2.5"]
222 [621, [convert_url_escapes, convert_url_escapes2]],
224 [623, [convert_he_letter]],
230 [621, [revert_glue_parskip]],
231 [620, [revert_url_escapes2, revert_url_escapes]],
235 if __name__ == "__main__":