]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/unicode_symbols.py
Minor cleanup.
[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, codecs
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(__file__))
32     filename = os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols')
33
34     # Read as Unicode strings in both, Python 2 and 3
35     # Specify the encoding for those systems where the default is not UTF-8
36     fp = codecs.open(filename, encoding="utf8")
37
38     # A backslash, followed by some non-word character, and then a character
39     # in brackets. The idea is to check for constructs like: \"{u}, which is how
40     # they are written in the unicodesymbols file; but they can also be written
41     # as: \"u or even \" u.
42     # The two backslashes in the string literal are needed to specify a literal
43     # backslash in the regex. Without r prefix, these would be four backslashes.
44     r = re.compile(r'\\(\W)\{(\w)\}')
45
46     spec_chars = []
47     for line in fp.readlines():
48         if not line.strip() or line.startswith('#'):
49             # skip empty lines and comments
50             continue
51         # Note: backslashes in the string literals with r prefix are not escaped,
52         #       so one backslash in the source file equals one backslash in memory.
53         #       Without r prefix backslahses are escaped, so two backslashes in the
54         #       source file equal one backslash in memory.
55         line=line.replace(' "',' ') # remove all quotation marks with spaces before
56         line=line.replace('" ',' ') # remove all quotation marks with spaces after
57         line=line.replace(r'\"','"') # unescape "
58         line=line.replace(r'\\','\\') # unescape \
59         try:
60             [ucs4,command,dead] = line.split(None,2)
61             if command[0:1] != "\\":
62                 continue
63             literal_char = unichr(int(ucs4, 16))
64             if (line.find("notermination=text") < 0 and
65                 line.find("notermination=both") < 0 and command[-1] != "}"):
66                 command = command + "{}"
67             spec_chars.append([command, literal_char])
68         except:
69             continue
70         m = r.match(command)
71         if m != None:
72             command = "\\"
73             commandbl = command
74             command += m.group(1) + m.group(2)
75             commandbl += m.group(1) + ' ' + m.group(2)
76             spec_chars.append([command, literal_char])
77             spec_chars.append([commandbl, literal_char])
78     fp.close()
79     return spec_chars
80
81
82 unicode_reps = read_unicodesymbols()
83