lyx_mirror/lib/lyx2lyx/lyx_2_5.py
Juergen Spitzmueller ed62816eb6 Handle URL escaping in beamer frames
File format change -- this introduces the first LyX 2.5 format and
hence sets up the lyx2lyx machinery
2024-05-13 07:26:41 +02:00

137 lines
4.5 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)
# 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_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 (put_cmd_in_ert, add_to_preamble, insert_to_preamble, lyx2latex,
# revert_language, revert_flex_inset, str2bool)
# revert_font_attrs, latex_length
# 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 in frames."""
if document.textclass not in ['beamer', 'scrarticle-beamer', 'beamerposter', 'article-beamer']:
return
rurl = re.compile(r'^[%#].*')
i = 0
while True:
i = find_token(document.body, "\\begin_layout Frame", i + 1)
if i == -1:
return
j = find_end_of_layout(document.body, i)
if j == -1:
document.warning("Malformed LyX document: Could not find end of Frame layout.")
continue
k = find_token(document.body, "\\begin_inset Flex URL", i, j)
if k == -1:
continue
l = find_end_of_inset(document.body, k)
if l == -1:
document.warning("Malformed LyX document: Could not find end of URL inset.")
continue
while True:
surl = find_re(document.body, rurl, k, l)
if surl == -1:
break
if document.body[surl - 1] == "\\backslash":
del document.body[surl - 1]
k = surl
def revert_url_escapes(document):
"""Unescape # and % in URLs in frames."""
if document.textclass not in ['beamer', 'scrarticle-beamer', 'beamerposter', 'article-beamer']:
return
rurl = re.compile(r'^(.*)([%#].*)')
i = 0
while True:
i = find_token(document.body, "\\begin_layout Frame", i + 1)
if i == -1:
return
j = find_end_of_layout(document.body, i)
if j == -1:
document.warning("Malformed LyX document: Could not find end of Frame layout.")
continue
k = find_token(document.body, "\\begin_inset Flex URL", i, j)
if k == -1:
continue
l = find_end_of_inset(document.body, k)
if l == -1:
document.warning("Malformed LyX document: Could not find end of URL inset.")
continue
while True:
surl = find_re(document.body, rurl, k, l)
if surl == -1:
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)]
k = surl
##
# Conversion hub
#
supported_versions = ["2.5.0", "2.5"]
convert = [
[621, [convert_url_escapes]]
]
revert = [[620, [revert_url_escapes]]
]
if __name__ == "__main__":
pass