示例#1
0
def get_json_suggestions(text, query):
    import json

    entries = []
    string_matches = [u'title', u'id']
    name_matches = [u'author', u'editor']

    try:
        data = json.loads(text)
    except:
        return entries

    if type(data) != list: return entries

    def check(string):
        return re.search(query, string, re.IGNORECASE)

    def test_entry(entry):
        if type(entry) != dict: return False
        for string in [entry.get(k) for k in string_matches]:
            if type(string) == unicode and check(string): return True
        for names in [entry.get(k) for k in name_matches]:
            if type(names) == list:
                for person in names:
                    if type(person.get(u'family')) == unicode:
                        if check(person[u'family']): return True
                    elif type(person.get(u'literal')) == unicode:
                        if check(person[u'literal']): return True

    for entry in filter(test_entry, data):
        entries.append({"word": entry.get('id'),
                        "menu": make_title_ascii(entry.get("title", "No Title"))
                        })

    return entries
示例#2
0
    def parse_suggestions(self, data):
        """
        Turns the output of the collators get_suggestions() methods into a dict
        like what vim completion functions use.

        """
        vim_suggestions = []
        for item in data:
            item_base = {"word": item['id'], "menu": make_title_ascii(item['title'])}
            if bool(int(vim.eval('g:pandoc#completion#bib#use_preview'))):
                item_base['info'] = dict_to_info(item)
            vim_suggestions.append(item_base)
        return vim_suggestions
示例#3
0
def get_bibtex_suggestions(text, query, use_bibtool=False, bib=None):
    global bibtex_title_search
    global bibtex_booktitle_search
    global bibtex_author_search
    global bibtex_editor_search
    global bibtex_crossref_search

    entries = []

    if use_bibtool:
        bibtex_id_search = re.compile(".*{\s*(?P<id>.*),")

        extra_args = shlex.split(vim.vars["pandoc#biblio#bibtool_extra_args"])

        search = [
            "--",
            "select{$key title booktitle author editor \"%(query)s\"}" % {
                "query": query
            }
        ]
        cmd = ["bibtool", "--preserve.key.case=on", *extra_args, *search, bib]
        text = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
        if isinstance(text, bytes):
            text = text.decode("utf-8")
    else:
        bibtex_id_search = re.compile(".*{\s*(?P<id>" + query + ".*),")

    for entry in [i for i in re.split("\n@", text)]:
        entry_dict = {}
        i1 = bibtex_id_search.match(entry)
        if i1:
            entry_dict["word"] = i1.group("id")

            title = "..."
            # search for title
            i2 = bibtex_title_search.search(entry)
            if i2:
                title = i2.group("title")
            else:
                i3 = bibtex_booktitle_search.search(entry)
                if i3:
                    title = i3.group("booktitle")
            title = re.sub("[{}]", "", re.sub("\s+", " ", title))

            entry_dict["menu"] = make_title_ascii(title)

            entries.append(entry_dict)

    return entries
示例#4
0
    def parse_suggestions(self, data):
        """
        Turns the output of the collators get_suggestions() methods into a dict
        like what vim completion functions use.

        """
        vim_suggestions = []
        for item in data:
            item_base = {
                "word": item['id'],
                "menu": make_title_ascii(item['title'])
            }
            if bool(int(vim.eval('g:pandoc#completion#bib#use_preview'))):
                item_base['info'] = dict_to_info(item)
            vim_suggestions.append(item_base)
        return vim_suggestions
示例#5
0
def get_json_suggestions(text, query):
    import json

    entries = []

    if sys.version_info.major == 2:
        string_matches = [u'title', u'id']
        name_matches = [u'author', u'editor']
        family_match = u'family'
        literal_match = u'literal'
    else:
        string_matches = ['title', 'id']
        name_matches = ['author', 'editor']
        family_match = 'family'
        literal_match = 'literal'

    try:
        data = json.loads(text)
    except:
        return entries

    if type(data) != list: return entries

    def check(string):
        return re.search(query, string, re.IGNORECASE)

    def test_entry(entry):
        if type(entry) != dict: return False
        for string in [entry.get(k) for k in string_matches]:
            if type(string) == str and check(string): return True
        for names in [entry.get(k) for k in name_matches]:
            if type(names) == list:
                for person in names:
                    if type(person.get(family_match)) == str:
                        if check(person[family_match]): return True
                    elif type(person.get(literal_match)) == str:
                        if check(person[literal_match]): return True

    for entry in filter(test_entry, data):
        entries.append({
            "word": entry.get('id'),
            "menu": make_title_ascii(entry.get("title", "No Title"))
        })

    return entries
