]> git.lyx.org Git - features.git/commitdiff
Speed up convert_specialchars. Part of #11200.
authorRichard Kimberly Heck <rikiheck@lyx.org>
Tue, 17 Jul 2018 02:14:40 +0000 (22:14 -0400)
committerRichard Kimberly Heck <rikiheck@lyx.org>
Tue, 11 Dec 2018 23:02:48 +0000 (18:02 -0500)
(cherry picked from commit 96ea84e04202325b2407c5eb6923aad49535b522)

lib/lyx2lyx/lyx_2_2.py

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