1 # This file is part of lyx2lyx
2 # Copyright (C) 2011 The LyX team
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.
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.
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
18 "Import unicode_reps from this module for access to the unicode<->LaTeX mapping."
20 import sys, os, re, codecs
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")
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")
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)\}")
41 for line in fp.readlines():
42 if not line.strip() or line.startswith("#"):
43 # skip empty lines and comments
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 \
54 [ucs4, command, dead] = line.split(None, 2)
55 if command[0:1] != "\\":
57 literal_char = chr(int(ucs4, 16))
59 line.find("notermination=text") < 0
60 and line.find("notermination=both") < 0
61 and command[-1] != "}"
63 command = command + "{}"
64 spec_chars.append([command, literal_char])
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])
79 unicode_reps = read_unicodesymbols()