# \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)
state = "WS"
# Used to handle things like \command[foo[bar]]{foo{bar}}
nestdepth = 0
- for j in range(len(command)):
- c = command[j]
+ b = 0
+ for c in command:
if ((state == "CMDNAME" and c == ' ') or
(state == "CMDNAME" and c == '[') or
(state == "CMDNAME" and c == '{')):
if nestdepth == 0:
state = "WS"
else:
- --nestdepth
+ nestdepth = nestdepth - 1
if ((state == "OPTION" and c == '[') or
(state == "SECOPTION" and c == '[') or
(state == "CONTENT" and c == '{')):
- ++nestdepth
+ nestdepth = nestdepth + 1
if state == "CMDNAME":
name += c
elif state == "OPTION":
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 != ']':
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]
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]],