Port InsetCommandParams::scanCommand to lyx2lyx.

Now we can parse all commands that could be parsed in the old
InsetCommandParams


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15551 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-10-25 20:08:44 +00:00
parent 7dba7817ce
commit 06139fb170

View File

@ -359,27 +359,78 @@ def convert_commandparams(document):
if i == -1: if i == -1:
break break
command = document.body[i][26:].strip() command = document.body[i][26:].strip()
match = re.match(regex, command) if command == "":
name = match.group(1) document.warning("Malformed LyX document: Missing LatexCommand name.")
option1 = match.group(2) i = i + 1
option2 = match.group(3) continue
argument = match.group(4)
# The following parser is taken from the original InsetCommandParams::scanCommand
name = ""
option1 = ""
option2 = ""
argument = ""
state = "WS"
# Used to handle things like \command[foo[bar]]{foo{bar}}
nestdepth = 0
for j in range(len(command)):
c = command[j]
if ((state == "CMDNAME" and c == ' ') or
(state == "CMDNAME" and c == '[') or
(state == "CMDNAME" and c == '{')):
state = "WS"
if ((state == "OPTION" and c == ']') or
(state == "SECOPTION" and c == ']') or
(state == "CONTENT" and c == '}')):
if nestdepth == 0:
state = "WS"
else:
--nestdepth
if ((state == "OPTION" and c == '[') or
(state == "SECOPTION" and c == '[') or
(state == "CONTENT" and c == '{')):
++nestdepth
if state == "CMDNAME":
name += c
elif state == "OPTION":
option1 += c
elif state == "SECOPTION":
option2 += c
elif state == "CONTENT":
argument += c
elif state == "WS":
if j > 0:
b = command[j-1]
else:
b = 0
if c == '\\':
state = "CMDNAME"
elif c == '[' and b != ']':
state = "OPTION"
nestdepth = 0 # Just to be sure
elif c == '[' and b == ']':
state = "SECOPTION"
nestdepth = 0 # Just to be sure
elif c == '{':
state = "CONTENT"
nestdepth = 0 # Just to be sure
# Now we have parsed the command, output the parameters
lines = ["\\begin_inset LatexCommand %s" % name] lines = ["\\begin_inset LatexCommand %s" % name]
if option1 != None: if option1 != "":
if commandparams_info[name][0] == "": if commandparams_info[name][0] == "":
document.warning("Ignoring invalid option `%s' of command `%s'." % (option1[1:-1], name)) document.warning("Ignoring invalid option `%s' of command `%s'." % (option1, name))
else: else:
lines.append('%s "%s"' % (commandparams_info[name][0], option1[1:-1].replace('"', '\\"'))) lines.append('%s "%s"' % (commandparams_info[name][0], option1.replace('"', '\\"')))
if option2 != None: if option2 != "":
if commandparams_info[name][1] == "": if commandparams_info[name][1] == "":
document.warning("Ignoring invalid second option `%s' of command `%s'." % (option2[1:-1], name)) document.warning("Ignoring invalid second option `%s' of command `%s'." % (option2, name))
else: else:
lines.append('%s "%s"' % (commandparams_info[name][1], option2[1:-1].replace('"', '\\"'))) lines.append('%s "%s"' % (commandparams_info[name][1], option2.replace('"', '\\"')))
if argument != None: if argument != "":
if commandparams_info[name][2] == "": if commandparams_info[name][2] == "":
document.warning("Ignoring invalid argument `%s' of command `%s'." % (argument[1:-1], name)) document.warning("Ignoring invalid argument `%s' of command `%s'." % (argument, name))
else: else:
lines.append('%s "%s"' % (commandparams_info[name][2], argument[1:-1].replace('"', '\\"'))) lines.append('%s "%s"' % (commandparams_info[name][2], argument.replace('"', '\\"')))
document.body[i:i+1] = lines document.body[i:i+1] = lines
i = i + 1 i = i + 1