-def line2lyx(line):
- '''Converts LaTeX commands, such as: \"u, to unicode characters, and
- escapes backslashes, etc, into ERT. line may well be a multi-line
- string when it is returned.
- NOTE: If we want to convert \label{} into an InsetLabel, then this
- is the place to do it.'''
+def extract_argument(line):
+ 'Extracts a LaTeX argument from the start of line. Returns (arg, rest).'
+
+ if not line:
+ return (None, "")
+
+ bracere = re.compile("(\s*)(.*)")
+ n = bracere.match(line)
+ whitespace = n.group(1)
+ stuff = n.group(2)
+ brace = stuff[:1]
+ if brace != "[" and brace != "{":
+ return (None, line)
+
+ # find closing brace
+ remain = stuff[1:]
+ pos = 0
+ num = 1
+ term = "}"
+ if brace == "[":
+ term = "]"
+ skip = False
+ for c in remain:
+ if skip:
+ skip = False
+ elif c == "\\":
+ skip = True
+ elif c == brace:
+ num += 1
+ elif c == term:
+ num -= 1
+ if c == 0:
+ break
+ pos += 1
+ if num != 0:
+ # We never found the matching brace
+ # So, to be on the safe side, let's just return everything
+ # which will then get wrapped as ERT
+ return (line, "")
+ return (line[:pos + 1], line[pos + 1:])
+
+
+def latex2ert(line):
+ '''Converts LaTeX commands into ERT. line may well be a multi-line
+ string when it is returned.'''