mirror of
https://git.lyx.org/repos/lyx.git
synced 2024-12-22 05:16:21 +00:00
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:
parent
58fa331b8b
commit
0db238a371
@ -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"
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user