]> 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 20bfde981986120b2d316fbc5d7b6fc09f42065f..762c884790153483f8326cf2c748c62a8a044501 100644 (file)
@@ -25,14 +25,11 @@ import sys, os
 
 # Uncomment only what you need to import, please.
 
-from parser_tools import find_end_of, find_token_backwards, find_end_of_layout#,
-#  find_token, find_tokens, \
-#  find_token_exact, find_end_of_inset, \
-#  is_in_inset, get_value, get_quoted_value, \
-#  del_token, check_token, get_option_value, get_bool_value
-
-from parser_tools import find_token, find_end_of_inset, get_value, \
-     get_bool_value, get_containing_layout
+from parser_tools import find_end_of, find_token_backwards, find_end_of_layout, \
+    find_token, find_end_of_inset, get_value,  get_bool_value, \
+    get_containing_layout, get_quoted_value, del_token
+#  find_tokens, find_token_exact, is_in_inset, \
+#  check_token, get_option_value
 
 from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
 #  get_ert, lyx2latex, \
@@ -40,8 +37,6 @@ from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
 #  insert_to_preamble, latex_length, revert_flex_inset, \
 #  revert_font_attrs, hex2ratio, str2bool
 
-from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert
-
 ####################################################################
 # Private helper functions
 
@@ -1023,7 +1018,7 @@ def revert_cjkquotes(document):
                 if cjk:
                     replace = [u"\u300A"]
                 else:
-                    replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$$", "\\end_inset"]
+                    replace = ["\\begin_inset Formula $\\langle\\kern -2.5pt\\langle$", "\\end_inset"]
             else:
                 # outer closing mark
                 if cjk:
@@ -1035,6 +1030,420 @@ def revert_cjkquotes(document):
         i = l
 
 
