]> git.lyx.org Git - features.git/blobdiff - lib/lyx2lyx/lyx_2_3.py
Basic support for natbib & jurabib options
[features.git] / lib / lyx2lyx / lyx_2_3.py
index 4c2eedb4dea45f3bed2fc445eb6b753574e28438..762c884790153483f8326cf2c748c62a8a044501 100644 (file)
@@ -1369,6 +1369,81 @@ def revert_biblatex(document):
         ]
 
 
+def revert_citekeyonly(document):
+    " Revert keyonly cite command to ERT "
+
+    i = 0
+    while (True):
+        i = find_token(document.body, "\\begin_inset CommandInset citation", i)
+        if i == -1:
+            break
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Can't find end of citation inset at line %d!!" %(i))
+            i += 1
+            continue
+        k = find_token(document.body, "LatexCommand", i, j)
+        if k == -1:
+            document.warning("Can't find LatexCommand for citation inset at line %d!" %(i))
+            i = j + 1
+            continue
+        cmd = get_value(document.body, "LatexCommand", k)
+        if cmd != "keyonly":
+            i = j + 1
+            continue
+
+        key = get_quoted_value(document.body, "key", i, j)
+        if not key:
+            document.warning("Citation inset at line %d does not have a key!" %(i))
+        # Replace known new commands with ERT
+        document.body[i:j+1] = put_cmd_in_ert([key])
+        i = j + 1
+
+
+
+def revert_bibpackopts(document):
+    " Revert support for natbib/jurabib package options "
+
+    engine = "basic"
+    i = find_token(document.header, "\\cite_engine", 0)
+    if i == -1:
+        document.warning("Malformed document! Missing \\cite_engine")
+    else:
+        engine = get_value(document.header, "\\cite_engine", i)
+
+    biblatex = False
+    if engine not in ["natbib", "jurabib"]:
+        return
+
+    biblio_options = ""
+    i = find_token(document.header, "\\biblio_options", 0)
+    if i != -1:
+        biblio_options = get_value(document.header, "\\biblio_options", i)
+        del document.header[i]
+
+    i = find_token(document.header, "\\begin_local_layout", 0)
+    if i == -1:
+        k = find_token(document.header, "\\language", 0)
+        if k == -1:
+            # this should not happen
+            document.warning("Malformed LyX document! No \\language header found!")
+            return
+        document.header[k-1 : k-1] = ["\\begin_local_layout", "\\end_local_layout"]
+        i = k - 1
+
+    j = find_end_of(document.header, i, "\\begin_local_layout", "\\end_local_layout")
+    if j == -1:
+        # this should not happen
+        document.warning("Malformed LyX document! Can't find end of local layout!")
+        return
+
+    document.header[i+1 : i+1] = [
+        "### Inserted by lyx2lyx (bibliography package options) ###",
+        "PackageOptions " + engine + " " + biblio_options,
+        "### End of insertion by lyx2lyx (bibliography package options) ###"
+    ]
+
+
 ##
 # Conversion hub
 #
@@ -1394,10 +1469,14 @@ convert = [
            [525, []],
            [526, []],
            [527, []],
-           [528, []]
+           [528, []],
+           [529, []],
+           [530, []]
           ]
 
 revert =  [
+           [529, [revert_bibpackopts]],
+           [528, [revert_citekeyonly]],
            [527, [revert_biblatex]],
            [526, [revert_noprefix]],
            [525, [revert_plural_refs]],