]> git.lyx.org Git - lyx.git/blobdiff - lib/lyx2lyx/lyx_1_5.py
* doc/cs_Tutorial.lyx: update by Pavel Sanda
[lyx.git] / lib / lyx2lyx / lyx_1_5.py
index 17f9f2d5e35798ec1d99364e7deece128b71005f..d4f00cfe3a30df2d593a5e3939db08437562ca61 100644 (file)
@@ -350,9 +350,6 @@ def convert_commandparams(document):
     # \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)
@@ -384,11 +381,11 @@ def convert_commandparams(document):
                 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":
@@ -488,6 +485,125 @@ def revert_commandparams(document):
         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
 #
@@ -499,9 +615,13 @@ convert = [[246, []],
            [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]],