X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Flyx2lyx%2Funicode_symbols.py;h=0c5032acebeb80d0e981cf9cbc3da119a5fe8a61;hb=c24233e3f16e73fbab534deaad51e7a4ff11d8e5;hp=d9eeff96835d81b0e9fe0b46a8ed63e56360add0;hpb=bcf715f398ead2e2c43af65bedcfecad27e48880;p=features.git diff --git a/lib/lyx2lyx/unicode_symbols.py b/lib/lyx2lyx/unicode_symbols.py index d9eeff9683..0c5032aceb 100644 --- a/lib/lyx2lyx/unicode_symbols.py +++ b/lib/lyx2lyx/unicode_symbols.py @@ -18,7 +18,7 @@ " Import unicode_reps from this module for access to the unicode<->LaTeX mapping. " -import sys, os, re +import sys, os, re, codecs # Provide support for both python 2 and 3 PY2 = sys.version_info[0] == 2 @@ -28,14 +28,13 @@ if not PY2: def read_unicodesymbols(): " Read the unicodesymbols list of unicode characters and corresponding commands." - pathname = os.path.abspath(os.path.dirname(sys.argv[0])) + pathname = os.path.abspath(os.path.dirname(__file__)) filename = os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols') - # For python 3+ we have to specify the encoding for those systems - # where the default is not UTF-8 - fp = open(filename, encoding="utf8") if (not PY2) else open(filename) + # Read as Unicode strings in both, Python 2 and 3 + # Specify the encoding for those systems where the default is not UTF-8 + fp = codecs.open(filename, encoding="utf8") - spec_chars = [] # A backslash, followed by some non-word character, and then a character # in brackets. The idea is to check for constructs like: \"{u}, which is how # they are written in the unicodesymbols file; but they can also be written @@ -43,36 +42,42 @@ def read_unicodesymbols(): # The two backslashes in the string literal are needed to specify a literal # backslash in the regex. Without r prefix, these would be four backslashes. r = re.compile(r'\\(\W)\{(\w)\}') + + spec_chars = [] for line in fp.readlines(): - if line[0] != '#' and line.strip() != "": - # Note: backslashes in the string literals with r prefix are not escaped, - # so one backslash in the source file equals one backslash in memory. - # Without r prefix backslahses are escaped, so two backslashes in the - # source file equal one backslash in memory. - line=line.replace(' "',' ') # remove all quotation marks with spaces before - line=line.replace('" ',' ') # remove all quotation marks with spaces after - line=line.replace(r'\"','"') # unescape " - line=line.replace(r'\\','\\') # unescape \ - try: - [ucs4,command,dead] = line.split(None,2) - if command[0:1] != "\\": - continue - if (line.find("notermination=text") < 0 and - line.find("notermination=both") < 0 and command[-1] != "}"): - command = command + "{}" - spec_chars.append([command, unichr(eval(ucs4))]) - except: + if not line.strip() or line.startswith('#'): + # skip empty lines and comments + continue + # Note: backslashes in the string literals with r prefix are not escaped, + # so one backslash in the source file equals one backslash in memory. + # Without r prefix backslahses are escaped, so two backslashes in the + # source file equal one backslash in memory. + line=line.replace(' "',' ') # remove all quotation marks with spaces before + line=line.replace('" ',' ') # remove all quotation marks with spaces after + line=line.replace(r'\"','"') # unescape " + line=line.replace(r'\\','\\') # unescape \ + try: + [ucs4,command,dead] = line.split(None,2) + if command[0:1] != "\\": continue - m = r.match(command) - if m != None: - command = "\\" - commandbl = command - command += m.group(1) + m.group(2) - commandbl += m.group(1) + ' ' + m.group(2) - spec_chars.append([command, unichr(eval(ucs4))]) - spec_chars.append([commandbl, unichr(eval(ucs4))]) + literal_char = unichr(int(ucs4, 16)) + if (line.find("notermination=text") < 0 and + line.find("notermination=both") < 0 and command[-1] != "}"): + command = command + "{}" + spec_chars.append([command, literal_char]) + except: + continue + m = r.match(command) + if m != None: + command = "\\" + commandbl = command + command += m.group(1) + m.group(2) + commandbl += m.group(1) + ' ' + m.group(2) + spec_chars.append([command, literal_char]) + spec_chars.append([commandbl, literal_char]) fp.close() return spec_chars unicode_reps = read_unicodesymbols() +