From 0db238a371fe9b9455cea32d88c8c5d4216fc9d9 Mon Sep 17 00:00:00 2001 From: Richard Heck Date: Thu, 4 Nov 2010 17:39:36 +0000 Subject: [PATCH] Factor out some common code. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@36084 a592a061-630c-0410-9148-cb99ea01b6c8 --- lib/lyx2lyx/lyx_2_0.py | 31 ++++++------------------------- lib/lyx2lyx/parser_tools.py | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/lib/lyx2lyx/lyx_2_0.py b/lib/lyx2lyx/lyx_2_0.py index 164e4a1c48..79f9f1be2f 100644 --- a/lib/lyx2lyx/lyx_2_0.py +++ b/lib/lyx2lyx/lyx_2_0.py @@ -25,7 +25,7 @@ import sys, os from parser_tools import find_token, find_end_of, find_tokens, \ find_end_of_inset, find_end_of_layout, find_token_backwards, \ - get_value, get_value_string + get_containing_inset, get_value, get_value_string from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \ put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \ @@ -1587,18 +1587,10 @@ def revert_nameref(document): i += 1 # Make sure it is actually in an inset! # A normal line could begin with "LatexCommand nameref"! - # We could just check document.lines[i-1], but that relies - # upon something that might easily change. - # So let's see if we're in a ref inset... - stins = find_token_backwards(document.body, "\\begin_inset CommandInset ref", cmdloc) + stins, endins = get_containing_inset(document.body, cmdloc, \ + "\\begin_inset CommandInset ref") if stins == -1: continue - endins = find_end_of_inset(document.body, stins) - if endins == -1: - document.warning("Can't find end of inset at line " + stins + "!!") - continue - if endins < cmdloc: - continue # ok, so it is in an InsetRef refline = find_token(document.body, "reference", stins, endins) @@ -1631,20 +1623,9 @@ def remove_Nameref(document): i += 1 # Make sure it is actually in an inset! - # We could just check document.lines[i-1], but that relies - # upon something that might easily change. - # We'll look back a few lines. - stins = cmdloc - 10 - if stins < 0: - stins = 0 - stins = find_token(document.body, "\\begin_inset CommandInset ref", stins) - if stins == -1 or stins > cmdloc: - continue - endins = find_end_of_inset(document.body, stins) - if endins == -1: - document.warning("Can't find end of inset at line " + stins + "!!") - continue - if endins < cmdloc: + stins, endins = get_containing_inset(document.body, \ + cmdloc, "\\begin_inset CommandInset ref") + if stins == -1: continue document.body[cmdloc] = "LatexCommand nameref" diff --git a/lib/lyx2lyx/parser_tools.py b/lib/lyx2lyx/parser_tools.py index 3c4a4bdcd7..43ebcd091c 100644 --- a/lib/lyx2lyx/parser_tools.py +++ b/lib/lyx2lyx/parser_tools.py @@ -242,3 +242,18 @@ def find_end_of_inset(lines, i): def find_end_of_layout(lines, i): " Find end of layout, where lines[i] is included." return find_end_of(lines, i, "\\begin_layout", "\\end_layout") + + +# checks if line i is in the inset e.g., "\\begin_inset CommandInset ref" +# if so, returns starting and ending lines +# otherwise, returns (-1, -1) +def get_containing_inset(lines, i, inset): + defval = (-1, -1) + stins = find_token_backwards(lines, inset, i) + if stins == -1: + return defval + endins = find_end_of_inset(lines, stins) + # note that this includes the notfound case. + if endins < i: + return defval + return (stins, endins) -- 2.39.2