# This file is part of lyx2lyx
# -*- coding: utf-8 -*-
-# Copyright (C) 2002-2004 Dekel Tsur <dekel@lyx.org>, José Matos <jamatos@lyx.org>
+# Copyright (C) 2002-2010 Dekel Tsur <dekel@lyx.org>,
+# José Matos <jamatos@lyx.org>, Richard Heck <rgheck@comcast.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-" This modules offer several free functions to help parse lines."
+" This modules offer several free functions to help parse lines. "
# Utilities for one line
def check_token(line, token):
if len(x) < len(y):
continue
if x[:len(y)] == y:
- return i
+ return i
else:
if lines[i][:len(token)] == token:
return i
i = find_token_exact(lines, token, start, end)
if i == -1:
- return ""
+ return default
if len(lines[i].split()) > 1:
return lines[i].split()[1]
else:
return default
+def get_value_string(lines, token, start, end = 0, trim = False, default = ""):
+ """ get_value_string(lines, token, start[[, end], trim, default]) -> string
+
+ Return tokens after token as string, in lines, where
+ token is the first element. When trim is used, the first and last character
+ of the string is trimmed."""
+
+ i = find_token_exact(lines, token, start, end)
+ if i == -1:
+ return default
+ if len(lines[i].split()) > 1:
+ for k in range (0, len(lines[i])):
+ if lines[i][k] == ' ':
+ if trim ==False:
+ return lines[i][k+1:len(lines[i])]
+ else:
+ return lines[i][k+2:len(lines[i])-1]
+ else:
+ return default
+
+
def del_token(lines, token, start, end):
""" del_token(lines, token, start, end) -> int
count = 1
while i > 0:
i = find_tokens_backwards(lines, [start_token, end_token], i-1)
+ if i == -1:
+ return -1
if check_token(lines[i], end_token):
count = count+1
else:
n = len(lines)
while i < n:
i = find_tokens(lines, [end_token, start_token], i+1)
+ if i == -1:
+ return -1
if check_token(lines[i], start_token):
count = count+1
else:
if is_nonempty_line(lines[i]):
return i
return -1
+
+
+def find_end_of_inset(lines, i):
+ " Find end of inset, where lines[i] is included."
+ return find_end_of(lines, i, "\\begin_inset", "\\end_inset")
+
+
+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)