mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-23 05:25:26 +00:00
Use the unicodesymbols file for the latex2lyx conversion.
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@25495 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
parent
59553ea2c7
commit
bdb72502aa
@ -114,32 +114,56 @@ def set_option(document, m, option, value):
|
|||||||
return l
|
return l
|
||||||
|
|
||||||
|
|
||||||
# To convert and revert indices, we need to convert between LaTeX
|
def read_unicodesymbols():
|
||||||
# strings and LyXText. Here we do a minimal conversion to prevent
|
" Read the unicodesymbols list of unicode characters and corresponding commands."
|
||||||
# crashes and data loss. Manual patch-up may be needed.
|
pathname = os.path.abspath(os.path.dirname(sys.argv[0]))
|
||||||
replacements = [
|
fp = open(os.path.join(pathname.strip('lyx2lyx'), 'unicodesymbols'))
|
||||||
[r'\\\"a', u'ä'],
|
spec_chars = []
|
||||||
[r'\\\"o', u'ö'],
|
# Two backslashes, followed by some non-word character, and then a character
|
||||||
[r'\\\"u', u'ü'],
|
# in brackets. The idea is to check for constructs like: \"{u}, which is how
|
||||||
[r'\\\'a', u'á'],
|
# they are written in the unicodesymbols file; but they can also be written
|
||||||
[r'\\\'e', u'é'],
|
# as: \"u.
|
||||||
[r'\\\'i', u'í'],
|
r = re.compile(r'\\\\(\W)\{(\w)\}')
|
||||||
[r'\\\'o', u'ó'],
|
for line in fp.readlines():
|
||||||
[r'\\\'u', u'ú']
|
if line[0] != '#' and line.strip() != "":
|
||||||
]
|
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):
|
def latex2lyx(data):
|
||||||
'''Takes a string, possibly multi-line, and returns the result of
|
'''Takes a string, possibly multi-line, and returns the result of
|
||||||
converting LaTeX constructs into LyX constructs. Returns a list of
|
converting LaTeX constructs into LyX constructs. Returns a list of
|
||||||
lines, suitable for insertion into document.body.'''
|
lines, suitable for insertion into document.body.'''
|
||||||
# Do the LaTeX --> LyX text conversion
|
|
||||||
r2 = re.compile('^(.*?)(\$.*?\$)(.*)')
|
mathre = re.compile('^(.*?)(\$.*?\$)(.*)')
|
||||||
retval = []
|
retval = []
|
||||||
|
|
||||||
# Convert LaTeX to Unicode
|
# Convert LaTeX to Unicode
|
||||||
# FIXME This should probably use the list we have elsewhere
|
reps = read_unicodesymbols()
|
||||||
for rep in replacements:
|
for rep in reps:
|
||||||
data = data.replace(rep[0], rep[1])
|
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, \" -> ":
|
# Generic, \" -> ":
|
||||||
data = wrap_into_ert(data, r'\"', '"')
|
data = wrap_into_ert(data, r'\"', '"')
|
||||||
# Math:
|
# Math:
|
||||||
@ -149,8 +173,8 @@ def latex2lyx(data):
|
|||||||
#document.warning(str(i) + ":" + document.body[i])
|
#document.warning(str(i) + ":" + document.body[i])
|
||||||
#document.warning("LAST: " + document.body[-1])
|
#document.warning("LAST: " + document.body[-1])
|
||||||
g = line
|
g = line
|
||||||
while r2.match(g):
|
while mathre.match(g):
|
||||||
m = r2.match(g)
|
m = mathre.match(g)
|
||||||
s = m.group(1)
|
s = m.group(1)
|
||||||
f = m.group(2).replace('\\\\', '\\')
|
f = m.group(2).replace('\\\\', '\\')
|
||||||
g = m.group(3)
|
g = m.group(3)
|
||||||
|
Loading…
Reference in New Issue
Block a user