]> git.lyx.org Git - lyx.git/blob - lib/lyx2lyx/unicode_symbols.py
EmbeddedObjects.lyx: more updates for the box description
[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     # Two backslashes, 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     r = re.compile(r'\\\\(\W)\{(\w)\}')
39     for line in fp.readlines():
40         if line[0] != '#' and line.strip() != "":
41             line=line.replace(' "',' ') # remove all quotation marks with spaces before
42             line=line.replace('" ',' ') # remove all quotation marks with spaces after
43             line=line.replace(r'\"','"') # replace \" by " (for characters with diaeresis)
44             try:
45                 [ucs4,command,dead] = line.split(None,2)
46                 if command[0:1] != "\\":
47                     continue
48                 spec_chars.append([command, unichr(eval(ucs4))])
49             except:
50                 continue
51             m = r.match(command)
52             if m != None:
53                 command = "\\\\"
54                 # If the character is a double-quote, then we need to escape it, too,
55                 # since it is done that way in the LyX file.
56                 if m.group(1) == "\"":
57                     command += "\\"
58                 commandbl = command
59                 command += m.group(1) + m.group(2)
60                 commandbl += m.group(1) + ' ' + m.group(2)
61                 spec_chars.append([command, unichr(eval(ucs4))])
62                 spec_chars.append([commandbl, unichr(eval(ucs4))])
63     fp.close()
64     return spec_chars
65
66
67 unicode_reps = read_unicodesymbols()