Speed up convert_specialchars. Part of #11200.

(cherry picked from commit 96ea84e042)
This commit is contained in:
Richard Kimberly Heck 2018-07-16 22:14:40 -04:00
parent f57346dd6e
commit b665b1af27

View File

@ -739,20 +739,20 @@ def convert_phrases(document):
for phrase in phrases: for phrase in phrases:
i = 0 i = 0
while i < len(document.body): while i < len(document.body):
words = document.body[i].split() if document.body[i] and document.body[i][0] == "\\":
if len(words) > 1 and words[0] == "\\begin_inset" and \ words = document.body[i].split()
words[1] in ["CommandInset", "External", "Formula", "Graphics", "listings"]: if len(words) > 1 and words[0] == "\\begin_inset" and \
# must not replace anything in insets that store LaTeX contents in .lyx files words[1] in ["CommandInset", "External", "Formula", "Graphics", "listings"]:
# (math and command insets withut overridden read() and write() methods # must not replace anything in insets that store LaTeX contents in .lyx files
j = find_end_of_inset(document.body, i) # (math and command insets without overridden read() and write() methods)
if j == -1: j = find_end_of_inset(document.body, i)
document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i)) if j == -1:
i += 1 document.warning("Malformed LyX document: Can't find end of inset at line " + str(i))
i += 1
else:
i = j
else: else:
i = j i += 1
continue
if document.body[i].find("\\") == 0:
i += 1
continue continue
j = document.body[i].find(phrase) j = document.body[i].find(phrase)
if j == -1: if j == -1:
@ -769,7 +769,7 @@ def convert_phrases(document):
def revert_phrases(document): def revert_phrases(document):
"convert special phrases to plain text" "revert special phrases to plain text"
i = 0 i = 0
while i < len(document.body): while i < len(document.body):
@ -814,16 +814,21 @@ def convert_specialchar_internal(document, forward):
i = 0 i = 0
while i < len(document.body): while i < len(document.body):
words = document.body[i].split() if document.body[i] and document.body[i][0] == "\\":
if len(words) > 1 and words[0] == "\\begin_inset" and \ words = document.body[i].split()
words[1] in ["CommandInset", "External", "Formula", "Graphics", "listings"]: if len(words) > 1 and words[0] == "\\begin_inset" and \
# see convert_phrases words[1] in ["CommandInset", "External", "Formula", "Graphics", "listings"]:
j = find_end_of_inset(document.body, i) # see convert_phrases
if j == -1: j = find_end_of_inset(document.body, i)
document.warning("Malformed LyX document: Can't find end of Formula inset at line " + str(i)) if j == -1:
i += 1 document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (words[1], i))
else: i += 1
i = j else:
i = j
continue
# else...
if not "\\SpecialChar" in document.body[i]:
i += 1
continue continue
for key, value in specialchars.items(): for key, value in specialchars.items():
if forward: if forward: