On 05/15/2014 05:19 AM, Jürgen Spitzmüller wrote:
2014-05-14 21:18 GMT+02:00 Richard Heck:

    This:


         Todo: this routine can currently handle only one mandatory
    argument of envi
         '''
    +    end_layout = find_end_of_layout(document.body, line)
         lineERT = line
         endn = line
         loop = 1
    -    while lineERT != -1 and n < nmax + 1:
    -      lineERT = find_token(document.body, "\\begin_inset ERT",
    lineERT)
    -      if environment == False and lineERT != -1:
    +    while n < nmax + 1:
    +      lineERT = find_token(document.body, "\\begin_inset ERT",
    lineERT, end_lay
    +      if lineERT == -1:
    +        break

    will solve some of that problem.


Good. Note that there is a second search for ERT inset in that method (check for "lineERT2"), which probably needs similar handling.

    But I see what you mean about all the hard-coding. I may just try
    to re-write the method. But without any comments it is hard to
    know what is supposed to be happening.


That would be great. I think I understand a bit of it, so please ask if things are unclear.

For the time being, the fix above should go in, I think.

The attached puts limits on all the various searches. It also re-organizes some of the code, as we don't need to hardcode quite so many things. Can someone have a quick look before I commit.

Richard

>From a8efb2959ed9ec4ed8456f5b30f4599c23579648 Mon Sep 17 00:00:00 2001
From: Richard Heck <rgh...@lyx.org>
Date: Thu, 15 May 2014 09:35:05 -0400
Subject: [PATCH] Add limits to searches in convert_TeX_brace_to_Argument.

---
 lib/lyx2lyx/lyx_2_1.py | 82 ++++++++++++++++++++++++++++----------------------
 1 file changed, 46 insertions(+), 36 deletions(-)

diff --git a/lib/lyx2lyx/lyx_2_1.py b/lib/lyx2lyx/lyx_2_1.py
index 20d87ba..dbe15fe 100644
--- a/lib/lyx2lyx/lyx_2_1.py
+++ b/lib/lyx2lyx/lyx_2_1.py
@@ -120,21 +120,28 @@ def convert_TeX_brace_to_Argument(document, line, n, nmax, inset, environment, o
     
     Todo: this routine can currently handle only one mandatory argument of environments
     '''
+
+    end_layout = find_end_of_layout(document.body, line)
     lineERT = line
     endn = line
     loop = 1
-    while lineERT != -1 and n < nmax + 1:
-      lineERT = find_token(document.body, "\\begin_inset ERT", lineERT)
-      if environment == False and lineERT != -1:
-        bracePair = -1
+    while n < nmax + 1:
+      lineERT = find_token(document.body, "\\begin_inset ERT", lineERT, end_layout)
+      if lineERT == -1:
+        break
+      if environment == False:
+        end_ERT = find_end_of_inset(document.body, lineERT)
+        if end_ERT == -1:
+          document.warning("Can't find end of ERT!!")
+          break
+        # Note that this only checks for ][ or }{ at the beginning of a line
         if opt:
-          bracePair = find_token(document.body, "][", lineERT)
+          bracePair = find_token(document.body, "][", lineERT, end_ERT)
         else:
-          bracePair = find_token(document.body, "}{", lineERT)
-        # assure that the "}{" is in this ERT
-        if bracePair == lineERT + 5:
+          bracePair = find_token(document.body, "}{", lineERT, end_ERT)
+        if bracePair != -1:
           end = find_token(document.body, "\\end_inset", bracePair)
-          document.body[lineERT : end + 1] = ["\\end_layout", "", "\\end_inset"]
+          document.body[lineERT : end_ERT + 1] = ["\\end_layout", "", "\\end_inset"]
           if loop == 1:
             # in the case that n > 1 we have optional arguments before
             # therefore detect them if any
@@ -156,24 +163,22 @@ def convert_TeX_brace_to_Argument(document, line, n, nmax, inset, environment, o
               document.body[line + 1 : line + 1] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
             else:
               document.body[line + 4 : line + 4] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
-          else:
+          else: # if loop != 1
             document.body[endn : endn] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
           n += 1
           endn = end
-          loop = loop + 1
+          loop += 1
         # now check the case that we have "}" + "{" in two ERTs
-        else:
-          endBrace = -1
+        else: # no brace pair found
           if opt:
-            endBrace = find_token(document.body, "]", lineERT)
+            endBrace = find_token(document.body, "]", lineERT, end_layout)
           else:
-            endBrace = find_token(document.body, "}", lineERT)
+            endBrace = find_token(document.body, "}", lineERT, end_layout)
           if endBrace == lineERT + 5:
-            beginBrace = -1
             if opt:
-              beginBrace = find_token(document.body, "[", endBrace)
+              beginBrace = find_token(document.body, "[", endBrace, end_layout)
             else:
-              beginBrace = find_token(document.body, "{", endBrace)
+              beginBrace = find_token(document.body, "{", endBrace, end_layout)
             # assure that the ERTs are consecutive (11 or 12 depending if there is a space between the ERTs or not)
             if beginBrace == endBrace + 11 or beginBrace == endBrace + 12:
               end = find_token(document.body, "\\end_inset", beginBrace)
@@ -212,27 +217,32 @@ def convert_TeX_brace_to_Argument(document, line, n, nmax, inset, environment, o
               lineERT += 1
           else:
             lineERT += 1
-      if environment == True and lineERT != -1:
-        opening = -1
+      if environment == True:
+        end_ERT = find_end_of_inset(document.body, lineERT)
+        if end_ERT == -1:
+          document.warning("Can't find end of ERT!!")
+          break
+        # Note that this only checks for [ or { at the beginning of a line
         if opt:
-          opening = find_token(document.body, "[", lineERT)
+          opening = find_token(document.body, "[", lineERT, end_ERT)
         else:
-          opening = find_token(document.body, "{", lineERT)
-        if opening == lineERT + 5: # assure that the "{" is in this ERT
-          end = find_token(document.body, "\\end_inset", opening)
-          document.body[lineERT : end + 1] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
+          opening = find_token(document.body, "{", lineERT, end_ERT)
+        if opening != -1:
+          document.body[lineERT : end_ERT + 1] = ["\\begin_inset Argument " + str(n), "status open", "", "\\begin_layout Plain Layout"]
           n += 1
-          lineERT2 = find_token(document.body, "\\begin_inset ERT", lineERT)
-          closing = -1
-          if opt:
-            closing = find_token(document.body, "]", lineERT)
-          else:
-            closing = find_token(document.body, "}", lineERT2)
-          if closing == lineERT2 + 5: # assure that the "}" is in this ERT
-            end2 = find_token(document.body, "\\end_inset", closing)
-            document.body[lineERT2 : end2 + 1] = ["\\end_layout", "", "\\end_inset"]
-        else:
-          lineERT += 1
+          lineERT2 = find_token(document.body, "\\begin_inset ERT", end_ERT, end_layout)
+          if lineERT2 != -1:
+            end_ERT2 = find_end_of_inset(document.body, lineERT)
+            if end_ERT2 == -1:
+              document.warning("Can't find end of second ERT!!")
+              break
+            if opt:
+              closing = find_token(document.body, "]", lineERT2, end_ERT2)
+            else:
+              closing = find_token(document.body, "}", lineERT2, end_ERT2)
+            if closing != -1: # assure that the "}" is in this ERT
+              end2 = find_token(document.body, "\\end_inset", closing)
+              document.body[lineERT2 : end2 + 1] = ["\\end_layout", "", "\\end_inset"]
 
 
 ###############################################################################
-- 
1.7.11.7

Reply via email to