]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/unicode_symbols.py
Reformat lyx2lyx code using ruff
[lyx.git] / lib / lyx2lyx / unicode_symbols.py
1 # This file is part of lyx2lyx
2 # Copyright (C) 2011 The LyX team
3 #
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.
8 #
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.
13 #
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17
18 "Import unicode_reps from this module for access to the unicode<->LaTeX mapping."
19
20 import sys, os, re, codecs
21
22
23 def read_unicodesymbols():
24     "Read the unicodesymbols list of unicode characters and corresponding commands."
25     pathname = os.path.abspath(os.path.dirname(__file__))
26     filename = os.path.join(pathname.strip("lyx2lyx"), "unicodesymbols")
27
28     # Read as Unicode strings in both, Python 2 and 3
29     # Specify the encoding for those systems where the default is not UTF-8
30     fp = codecs.open(filename, encoding="utf8")
31
32     # A backslash, followed by some non-word character, and then a character
33     # in brackets. The idea is to check for constructs like: \"{u}, which is how
34     # they are written in the unicodesymbols file; but they can also be written
35     # as: \"u or even \" u.
36     # The two backslashes in the string literal are needed to specify a literal
37     # backslash in the regex. Without r prefix, these would be four backslashes.
38     r = re.compile(r"\\(\W)\{(\w)\}")
39
40     spec_chars = []
41     for line in fp.readlines():
42         if not line.strip() or line.startswith("#"):
43             # skip empty lines and comments
44             continue
45         # Note: backslashes in the string literals with r prefix are not escaped,
46         #       so one backslash in the source file equals one backslash in memory.
47         #       Without r prefix backslahses are escaped, so two backslashes in the
48         #       source file equal one backslash in memory.
49         line = line.replace(' "', " ")  # remove all quotation marks with spaces before
50         line = line.replace('" ', " ")  # remove all quotation marks with spaces after
51         line = line.replace(r"\"", '"')  # unescape "
52         line = line.replace(r"\\", "\\")  # unescape \
53         try:
54             [ucs4, command, dead] = line.split(None, 2)
55             if command[0:1] != "\\":
56                 continue
57             literal_char = chr(int(ucs4, 16))
58             if (
59                 line.find("notermination=text") < 0
60                 and line.find("notermination=both") < 0
61                 and command[-1] != "}"
62             ):
63                 command = command + "{}"
64             spec_chars.append([command, literal_char])
65         except:
66             continue
67         m = r.match(command)
68         if m != None:
69             command = "\\"
70             commandbl = command
71             command += m.group(1) + m.group(2)
72             commandbl += m.group(1) + " " + m.group(2)
73             spec_chars.append([command, literal_char])
74             spec_chars.append([commandbl, literal_char])
75     fp.close()
76     return spec_chars
77
78
79 unicode_reps = read_unicodesymbols()