2010-11-04 19:07:30 +00:00
|
|
|
# This file is part of lyx2lyx
|
2011-01-21 13:24:23 +00:00
|
|
|
# Copyright (C) 2011 The LyX team
|
2010-11-04 19:07:30 +00:00
|
|
|
#
|
|
|
|
# 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
|
2011-08-25 23:10:36 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2010-11-04 19:07:30 +00:00
|
|
|
|
2024-06-15 09:06:06 +00:00
|
|
|
"Import unicode_reps from this module for access to the unicode<->LaTeX mapping."
|
2010-11-04 19:07:30 +00:00
|
|
|
|
2024-06-18 09:25:52 +00:00
|
|
|
import codecs
|
2024-06-15 10:26:28 +00:00
|
|
|
import os
|
|
|
|
import re
|
2010-11-04 19:07:30 +00:00
|
|
|
|
2015-03-11 12:04:46 +00:00
|
|
|
|
2010-11-04 19:07:30 +00:00
|
|
|
def read_unicodesymbols():
|
2024-06-15 09:06:06 +00:00
|
|
|
"Read the unicodesymbols list of unicode characters and corresponding commands."
|
2018-01-31 14:09:32 +00:00
|
|
|
pathname = os.path.abspath(os.path.dirname(__file__))
|
2024-06-15 09:06:06 +00:00
|
|
|
filename = os.path.join(pathname.strip("lyx2lyx"), "unicodesymbols")
|
2017-03-27 10:35:29 +00:00
|
|
|
|
2018-01-31 14:09:32 +00:00
|
|
|
# Read as Unicode strings in both, Python 2 and 3
|
|
|
|
# Specify the encoding for those systems where the default is not UTF-8
|
|
|
|
fp = codecs.open(filename, encoding="utf8")
|
2017-03-27 10:35:29 +00:00
|
|
|
|
2015-06-14 10:44:44 +00:00
|
|
|
# A backslash, followed by some non-word character, and then a character
|
2010-11-04 19:07:30 +00:00
|
|
|
# in brackets. The idea is to check for constructs like: \"{u}, which is how
|
|
|
|
# they are written in the unicodesymbols file; but they can also be written
|
|
|
|
# as: \"u or even \" u.
|
2015-06-14 10:44:44 +00:00
|
|
|
# The two backslashes in the string literal are needed to specify a literal
|
|
|
|
# backslash in the regex. Without r prefix, these would be four backslashes.
|
2024-06-15 09:06:06 +00:00
|
|
|
r = re.compile(r"\\(\W)\{(\w)\}")
|
2018-01-31 14:09:32 +00:00
|
|
|
|
|
|
|
spec_chars = []
|
2010-11-04 19:07:30 +00:00
|
|
|
for line in fp.readlines():
|
2024-06-15 09:06:06 +00:00
|
|
|
if not line.strip() or line.startswith("#"):
|
2018-01-31 14:09:32 +00:00
|
|
|
# skip empty lines and comments
|
|
|
|
continue
|
|
|
|
# Note: backslashes in the string literals with r prefix are not escaped,
|
|
|
|
# so one backslash in the source file equals one backslash in memory.
|
|
|
|
# Without r prefix backslahses are escaped, so two backslashes in the
|
|
|
|
# source file equal one backslash in memory.
|
2024-06-15 09:06:06 +00:00
|
|
|
line = line.replace(' "', " ") # remove all quotation marks with spaces before
|
|
|
|
line = line.replace('" ', " ") # remove all quotation marks with spaces after
|
|
|
|
line = line.replace(r"\"", '"') # unescape "
|
|
|
|
line = line.replace(r"\\", "\\") # unescape \
|
2018-01-31 14:09:32 +00:00
|
|
|
try:
|
2024-06-15 09:06:06 +00:00
|
|
|
[ucs4, command, dead] = line.split(None, 2)
|
2018-01-31 14:09:32 +00:00
|
|
|
if command[0:1] != "\\":
|
2010-11-04 19:07:30 +00:00
|
|
|
continue
|
2024-06-10 09:55:40 +00:00
|
|
|
literal_char = chr(int(ucs4, 16))
|
2024-06-15 09:06:06 +00:00
|
|
|
if (
|
|
|
|
line.find("notermination=text") < 0
|
|
|
|
and line.find("notermination=both") < 0
|
|
|
|
and command[-1] != "}"
|
|
|
|
):
|
2018-01-31 14:09:32 +00:00
|
|
|
command = command + "{}"
|
|
|
|
spec_chars.append([command, literal_char])
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
m = r.match(command)
|
|
|
|
if m != None:
|
|
|
|
command = "\\"
|
|
|
|
commandbl = command
|
|
|
|
command += m.group(1) + m.group(2)
|
2024-06-15 09:06:06 +00:00
|
|
|
commandbl += m.group(1) + " " + m.group(2)
|
2018-01-31 14:09:32 +00:00
|
|
|
spec_chars.append([command, literal_char])
|
|
|
|
spec_chars.append([commandbl, literal_char])
|
2010-11-04 19:07:30 +00:00
|
|
|
fp.close()
|
|
|
|
return spec_chars
|
|
|
|
|
|
|
|
|
|
|
|
unicode_reps = read_unicodesymbols()
|