2024-05-13 05:26:41 +00:00
|
|
|
# This file is part of lyx2lyx
|
|
|
|
# Copyright (C) 2024 The LyX team
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
"""Convert files to the file format generated by lyx 2.5"""
|
2024-05-13 05:26:41 +00:00
|
|
|
|
2024-06-10 11:05:03 +00:00
|
|
|
import re
|
2024-05-13 05:26:41 +00:00
|
|
|
|
|
|
|
# Uncomment only what you need to import, please.
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
from parser_tools import find_end_of_inset, find_token, find_re, get_value
|
2024-05-13 05:26:41 +00:00
|
|
|
# count_pars_in_inset, del_complete_lines, del_token, find_end_of,
|
2024-06-10 11:05:03 +00:00
|
|
|
# find_end_of_layout,
|
2024-05-13 05:26:41 +00:00
|
|
|
# find_token_backwards, find_token_exact, get_bool_value,
|
2024-06-01 11:47:39 +00:00
|
|
|
# get_containing_inset, get_containing_layout, get_option_value,
|
2024-05-13 05:26:41 +00:00
|
|
|
# get_quoted_value, is_in_inset,
|
2024-06-10 09:55:40 +00:00
|
|
|
# del_value,
|
2024-05-13 05:26:41 +00:00
|
|
|
# find_complete_lines,
|
|
|
|
# find_re, find_substring,
|
|
|
|
# set_bool_value
|
|
|
|
# find_tokens, check_token
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
from lyx2lyx_tools import add_to_preamble, latex_length
|
2024-06-01 11:47:39 +00:00
|
|
|
# put_cmd_in_ert, insert_to_preamble, lyx2latex,
|
|
|
|
# revert_language, revert_flex_inset, str2bool,
|
|
|
|
# revert_font_attrs,
|
2024-05-13 05:26:41 +00:00
|
|
|
# get_ert, lyx2verbatim, length_in_bp, convert_info_insets
|
|
|
|
# revert_flex_inset, hex2ratio
|
|
|
|
|
|
|
|
####################################################################
|
|
|
|
# Private helper functions
|
|
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
###
|
|
|
|
### Conversion and reversion routines
|
|
|
|
###
|
|
|
|
###############################################################################
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2024-05-13 05:26:41 +00:00
|
|
|
def convert_url_escapes(document):
|
2024-05-15 11:15:08 +00:00
|
|
|
"""Unescape # and % in URLs with hyperref."""
|
|
|
|
|
|
|
|
hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
|
2024-06-15 09:06:06 +00:00
|
|
|
beamer = document.textclass in [
|
|
|
|
"beamer",
|
|
|
|
"scrarticle-beamer",
|
|
|
|
"beamerposter",
|
|
|
|
"article-beamer",
|
|
|
|
]
|
2024-05-15 11:15:08 +00:00
|
|
|
|
|
|
|
if not hyperref and not beamer:
|
2024-05-13 05:26:41 +00:00
|
|
|
return
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
rurl = re.compile(r"^[%#].*")
|
2024-05-13 05:26:41 +00:00
|
|
|
i = 0
|
|
|
|
while True:
|
2024-05-15 11:15:08 +00:00
|
|
|
i = find_token(document.body, "\\begin_inset Flex URL", i)
|
2024-05-13 05:26:41 +00:00
|
|
|
if i == -1:
|
|
|
|
return
|
2024-05-15 11:15:08 +00:00
|
|
|
j = find_end_of_inset(document.body, i)
|
2024-05-13 05:26:41 +00:00
|
|
|
if j == -1:
|
|
|
|
document.warning("Malformed LyX document: Could not find end of URL inset.")
|
2024-05-15 11:15:08 +00:00
|
|
|
i += 1
|
2024-05-13 05:26:41 +00:00
|
|
|
continue
|
|
|
|
while True:
|
2024-05-15 11:15:08 +00:00
|
|
|
surl = find_re(document.body, rurl, i, j)
|
2024-05-13 05:26:41 +00:00
|
|
|
if surl == -1:
|
2024-05-15 11:15:08 +00:00
|
|
|
i = j
|
2024-05-13 05:26:41 +00:00
|
|
|
break
|
|
|
|
if document.body[surl - 1] == "\\backslash":
|
|
|
|
del document.body[surl - 1]
|
2024-05-15 11:15:08 +00:00
|
|
|
i = surl
|
2024-06-10 09:55:40 +00:00
|
|
|
|
2024-05-13 05:26:41 +00:00
|
|
|
|
|
|
|
def revert_url_escapes(document):
|
2024-05-15 11:15:08 +00:00
|
|
|
"""Unescape # and % in URLs with hyperref."""
|
|
|
|
|
|
|
|
hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
|
2024-06-15 09:06:06 +00:00
|
|
|
beamer = document.textclass in [
|
|
|
|
"beamer",
|
|
|
|
"scrarticle-beamer",
|
|
|
|
"beamerposter",
|
|
|
|
"article-beamer",
|
|
|
|
]
|
2024-05-15 11:15:08 +00:00
|
|
|
|
|
|
|
if not hyperref and not beamer:
|
2024-05-13 05:26:41 +00:00
|
|
|
return
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
rurl = re.compile(r"^(.*)([%#].*)")
|
2024-05-13 05:26:41 +00:00
|
|
|
i = 0
|
|
|
|
while True:
|
2024-05-15 11:15:08 +00:00
|
|
|
i = find_token(document.body, "\\begin_inset Flex URL", i)
|
2024-05-13 05:26:41 +00:00
|
|
|
if i == -1:
|
2024-05-15 16:36:47 +00:00
|
|
|
return
|
2024-05-15 11:15:08 +00:00
|
|
|
j = find_end_of_inset(document.body, i)
|
|
|
|
if j == -1:
|
2024-05-13 05:26:41 +00:00
|
|
|
document.warning("Malformed LyX document: Could not find end of URL inset.")
|
2024-05-15 11:15:08 +00:00
|
|
|
i += 1
|
2024-05-13 05:26:41 +00:00
|
|
|
continue
|
|
|
|
while True:
|
2024-05-15 11:15:08 +00:00
|
|
|
surl = find_re(document.body, rurl, i, j)
|
2024-05-13 05:26:41 +00:00
|
|
|
if surl == -1:
|
2024-05-15 11:15:08 +00:00
|
|
|
i = j
|
2024-05-13 05:26:41 +00:00
|
|
|
break
|
|
|
|
m = rurl.match(document.body[surl])
|
|
|
|
if m:
|
|
|
|
if m.group(1) == "" and document.body[surl - 1] == "\\backslash":
|
|
|
|
break
|
|
|
|
document.body[surl : surl + 1] = [m.group(1), "\\backslash", m.group(2)]
|
2024-05-15 11:15:08 +00:00
|
|
|
i = surl
|
2024-05-13 05:26:41 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2024-05-13 07:17:30 +00:00
|
|
|
def convert_url_escapes2(document):
|
2024-05-13 08:26:45 +00:00
|
|
|
"""Unescape backslashes in URLs with hyperref."""
|
2024-05-13 07:17:30 +00:00
|
|
|
|
|
|
|
i = find_token(document.header, "\\use_hyperref true", 0)
|
2024-06-10 09:55:40 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if i == -1 and document.textclass not in [
|
|
|
|
"beamer",
|
|
|
|
"scrarticle-beamer",
|
|
|
|
"beamerposter",
|
|
|
|
"article-beamer",
|
|
|
|
]:
|
2024-05-13 07:17:30 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
|
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
j = find_end_of_inset(document.body, i)
|
|
|
|
if j == -1:
|
|
|
|
document.warning("Malformed LyX document: Could not find end of URL inset.")
|
2024-05-15 11:15:08 +00:00
|
|
|
i += 1
|
2024-05-13 07:17:30 +00:00
|
|
|
continue
|
|
|
|
while True:
|
|
|
|
bs = find_token(document.body, "\\backslash", i, j)
|
|
|
|
if bs == -1:
|
|
|
|
break
|
|
|
|
if document.body[bs + 2] == "\\backslash":
|
|
|
|
del document.body[bs + 2]
|
|
|
|
i = bs + 1
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2024-05-13 07:17:30 +00:00
|
|
|
def revert_url_escapes2(document):
|
2024-05-13 08:26:45 +00:00
|
|
|
"""Escape backslashes in URLs with hyperref."""
|
2024-05-13 07:17:30 +00:00
|
|
|
|
|
|
|
i = find_token(document.header, "\\use_hyperref true", 0)
|
2024-06-10 09:55:40 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
if i == -1 and document.textclass not in [
|
|
|
|
"beamer",
|
|
|
|
"scrarticle-beamer",
|
|
|
|
"beamerposter",
|
|
|
|
"article-beamer",
|
|
|
|
]:
|
2024-05-13 07:17:30 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
i = find_token(document.body, "\\begin_inset Flex URL", i + 1)
|
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
j = find_end_of_inset(document.body, i)
|
|
|
|
if j == -1:
|
|
|
|
document.warning("Malformed LyX document: Could not find end of URL inset.")
|
2024-05-15 11:15:08 +00:00
|
|
|
i += 1
|
2024-05-13 07:17:30 +00:00
|
|
|
continue
|
|
|
|
while True:
|
|
|
|
bs = find_token(document.body, "\\backslash", i, j)
|
|
|
|
if bs == -1:
|
|
|
|
break
|
|
|
|
document.body[bs] = "\\backslash\\backslash"
|
|
|
|
i = bs + 1
|
|
|
|
|
2024-06-01 11:47:39 +00:00
|
|
|
|
|
|
|
def revert_glue_parskip(document):
|
|
|
|
"""Revert parskip with glue length to user preamble."""
|
|
|
|
|
|
|
|
i = find_token(document.header, "\\paragraph_separation skip", 0)
|
|
|
|
if i == -1:
|
|
|
|
return
|
|
|
|
|
|
|
|
j = find_token(document.header, "\\defskip", 0)
|
|
|
|
if j == -1:
|
|
|
|
document.warning("Malformed LyX document! Missing \\defskip.")
|
|
|
|
return
|
|
|
|
|
|
|
|
val = get_value(document.header, "\\defskip", j)
|
|
|
|
|
|
|
|
if val.find("+") == -1 and val.find("-", 1) == -1:
|
|
|
|
# not a glue length
|
|
|
|
return
|
|
|
|
|
|
|
|
add_to_preamble(document, ["\\usepackage[skip={" + latex_length(val)[1] + "}]{parskip}"])
|
|
|
|
|
|
|
|
document.header[i] = "\\paragraph_separation indent"
|
|
|
|
document.header[j] = "\\paragraph_indentation default"
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
|
2024-06-11 18:14:27 +00:00
|
|
|
def convert_he_letter(document):
|
|
|
|
"""Convert hebrew letter to letter document class"""
|
|
|
|
|
|
|
|
if document.textclass == "heb-letter":
|
|
|
|
document.textclass = "letter"
|
|
|
|
|
2024-06-01 11:47:39 +00:00
|
|
|
|
2024-05-13 05:26:41 +00:00
|
|
|
##
|
|
|
|
# Conversion hub
|
|
|
|
#
|
|
|
|
|
|
|
|
supported_versions = ["2.5.0", "2.5"]
|
|
|
|
convert = [
|
2024-06-15 09:06:06 +00:00
|
|
|
[621, [convert_url_escapes, convert_url_escapes2]],
|
|
|
|
[622, []],
|
|
|
|
[623, [convert_he_letter]],
|
|
|
|
]
|
2024-05-13 05:26:41 +00:00
|
|
|
|
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
revert = [
|
|
|
|
[622, []],
|
|
|
|
[621, [revert_glue_parskip]],
|
|
|
|
[620, [revert_url_escapes2, revert_url_escapes]],
|
|
|
|
]
|
2024-05-13 05:26:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
pass
|