]> git.lyx.org Git - features.git/blobdiff - lib/lyx2lyx/lyx_2_3.py
Support for multiple bibliographies
[features.git] / lib / lyx2lyx / lyx_2_3.py
index a2adde7339a48cebfad4112c61a2e583b77354ff..3b46470b18dd85af4df9e3d929ccbd4131e40f8a 100644 (file)
@@ -1191,7 +1191,7 @@ def revert_biblatex(document):
     biblatex = False
     if engine in ["biblatex", "biblatex-natbib"]:
         biblatex = True
-        document.header[i] = "\cite_engine natbib"
+        document.header[i] = "\\cite_engine natbib"
 
     # 3. Store and remove new document headers
     bibstyle = ""
@@ -1331,10 +1331,10 @@ def revert_biblatex(document):
             res = "\\" + new_citations[cmd]
             if pre:
                 res += "[" + pre + "]"
-            elif post:
-                res += "[]"
             if post:
                 res += "[" + post + "]"
+            elif pre:
+                res += "[]"
             res += "{" + key + "}"
             document.body[i:j+1] = put_cmd_in_ert([res])
         elif cmd not in old_citations:
@@ -1400,6 +1400,343 @@ def revert_citekeyonly(document):
         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
+
+    i = find_token(document.header, "\\biblio_options", 0)
+    if i == -1:
+        # Nothing to do if we have no options
+        return
+
+    biblio_options = get_value(document.header, "\\biblio_options", i)
+    del document.header[i]
+
+    if not biblio_options:
+        # Nothing to do for empty options
+        return
+
+    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) ###"
+    ]
+
+
+def revert_qualicites(document):
+    " Revert qualified citation list commands to ERT "
+
+    # Citation insets that support qualified lists, with their LaTeX code
+    ql_citations = {
+        "cite" : "cites",
+        "Cite" : "Cites",
+        "citet" : "textcites",
+        "Citet" : "Textcites",
+        "citep" : "parencites",
+        "Citep" : "Parencites",
+        "Footcite" : "Smartcites",
+        "footcite" : "smartcites",
+        "Autocite" : "Autocites",
+        "autocite" : "autocites",
+        }
+
+    # Get cite engine
+    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 = engine in ["biblatex", "biblatex-natbib"]
+
+    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
+        pres = find_token(document.body, "pretextlist", i, j)
+        posts = find_token(document.body, "posttextlist", i, j)
+        if pres == -1 and posts == -1:
+            # nothing to do.
+            i = j + 1
+            continue
+        pretexts = get_quoted_value(document.body, "pretextlist", pres)
+        posttexts = get_quoted_value(document.body, "posttextlist", posts)
+        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 biblatex and cmd in list(ql_citations.keys()):
+            pre = get_quoted_value(document.body, "before", i, j)
+            post = get_quoted_value(document.body, "after", i, j)
+            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))
+                key = "???"
+            keys = key.split(",")
+            prelist = pretexts.split("\t")
+            premap = dict()
+            for pp in prelist:
+                ppp = pp.split(" ", 1)
+                premap[ppp[0]] = ppp[1]
+            postlist = posttexts.split("\t")
+            postmap = dict()
+            for pp in postlist:
+                ppp = pp.split(" ", 1)
+                postmap[ppp[0]] = ppp[1]
+            # Replace known new commands with ERT
+            if "(" in pre or ")" in pre:
+                pre = "{" + pre + "}"
+            if "(" in post or ")" in post:
+                post = "{" + post + "}"
+            res = "\\" + ql_citations[cmd]
+            if pre:
+                res += "(" + pre + ")"
+            if post:
+                res += "(" + post + ")"
+            elif pre:
+                res += "()"
+            for kk in keys:
+                if premap.get(kk, "") != "":
+                    res += "[" + premap[kk] + "]"
+                if postmap.get(kk, "") != "":
+                    res += "[" + postmap[kk] + "]"
+                elif premap.get(kk, "") != "":
+                    res += "[]"
+                res += "{" + kk + "}"
+            document.body[i:j+1] = put_cmd_in_ert([res])
+        else:
+            # just remove the params
+            del document.body[posttexts]
+            del document.body[pretexts]
+            i += 1
+
+
+command_insets = ["bibitem", "citation", "href", "index_print", "nomenclature"]
+def convert_literalparam(document):
+    " Add param literal "
+
+    # These already had some sort of latexify method
+    latexified_insets = ["href", "index_print", "nomenclature"]
+
+    for inset in command_insets:
+        i = 0
+        while True:
+            i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
+            if i == -1:
+                break
+            j = find_end_of_inset(document.body, i)
+            if j == -1:
+                document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
+                i += 1
+                continue
+            while i < j and document.body[i].strip() != '':
+                i += 1
+            if inset in latexified_insets:
+                document.body.insert(i, "literal \"false\"")
+            else:
+                document.body.insert(i, "literal \"true\"")
+
+
+
+def revert_literalparam(document):
+    " Remove param literal "
+
+    for inset in command_insets:
+        i = 0
+        while True:
+            i = find_token(document.body, '\\begin_inset CommandInset %s' % inset, i)
+            if i == -1:
+                break
+            j = find_end_of_inset(document.body, i)
+            if j == -1:
+                document.warning("Malformed LyX document: Can't find end of %s inset at line %d" % (inset, i))
+                i += 1
+                continue
+            k = find_token(document.body, 'literal', i, j)
+            if k == -1:
+                i += 1
+                continue
+            del document.body[k]
+
+
+
+def revert_multibib(document):
+    " Revert multibib support "
+
+    # 1. Get cite engine
+    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)
+
+    # 2. Do we use biblatex?
+    biblatex = False
+    if engine in ["biblatex", "biblatex-natbib"]:
+        biblatex = True
+
+    # 3. Store and remove multibib document header
+    multibib = ""
+    i = find_token(document.header, "\\multibib", 0)
+    if i != -1:
+        multibib = get_value(document.header, "\\multibib", i)
+        del document.header[i]
+
+    if not multibib:
+        return
+
+    # 4. The easy part: Biblatex
+    if biblatex:
+        i = find_token(document.header, "\\biblio_options", 0)
+        if i == -1:
+            k = find_token(document.header, "\\use_bibtopic", 0)
+            if k == -1:
+                # this should not happen
+                document.warning("Malformed LyX document! No \\use_bibtopic header found!")
+                return
+            document.header[k-1 : k-1] = ["\\biblio_options " + "refsection=" + multibib]
+        else:
+            biblio_options = get_value(document.header, "\\biblio_options", i)
+            if biblio_options:
+                biblio_options += ","
+            biblio_options += "refsection=" + multibib
+            document.header[i] = "\\biblio_options " + biblio_options
+
+        # Bibtex insets
+        i = 0
+        while (True):
+            i = find_token(document.body, "\\begin_inset CommandInset bibtex", i)
+            if i == -1:
+                break
+            j = find_end_of_inset(document.body, i)
+            if j == -1:
+                document.warning("Can't find end of bibtex inset at line %d!!" %(i))
+                i += 1
+                continue
+            btprint = get_quoted_value(document.body, "btprint", i, j)
+            if btprint != "bibbysection":
+                i += 1
+                continue
+            opts = get_quoted_value(document.body, "biblatexopts", i, j)
+            # change btprint line
+            k = find_token(document.body, "btprint", i, j)
+            if k != -1:
+                document.body[k] = "btprint \"btPrintCited\""
+            # Insert ERT \\bibbysection and wrap bibtex inset to a Note
+            pcmd = "bibbysection"
+            if opts:
+                pcmd += "[" + opts + "]"
+            repl = ["\\begin_inset ERT", "status open", "", "\\begin_layout Plain Layout",\
+                    "", "", "\\backslash", pcmd, "\\end_layout", "", "\\end_inset", "", "",\
+                    "\\end_layout", "", "\\begin_layout Standard", "\\begin_inset Note Note",\
+                    "status open", "", "\\begin_layout Plain Layout" ]
+            repl += document.body[i:j+1]
+            repl += ["", "\\end_layout", "", "\\end_inset", "", ""]
+            document.body[i:j+1] = repl
+            j += 27
+
+            i = j + 1
+        return
+
+    # 5. More tricky: Bibtex/Bibtopic
+    k = find_token(document.header, "\\use_bibtopic", 0)
+    if k == -1:
+        # this should not happen
+        document.warning("Malformed LyX document! No \\use_bibtopic header found!")
+        return
+    document.header[k] = "\\use_bibtopic true"
+
+    # Possible units. This assumes that the LyX name follows the std,
+    # which might not always be the case. But it's as good as we can get.
+    units = {
+        "part" : "Part",
+        "chapter" : "Chapter",
+        "section" : "Section",
+        "subsection" : "Subsection",
+        }
+
+    if multibib not in units.keys():
+        document.warning("Unknown multibib value `%s'!" % nultibib)
+        return
+    unit = units[multibib]
+    btunit = False
+    i = 0
+    while (True):
+        i = find_token(document.body, "\\begin_layout " + unit, i)
+        if i == -1:
+            break
+        if btunit:
+            document.body[i-1 : i-1] = ["\\begin_layout Standard",
+                                "\\begin_inset ERT", "status open", "",
+                                "\\begin_layout Plain Layout", "", "",
+                                "\\backslash",
+                                "end{btUnit}", "\\end_layout",
+                                "\\begin_layout Plain Layout", "",
+                                "\\backslash",
+                                "begin{btUnit}"
+                                "\\end_layout", "", "\\end_inset", "", "",
+                                "\\end_layout", ""]
+            i += 21
+        else:
+            document.body[i-1 : i-1] = ["\\begin_layout Standard",
+                                "\\begin_inset ERT", "status open", "",
+                                "\\begin_layout Plain Layout", "", "",
+                                "\\backslash",
+                                "begin{btUnit}"
+                                "\\end_layout", "", "\\end_inset", "", "",
+                                "\\end_layout", ""]
+            i += 16
+        btunit = True
+        i += 1
+
+    if btunit:
+        i = find_token(document.body, "\\end_body", i)
+        document.body[i-1 : i-1] = ["\\begin_layout Standard",
+                                "\\begin_inset ERT", "status open", "",
+                                "\\begin_layout Plain Layout", "", "",
+                                "\\backslash",
+                                "end{btUnit}"
+                                "\\end_layout", "", "\\end_inset", "", "",
+                                "\\end_layout", ""]
+    
+
 ##
 # Conversion hub
 #
@@ -1426,10 +1763,18 @@ convert = [
            [526, []],
            [527, []],
            [528, []],
-           [529, []]
+           [529, []],
+           [530, []],
+           [531, []],
+           [532, [convert_literalparam]],
+           [533, []],
           ]
 
 revert =  [
+           [532, [revert_multibib]],
+           [531, [revert_literalparam]],
+           [530, [revert_qualicites]],
+           [529, [revert_bibpackopts]],
            [528, [revert_citekeyonly]],
            [527, [revert_biblatex]],
            [526, [revert_noprefix]],