mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-11-28 12:26:59 +00:00
214 lines
6.7 KiB
Python
214 lines
6.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
# 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.
|
|
|
|
""" Convert files to the file format generated by lyx 2.5"""
|
|
|
|
import re, string
|
|
import unicodedata
|
|
import sys, os
|
|
|
|
from datetime import (datetime, date, time)
|
|
|
|
# Uncomment only what you need to import, please.
|
|
|
|
from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, find_re, get_value)
|
|
# count_pars_in_inset, del_complete_lines, del_token, find_end_of,
|
|
# find_token_backwards, find_token_exact, get_bool_value,
|
|
# get_containing_inset, get_containing_layout, get_option_value,
|
|
# get_quoted_value, is_in_inset,
|
|
# del_value,
|
|
# find_complete_lines,
|
|
# find_re, find_substring,
|
|
# set_bool_value
|
|
# find_tokens, check_token
|
|
|
|
from lyx2lyx_tools import (add_to_preamble, latex_length)
|
|
# put_cmd_in_ert, insert_to_preamble, lyx2latex,
|
|
# revert_language, revert_flex_inset, str2bool,
|
|
# revert_font_attrs,
|
|
# get_ert, lyx2verbatim, length_in_bp, convert_info_insets
|
|
# revert_flex_inset, hex2ratio
|
|
|
|
####################################################################
|
|
# Private helper functions
|
|
|
|
|
|
|
|
###############################################################################
|
|
###
|
|
### Conversion and reversion routines
|
|
###
|
|
###############################################################################
|
|
|
|
def convert_url_escapes(document):
|
|
"""Unescape # and % in URLs with hyperref."""
|
|
|
|
hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
|
|
beamer = document.textclass in ['beamer', 'scrarticle-beamer', 'beamerposter', 'article-beamer']
|
|
|
|
if not hyperref and not beamer:
|
|
return
|
|
|
|
rurl = re.compile(r'^[%#].*')
|
|
i = 0
|
|
while True:
|
|
i = find_token(document.body, "\\begin_inset Flex URL", i)
|
|
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.")
|
|
i += 1
|
|
continue
|
|
while True:
|
|
surl = find_re(document.body, rurl, i, j)
|
|
if surl == -1:
|
|
i = j
|
|
break
|
|
if document.body[surl - 1] == "\\backslash":
|
|
del document.body[surl - 1]
|
|
i = surl
|
|
|
|
|
|
def revert_url_escapes(document):
|
|
"""Unescape # and % in URLs with hyperref."""
|
|
|
|
hyperref = find_token(document.header, "\\use_hyperref true", 0) != -1
|
|
beamer = document.textclass in ['beamer', 'scrarticle-beamer', 'beamerposter', 'article-beamer']
|
|
|
|
if not hyperref and not beamer:
|
|
return
|
|
|
|
rurl = re.compile(r'^(.*)([%#].*)')
|
|
i = 0
|
|
while True:
|
|
i = find_token(document.body, "\\begin_inset Flex URL", i)
|
|
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.")
|
|
i += 1
|
|
continue
|
|
while True:
|
|
surl = find_re(document.body, rurl, i, j)
|
|
if surl == -1:
|
|
i = j
|
|
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)]
|
|
i = surl
|
|
|
|
def convert_url_escapes2(document):
|
|
"""Unescape backslashes in URLs with hyperref."""
|
|
|
|
i = find_token(document.header, "\\use_hyperref true", 0)
|
|
|
|
if i == -1 and document.textclass not in ['beamer', 'scrarticle-beamer', 'beamerposter', 'article-beamer']:
|
|
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.")
|
|
i += 1
|
|
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
|
|
|
|
def revert_url_escapes2(document):
|
|
"""Escape backslashes in URLs with hyperref."""
|
|
|
|
i = find_token(document.header, "\\use_hyperref true", 0)
|
|
|
|
if i == -1 and document.textclass not in ['beamer', 'scrarticle-beamer', 'beamerposter', 'article-beamer']:
|
|
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.")
|
|
i += 1
|
|
continue
|
|
while True:
|
|
bs = find_token(document.body, "\\backslash", i, j)
|
|
if bs == -1:
|
|
break
|
|
document.body[bs] = "\\backslash\\backslash"
|
|
i = bs + 1
|
|
|
|
|
|
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"
|
|
|
|
|
|
##
|
|
# Conversion hub
|
|
#
|
|
|
|
supported_versions = ["2.5.0", "2.5"]
|
|
convert = [
|
|
[621, [convert_url_escapes, convert_url_escapes2]],
|
|
[622, []]
|
|
]
|
|
|
|
|
|
revert = [[621, [revert_glue_parskip]],
|
|
[620, [revert_url_escapes2, revert_url_escapes]]
|
|
]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
pass
|