示例#6
0
def get_bibtex_suggestions(text, query, use_bibtool=False, bib=None):
    global bibtex_title_search
    global bibtex_booktitle_search
    global bibtex_author_search
    global bibtex_editor_search
    global bibtex_crossref_search

    entries = []

    if use_bibtool:
        bibtex_id_search = re.compile(".*{\s*(?P<id>.*),")

        args = "-- select{$key title booktitle author editor \"%(query)s\"}'" % {
            "query": query
        }
        text = sp.Popen(["bibtool", args, bib], stdout=sp.PIPE,
                        stderr=sp.PIPE).communicate()[0]
    else:
        bibtex_id_search = re.compile(".*{\s*(?P<id>" + query + ".*),")

    for entry in [i for i in re.split("\n@", text)]:
        entry_dict = {}
        i1 = bibtex_id_search.match(entry)
        if i1:
            entry_dict["word"] = i1.group("id")

            title = "..."
            # search for title
            i2 = bibtex_title_search.search(entry)
            if i2:
                title = i2.group("title")
            else:
                i3 = bibtex_booktitle_search.search(entry)
                if i3:
                    title = i3.group("booktitle")
            title = re.sub("[{}]", "", re.sub("\s+", " ", title))

            entry_dict["menu"] = make_title_ascii(title)

            entries.append(entry_dict)

    return entries
示例#7
0
def get_bibtex_suggestions(text, query, use_bibtool=False, bib=None):
    global bibtex_title_search
    global bibtex_booktitle_search
    global bibtex_author_search
    global bibtex_editor_search
    global bibtex_crossref_search

    entries = []

    if use_bibtool:
        bibtex_id_search = re.compile(".*{\s*(?P<id>.*),")

        args = "-- select{$key title booktitle author editor \"%(query)s\"}'" % {"query": query}
        text = sp.Popen(["bibtool", "--preserve.key.case=on",  args, bib], stdout=sp.PIPE, stderr=sp.PIPE).communicate()[0]
        if isinstance(text, bytes):
            text = text.decode("utf-8")
    else:
        bibtex_id_search = re.compile(".*{\s*(?P<id>" + query + ".*),")

    for entry in [i for i in re.split("\n@", text)]:
        entry_dict = {}
        i1 = bibtex_id_search.match(entry)
        if i1:
            entry_dict["word"] = i1.group("id")

            title = "..."
            # search for title
            i2 = bibtex_title_search.search(entry)
            if i2:
                title = i2.group("title")
            else:
                i3 = bibtex_booktitle_search.search(entry)
                if i3:
                    title = i3.group("booktitle")
            title = re.sub("[{}]", "", re.sub("\s+", " ", title))

            entry_dict["menu"] = make_title_ascii(title)

            entries.append(entry_dict)

    return entries
示例#8
0
def get_ris_suggestions(text, query):
    global ris_title_search
    global ris_author_search

    entries = []

    ris_id_search = re.compile("^ID\s*-\s*(?P<id>" +  query + ".*)\n", re.MULTILINE)

    for entry in re.split("ER\s*-\s*\n", text):
        entry_dict = {}
        i1 = ris_id_search.search(entry)
        if i1:
            entry_dict["word"] = i1.group("id")
            title = "..."
            i2 = ris_title_search.search(entry)
            if i2:
                title = i2.group("title")

            entry_dict["menu"] = make_title_ascii(title)
            entries.append(entry_dict)

    return entries
示例#9
0
def get_ris_suggestions(text, query):
    global ris_title_search
    global ris_author_search

    entries = []

    ris_id_search = re.compile("^ID\s*-\s*(?P<id>" + query + ".*)\n",
                               re.MULTILINE)

    for entry in re.split("ER\s*-\s*\n", text):
        entry_dict = {}
        i1 = ris_id_search.search(entry)
        if i1:
            entry_dict["word"] = i1.group("id")
            title = "..."
            i2 = ris_title_search.search(entry)
            if i2:
                title = i2.group("title")

            entry_dict["menu"] = make_title_ascii(title)
            entries.append(entry_dict)

    return entries
示例#10
0
def get_json_suggestions(text, query):
    import json

    entries = []
    string_matches = [u'title', u'id']
    name_matches = [u'author', u'editor']

    try:
        data = json.loads(text)
    except:
        return entries

    if type(data) != list: return entries

    def check(string):
        return re.search(query, string, re.IGNORECASE)

    def test_entry(entry):
        if type(entry) != dict: return False
        for string in [entry.get(k) for k in string_matches]:
            if type(string) == unicode and check(string): return True
        for names in [entry.get(k) for k in name_matches]:
            if type(names) == list:
                for person in names:
                    if type(person.get(u'family')) == unicode:
                        if check(person[u'family']): return True
                    elif type(person.get(u'literal')) == unicode:
                        if check(person[u'literal']): return True

    for entry in filter(test_entry, data):
        entries.append({
            "word": entry.get('id'),
            "menu": make_title_ascii(entry.get("title", "No Title"))
        })

    return entries