# \begin_inset LatexCommand bibitem was not the official version (see
# convert_bibitem()), but could be read in, so we convert it here, too.
- # FIXME: Handle things like \command[foo[bar]]{foo{bar}}
- # we need a real parser here.
- regex = re.compile(r'\\([^\[\{]+)(\[[^\[\{]*\])?(\[[^\[\{]*\])?(\{[^}]*\})?')
i = 0
while 1:
i = find_token(document.body, "\\begin_inset LatexCommand", i)
if i == -1:
break
command = document.body[i][26:].strip()
- match = re.match(regex, command)
- name = match.group(1)
- option1 = match.group(2)
- option2 = match.group(3)
- argument = match.group(4)
+ if command == "":
+ document.warning("Malformed LyX document: Missing LatexCommand name.")
+ i = i + 1
+ continue
+
+ # 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
+ b = 0
+ for c in command:
+ 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 = nestdepth - 1
+ if ((state == "OPTION" and c == '[') or
+ (state == "SECOPTION" and c == '[') or
+ (state == "CONTENT" and c == '{')):
+ nestdepth = nestdepth + 1
+ if state == "CMDNAME":
+ name += c
+ elif state == "OPTION":
+ option1 += c
+ elif state == "SECOPTION":
+ option2 += c
+ elif state == "CONTENT":
+ argument += c
+ elif state == "WS":
+ 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
+ b = c
+
+ # Now we have parsed the command, output the parameters
lines = ["\\begin_inset LatexCommand %s" % name]
- if option1 != None:
+ if option1 != "":
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:
- lines.append('%s "%s"' % (commandparams_info[name][0], option1[1:-1].replace('"', '\\"')))
- if option2 != None:
+ lines.append('%s "%s"' % (commandparams_info[name][0], option1.replace('"', '\\"')))
+ if option2 != "":
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:
- lines.append('%s "%s"' % (commandparams_info[name][1], option2[1:-1].replace('"', '\\"')))
- if argument != None:
+ lines.append('%s "%s"' % (commandparams_info[name][1], option2.replace('"', '\\"')))
+ if argument != "":
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:
- 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
i = i + 1
i = j + 1
+def revert_nomenclature(document):
+ " Convert nomenclature entry to ERT. "
+ regex = re.compile(r'(\S+)\s+(.+)')
+ i = 0
+ use_nomencl = 0
+ while 1:
+ i = find_token(document.body, "\\begin_inset LatexCommand nomenclature", i)
+ if i == -1:
+ break
+ use_nomencl = 1
+ j = find_end_of_inset(document.body, i + 1)
+ preview_line = ""
+ symbol = ""
+ description = ""
+ prefix = ""
+ for k in range(i + 1, j):
+ match = re.match(regex, document.body[k])
+ if match:
+ name = match.group(1)
+ value = match.group(2)
+ if name == "preview":
+ preview_line = document.body[k]
+ elif name == "symbol":
+ symbol = value.strip('"').replace('\\"', '"')
+ elif name == "description":
+ description = value.strip('"').replace('\\"', '"')
+ elif name == "prefix":
+ prefix = value.strip('"').replace('\\"', '"')
+ elif document.body[k].strip() != "":
+ document.warning("Ignoring unknown contents `%s' in nomenclature inset." % document.body[k])
+ if prefix == "":
+ command = 'nomenclature{%s}{%s}' % (symbol, description)
+ else:
+ command = 'nomenclature[%s]{%s}{%s}' % (prefix, symbol, description)
+ document.body[i:j+1] = ['\\begin_inset ERT',
+ 'status collapsed',
+ '',
+ '\\begin_layout %s' % document.default_layout,
+ '',
+ '',
+ '\\backslash',
+ command,
+ '\\end_layout',
+ '',
+ '\\end_inset']
+ i = i + 11
+ if use_nomencl and find_token(document.preamble, '\\usepackage{nomencl}[2005/09/22]', 0) == -1:
+ document.preamble.append('\\usepackage{nomencl}[2005/09/22]')
+ document.preamble.append('\\makenomenclature')
+
+
+def revert_printnomenclature(document):
+ " Convert printnomenclature to ERT. "
+ regex = re.compile(r'(\S+)\s+(.+)')
+ i = 0
+ use_nomencl = 0
+ while 1:
+ i = find_token(document.body, "\\begin_inset LatexCommand printnomenclature", i)
+ if i == -1:
+ break
+ use_nomencl = 1
+ j = find_end_of_inset(document.body, i + 1)
+ preview_line = ""
+ labelwidth = ""
+ for k in range(i + 1, j):
+ match = re.match(regex, document.body[k])
+ if match:
+ name = match.group(1)
+ value = match.group(2)
+ if name == "preview":
+ preview_line = document.body[k]
+ elif name == "labelwidth":
+ labelwidth = value.strip('"').replace('\\"', '"')
+ elif document.body[k].strip() != "":
+ document.warning("Ignoring unknown contents `%s' in printnomenclature inset." % document.body[k])
+ if labelwidth == "":
+ command = 'nomenclature{}'
+ else:
+ command = 'nomenclature[%s]' % labelwidth
+ document.body[i:j+1] = ['\\begin_inset ERT',
+ 'status collapsed',
+ '',
+ '\\begin_layout %s' % document.default_layout,
+ '',
+ '',
+ '\\backslash',
+ command,
+ '\\end_layout',
+ '',
+ '\\end_inset']
+ i = i + 11
+ if use_nomencl and find_token(document.preamble, '\\usepackage{nomencl}[2005/09/22]', 0) == -1:
+ document.preamble.append('\\usepackage{nomencl}[2005/09/22]')
+ document.preamble.append('\\makenomenclature')
+
+
+def convert_esint(document):
+ " Add \\use_esint setting to header. "
+ i = find_token(document.header, "\\cite_engine", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing `\\cite_engine'.")
+ return
+ # 0 is off, 1 is auto, 2 is on.
+ document.header.insert(i, '\\use_esint 0')
+
+
+def revert_esint(document):
+ " Remove \\use_esint setting from header. "
+ i = find_token(document.header, "\\use_esint", 0)
+ if i == -1:
+ document.warning("Malformed LyX document: Missing `\\use_esint'.")
+ return
+ use_esint = document.header[i].split()[1]
+ del document.header[i]
+ # 0 is off, 1 is auto, 2 is on.
+ if (use_esint == 2):
+ document.preamble.append('\\usepackage{esint}')
+
+
##
# Conversion hub
#
[249, [convert_utf8]],
[250, []],
[251, []],
- [252, [convert_commandparams, convert_bibitem]]]
+ [252, [convert_commandparams, convert_bibitem]],
+ [253, []],
+ [254, [convert_esint]]]
-revert = [[251, [revert_commandparams]],
+revert = [[253, [revert_esint]],
+ [252, [revert_nomenclature, revert_printnomenclature]],
+ [251, [revert_commandparams]],
[250, [revert_cs_label]],
[249, []],
[248, [revert_utf8]],