+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'))
+ spec_chars = []
+ # Two backslashes, 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.
+ r = re.compile(r'\\\\(\W)\{(\w)\}')
+ for line in fp.readlines():
+ if line[0] != '#' and line.strip() != "" and line.find("\\") != -1:
+ 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)
+ try:
+ [ucs4,command,dead] = line.split(None,2)
+ 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 += m.group(1) + m.group(2)
+ spec_chars.append([command, unichr(eval(ucs4))])
+ fp.close()
+ return spec_chars
+
+
+def latex2lyx(data):
+ '''Takes a string, possibly multi-line, and returns the result of
+ converting LaTeX constructs into LyX constructs. Returns a list of
+ lines, suitable for insertion into document.body.'''
+
+ mathre = re.compile('^(.*?)(\$.*?\$)(.*)')
+ retval = []
+
+ # Convert LaTeX to Unicode
+ reps = read_unicodesymbols()
+ for rep in reps:
+ try:
+ data = data.replace(rep[0], rep[1])
+ except:
+ # There seems to be a character in the unicodesymbols file
+ # that causes problems, namely, 0x2109.
+ pass
+ # Generic, \" -> ":
+ data = wrap_into_ert(data, r'\"', '"')
+ # Math:
+ lines = data.split('\n')
+ for line in lines:
+ #document.warning("LINE: " + line)
+ #document.warning(str(i) + ":" + document.body[i])
+ #document.warning("LAST: " + document.body[-1])
+ g = line
+ m = mathre.match(g)
+ if m == None:
+ g = wrap_into_ert(g, '\\', '\\backslash')
+ g = wrap_into_ert(g, '{', '{')
+ g = wrap_into_ert(g, '}', '}')
+ subst = g.split('\n')
+ retval += subst
+ continue
+ while m != None:
+ s = m.group(1)
+ f = m.group(2).replace('\\\\', '\\')
+ g = m.group(3)
+ if s:
+ # this is non-math!
+ s = wrap_into_ert(s, '\\', '\\backslash')
+ s = wrap_into_ert(s, '{', '{')
+ s = wrap_into_ert(s, '}', '}')
+ subst = s.split('\n')
+ retval += subst
+ retval.append("\\begin_inset Formula " + f)
+ retval.append("\\end_inset")
+ m = mathre.match(g)
+ # Generic, \\ -> \backslash:
+ g = wrap_into_ert(g, r'\\', '\\backslash')
+ g = wrap_into_ert(g, '{', '{')
+ g = wrap_into_ert(g, '}', '}')
+ subst = g.split('\n')
+ retval += subst
+ return retval
+
+