예제 #1
0
파일: PyTex.py 프로젝트: jjwilke/PySkyNet
def loadCitation(cword):
    import os

    import pyvim.pyvim as vim
    import re
    import pygtk
    import gtk
    import os.path

    from pylatex.pybib import Record

    Record.unsetFormat()
    Record.setDefaults()

    entries = []
    if "~" in cword:
        cword = "~" + cword.split("~")[-1]

    if "\cite{" in cword:  # we are currently on a citation
        # get the entries within the citation
        innards = re.compile(r"cite[{](.*?)[}]").search(cword).groups()[0].strip()
        if innards:
            entries = map(lambda x: x.strip(), innards.split(","))
        else:
            entries = []
    else:  # no citation, but put one in
        vim.appendAtWord("~\cite{}", ",", ".")
        cword = "~\cite{}"

    # build the citation
    import os.path

    if not hasattr(PyTexGlobals, "bib"):
        # import PyGui
        # filesel = PyGui.FileSelect(os.path.join(os.path.expanduser("~"), "Documents"), setBibliography, main=True)
        # gtk.main()
        from pylatex.pybib import Bibliography

        empty = Bibliography()
        empty.buildRecords(Bibliography.ENDNOTE_XML_LIB)
        setattr(PyTexGlobals, "bib", empty)

    bib = getattr(PyTexGlobals, "bib")
    title = "Reference at line %d" % vim.PyVimGlobals.line
    # title = "test"
    citeobj = Citation(title, bib, entries)

    gtk.gdk.threads_init()
    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave()

    # use the citation object to generate the new reference
    entries = citeobj.getEntries()
    labels = []
    for entry in entries:
        labels.append(str(entry.getAttribute("label")))

    newtext = ""
    if labels:
        newtext = "~\cite{%s}" % ",".join(labels)

    vim.replace(cword, newtext)