Factor out some common code.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@36084 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Richard Heck 2010-11-04 17:39:36 +00:00
parent 58fa331b8b
commit 0db238a371
2 changed files with 21 additions and 25 deletions

View File

@ -25,7 +25,7 @@ import sys, os
from parser_tools import find_token, find_end_of, find_tokens, \ from parser_tools import find_token, find_end_of, find_tokens, \
find_end_of_inset, find_end_of_layout, find_token_backwards, \ 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, \ from lyx2lyx_tools import add_to_preamble, insert_to_preamble, \
put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \ put_cmd_in_ert, lyx2latex, latex_length, revert_flex_inset, \
@ -1587,18 +1587,10 @@ def revert_nameref(document):
i += 1 i += 1
# Make sure it is actually in an inset! # Make sure it is actually in an inset!
# A normal line could begin with "LatexCommand nameref"! # A normal line could begin with "LatexCommand nameref"!
# We could just check document.lines[i-1], but that relies stins, endins = get_containing_inset(document.body, cmdloc, \
# upon something that might easily change. "\\begin_inset CommandInset ref")
# So let's see if we're in a ref inset...
stins = find_token_backwards(document.body, "\\begin_inset CommandInset ref", cmdloc)
if stins == -1: if stins == -1:
continue 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 # ok, so it is in an InsetRef
refline = find_token(document.body, "reference", stins, endins) refline = find_token(document.body, "reference", stins, endins)
@ -1631,20 +1623,9 @@ def remove_Nameref(document):
i += 1 i += 1
# Make sure it is actually in an inset! # Make sure it is actually in an inset!
# We could just check document.lines[i-1], but that relies stins, endins = get_containing_inset(document.body, \
# upon something that might easily change. cmdloc, "\\begin_inset CommandInset ref")
# We'll look back a few lines. if stins == -1:
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:
continue continue
document.body[cmdloc] = "LatexCommand nameref" document.body[cmdloc] = "LatexCommand nameref"

View File

@ -242,3 +242,18 @@ def find_end_of_inset(lines, i):
def find_end_of_layout(lines, i): def find_end_of_layout(lines, i):
" Find end of layout, where lines[i] is included." " Find end of layout, where lines[i] is included."
return find_end_of(lines, i, "\\begin_layout", "\\end_layout") 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)