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