+def revert_crimson(document):
+    " Revert native Cochineal/Crimson font definition to LaTeX " 
+
+    if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1:
+        preamble = ""
+        i = find_token(document.header, "\\font_roman \"cochineal\"", 0)
+        if i != -1:
+            osf = False
+            j = find_token(document.header, "\\font_osf true", 0)
+            if j != -1:
+                osf = True
+            preamble = "\\usepackage"
+            if osf:
+                document.header[j] = "\\font_osf false"
+                preamble += "[proportional,osf]"
+            preamble += "{cochineal}"
+            add_to_preamble(document, [preamble])
+            document.header[i] = document.header[i].replace("cochineal", "default")
+
+
+def revert_cochinealmath(document):
+    " Revert cochineal newtxmath definitions to LaTeX " 
+
+    if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: 
+        i = find_token(document.header, "\\font_math \"cochineal-ntxm\"", 0)
+        if i != -1:
+            add_to_preamble(document, "\\usepackage[cochineal]{newtxmath}")
+            document.header[i] = document.header[i].replace("cochineal-ntxm", "auto")
+
+
+def revert_labelonly(document):
+    " Revert labelonly tag for InsetRef "
+    i = 0
+    while (True):
+        i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Can't find end of reference inset at line %d!!" %(i))
+            i += 1
+            continue
+        k = find_token(document.body, "LatexCommand labelonly", i, j)
+        if k == -1:
+            i = j
+            continue
+        label = get_quoted_value(document.body, "reference", i, j)
+        if not label:
+            document.warning("Can't find label for reference at line %d!" %(i))
+            i = j + 1
+            continue
+        document.body[i:j+1] = put_cmd_in_ert([label])
+        i += 1
+
+
+def revert_plural_refs(document):
+    " Revert plural and capitalized references "
+    i = find_token(document.header, "\\use_refstyle 1", 0)
+    use_refstyle = (i != 0)
+
+    i = 0
+    while (True):
+        i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Can't find end of reference inset at line %d!!" %(i))
+            i += 1
+            continue
+
+        plural = caps = suffix = False
+        k = find_token(document.body, "LaTeXCommand formatted", i, j)
+        if k != -1 and use_refstyle:
+            plural = get_bool_value(document.body, "plural", i, j, False)
+            caps   = get_bool_value(document.body, "caps", i, j, False)
+            label  = get_quoted_value(document.body, "reference", i, j)
+            if label:
+                try:
+                    (prefix, suffix) = label.split(":", 1)
+                except:
+                    document.warning("No `:' separator in formatted reference at line %d!" % (i))
+            else:
+                document.warning("Can't find label for reference at line %d!" % (i))
+
+        # this effectively tests also for use_refstyle and a formatted reference
+        # we do this complicated test because we would otherwise do this erasure
+        # over and over and over
+        if not ((plural or caps) and suffix):
+            del_token(document.body, "plural", i, j)
+            del_token(document.body, "caps", i, j - 1) # since we deleted a line
+            i = j - 1
+            continue
+
+        if caps:
+            prefix = prefix[0].title() + prefix[1:]
+        cmd = "\\" + prefix + "ref"
+        if plural:
+            cmd += "[s]"
+        cmd += "{" + suffix + "}"
+        document.body[i:j+1] = put_cmd_in_ert([cmd])
+        i += 1
+
+
+def revert_noprefix(document):
+    " Revert labelonly tags with 'noprefix' set "
+    i = 0
+    while (True):
+        i = find_token(document.body, "\\begin_inset CommandInset ref", i)
+        if i == -1:
+            return
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Can't find end of reference inset at line %d!!" %(i))
+            i += 1
+            continue
+        k = find_token(document.body, "LatexCommand labelonly", i, j)
+        if k == -1:
+            i = j
+            continue
+        noprefix = get_bool_value(document.body, "noprefix", i, j)
+        if not noprefix:
+            del_token(document.body, "noprefix", i, j)
+            i = j
+            continue
+        label = get_quoted_value(document.body, "reference", i, j)
+        if not label:
+            document.warning("Can't find label for reference at line %d!" %(i))
+            i = j + 1
+            continue
+        try:
+            (prefix, suffix) = label.split(":", 1)
+        except:
+            document.warning("No `:' separator in formatted reference at line %d!" % (i))
+            # we'll leave this as an ordinary labelonly reference
+            del_token(document.body, "noprefix", i, j)
+            i = j
+            continue
+        document.body[i:j+1] = put_cmd_in_ert([suffix])
+        i += 1
+
+
+def revert_biblatex(document):
+    " Revert biblatex support "
+
+    #
+    # Header
+    #
+
+    # 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. Store biblatex state and revert to natbib
+    biblatex = False
+    if engine in ["biblatex", "biblatex-natbib"]:
+        biblatex = True
+        document.header[i] = "\cite_engine natbib"
+
+    # 3. Store and remove new document headers
+    bibstyle = ""
+    i = find_token(document.header, "\\biblatex_bibstyle", 0)
+    if i != -1:
+        bibstyle = get_value(document.header, "\\biblatex_bibstyle", i)
+        del document.header[i]
+
+    citestyle = ""
+    i = find_token(document.header, "\\biblatex_citestyle", 0)
+    if i != -1:
+        citestyle = get_value(document.header, "\\biblatex_citestyle", i)
+        del document.header[i]
+
+    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]
+
+    if biblatex:
+        bbxopts = "[natbib=true"
+        if bibstyle != "":
+            bbxopts += ",bibstyle=" + bibstyle
+        if citestyle != "":
+            bbxopts += ",citestyle=" + citestyle
+        if biblio_options != "":
+            bbxopts += "," + biblio_options
+        bbxopts += "]"
+        add_to_preamble(document, "\\usepackage" + bbxopts + "{biblatex}")
+
+    #
+    # Body
+    #
+
+    # 1. Bibtex insets
+    i = 0
+    bibresources = []
+    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
+        bibs = get_quoted_value(document.body, "bibfiles", i, j)
+        opts = get_quoted_value(document.body, "biblatexopts", i, j)
+        # store resources
+        if bibs:
+            bibresources += bibs.split(",")
+        else:
+            document.warning("Can't find bibfiles for bibtex inset at line %d!" %(i))
+        # remove biblatexopts line
+        k = find_token(document.body, "biblatexopts", i, j)
+        if k != -1:
+            del document.body[k]
+        # Re-find inset end line
+        j = find_end_of_inset(document.body, i)
+        # Insert ERT \\printbibliography and wrap bibtex inset to a Note
+        if biblatex:
+            pcmd = "printbibliography"
+            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
+
+    if biblatex:
+        for b in bibresources:
+            add_to_preamble(document, "\\addbibresource{" + b + ".bib}")
+
+    # 2. Citation insets
+
+    # Specific citation insets used in biblatex that need to be reverted to ERT
+    new_citations = {
+        "Cite" : "Cite",
+        "citebyear" : "citeyear",
+        "citeyear" : "cite*",
+        "Footcite" : "Smartcite",
+        "footcite" : "smartcite",
+        "Autocite" : "Autocite",
+        "autocite" : "autocite",
+        "citetitle" : "citetitle",
+        "citetitle*" : "citetitle*",
+        "fullcite" : "fullcite",
+        "footfullcite" : "footfullcite",
+        "supercite" : "supercite",
+        "citeauthor" : "citeauthor",
+        "citeauthor*" : "citeauthor*",
+        "Citeauthor" : "Citeauthor",
+        "Citeauthor*" : "Citeauthor*"
+        }
+
+    # All commands accepted by LyX < 2.3. Everything else throws an error.
+    old_citations = [ "cite", "nocite", "citet", "citep", "citealt", "citealp",\
+                     "citeauthor", "citeyear", "citeyearpar", "citet*", "citep*",\
+                      "citealt*", "citealp*", "citeauthor*", "Citet",  "Citep",\
+                      "Citealt",  "Citealp",  "Citeauthor", "Citet*", "Citep*",\
+                      "Citealt*", "Citealp*", "Citeauthor*", "fullcite", "footcite",\
+                      "footcitet", "footcitep", "footcitealt", "footcitealp",\
+                      "footciteauthor", "footciteyear", "footciteyearpar",\
+                     "citefield", "citetitle", "cite*" ]
+
+    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 biblatex and cmd in list(new_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 = "???"
+            # Replace known new commands with ERT
+            res = "\\" + new_citations[cmd]
+            if pre:
+                res += "[" + pre + "]"
+            elif post:
+                res += "[]"
+            if post:
+                res += "[" + post + "]"
+            res += "{" + key + "}"
+            document.body[i:j+1] = put_cmd_in_ert([res])
+        elif cmd not in old_citations:
+            # Reset unknown commands to cite. This is what LyX does as well
+            # (but LyX 2.2 would break on unknown commands)
+            document.body[k] = "LatexCommand cite"
+            document.warning("Reset unknown cite command '%s' with cite" % cmd)
+        i = j + 1
+
+    # Emulate the old biblatex-workaround (pretend natbib in order to use the styles)
+    if biblatex:
+        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 (biblatex emulation) ###",
+            "Provides natbib 1",
+            "### End of insertion by lyx2lyx (biblatex emulation) ###"
+        ]
+
+
+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
 #
@@ -1055,10 +1464,24 @@ convert = [
            [520, []],
            [521, [convert_frenchquotes]],
            [522, []],
-           [523, []]
+           [523, []],
+           [524, []],
+           [525, []],
+           [526, []],
+           [527, []],
+           [528, []],
+           [529, []],
+           [530, []]
           ]
 
 revert =  [
+           [529, [revert_bibpackopts]],
+           [528, [revert_citekeyonly]],
+           [527, [revert_biblatex]],
+           [526, [revert_noprefix]],
+           [525, [revert_plural_refs]],
+           [524, [revert_labelonly]],
+           [523, [revert_crimson, revert_cochinealmath]],
            [522, [revert_cjkquotes]],
            [521, [revert_dynamicquotes]],
            [520, [revert_britishquotes, revert_swedishgquotes, revert_frenchquotes, revert_frenchinquotes, revert_russianquotes, revert_swissquotes]],