]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/unicode_symbols.py
Length.cpp: add new unit representing \baselineskip
[lyx.git] / lib / lyx2lyx / unicode_symbols.py
1 # This file is part of lyx2lyx
2 # -*- coding: utf-8 -*-
3 # Copyright (C) 2011 The LyX team
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18
19 " Import unicode_reps from this module for access to the unicode<->LaTeX mapping. "
20
21 import sys, os, re
22
23 # Provide support for both python 2 and 3
24 PY2 = sys.version_info[0] == 2
25 if not PY2:
26     unichr = chr
27 # End of code to support for both python 2 and 3
28
29 def read_unicodesymbols():
30     " Read the unicodesymbols list of unicode characters and corresponding commands."
31     pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
32     filename = os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols')
33
34     # For python 3+ we have to specify the encoding for those systems
35     # where the default is not UTF-8
36     fp = open(filename, encoding="utf8") if (not PY2) else open(filename)
37
38     spec_chars = []
39     # A backslash, followed by some non-word character, and then a character
40     # in brackets. The idea is to check for constructs like: \"{u}, which is how
41     # they are written in the unicodesymbols file; but they can also be written
42     # as: \"u or even \" u.
43     # The two backslashes in the string literal are needed to specify a literal
44     # backslash in the regex. Without r prefix, these would be four backslashes.
45     r = re.compile(r'\\(\W)\{(\w)\}')
46     for line in fp.readlines():
47         if line[0] != '#' and line.strip() != "":
48             # Note: backslashes in the string literals with r prefix are not escaped,
49             #       so one backslash in the source file equals one backslash in memory.
50             #       Without r prefix backslahses are escaped, so two backslashes in the
51             #       source file equal one backslash in memory.
52             line=line.replace(' "',' ') # remove all quotation marks with spaces before
53             line=line.replace('" ',' ') # remove all quotation marks with spaces after
54             line=line.replace(r'\"','"') # unescape "
55             line=line.replace(r'\\','\\') # unescape \
56             try:
57                 [ucs4,command,dead] = line.split(None,2)
58                 if command[0:1] != "\\":
59                     continue
60                 if (line.find("notermination=text") < 0 and
61                     line.find("notermination=both") < 0 and command[-1] != "}"):
62                     command = command + "{}"
63                 spec_chars.append([command, unichr(eval(ucs4))])
64             except:
65                 continue
66             m = r.match(command)
67             if m != None:
68                 command = "\\"
69                 commandbl = command
70                 command += m.group(1) + m.group(2)
71                 commandbl += m.group(1) + ' ' + m.group(2)
72                 spec_chars.append([command, unichr(eval(ucs4))])
73                 spec_chars.append([commandbl, unichr(eval(ucs4))])
74     fp.close()
75     return spec_chars
76
77
78 unicode_reps = read_unicodesymbols()