X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Flyx2lyx%2Funicode_symbols.py;h=d9eeff96835d81b0e9fe0b46a8ed63e56360add0;hb=b35fb98335b1fff52ac0a266d45e48579427dbf8;hp=7f6d4bfb385f5f2b3525e36226c1fd32e41beac0;hpb=1246a70abd5e5eec6057daa3b35f7c626270cf4d;p=lyx.git diff --git a/lib/lyx2lyx/unicode_symbols.py b/lib/lyx2lyx/unicode_symbols.py index 7f6d4bfb38..d9eeff9683 100644 --- a/lib/lyx2lyx/unicode_symbols.py +++ b/lib/lyx2lyx/unicode_symbols.py @@ -14,41 +14,58 @@ # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA " Import unicode_reps from this module for access to the unicode<->LaTeX mapping. " import sys, os, re +# Provide support for both python 2 and 3 +PY2 = sys.version_info[0] == 2 +if not PY2: + unichr = chr +# End of code to support for both python 2 and 3 + def read_unicodesymbols(): " Read the unicodesymbols list of unicode characters and corresponding commands." pathname = os.path.abspath(os.path.dirname(sys.argv[0])) - fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols')) + 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) + spec_chars = [] - # Two backslashes, followed by some non-word character, and then a character + # 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 # as: \"u or even \" u. - r = re.compile(r'\\\\(\W)\{(\w)\}') + # 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)\}') 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'\"','"') # replace \" by " (for characters with diaeresis) + 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: continue m = r.match(command) if m != None: - command = "\\\\" - # If the character is a double-quote, then we need to escape it, too, - # since it is done that way in the LyX file. - if m.group(1) == "\"": - command += "\\" + command = "\\" commandbl = command command += m.group(1) + m.group(2) commandbl += m.group(1) + ' ' + m.group(2)