Created
September 8, 2011 21:22
-
-
Save fmoralesc/1204775 to your computer and use it in GitHub Desktop.
bibtool + regex parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# usage: script.py QUERY BIBFILE | |
import sys | |
import os | |
import re | |
from subprocess import Popen, PIPE | |
title_search = re.compile("\s*[Tt]itle\s*=\s*{(?P<title>\S.*)},\n") | |
booktitle_search = re.compile("\s*[Bb]ooktitle\s*=\s*{(?P<title>\S.*)},\n") | |
author_search = re.compile("\s*[Aa]uthor\s*=\s*{(?P<author>\S.*)},\n") | |
id_search = re.compile(".*{\s*(?P<id>.*),") | |
def parse_bibfile_for_vim(): | |
entries = [] | |
for entry in [i for i in re.split("\n@", text)]: | |
entry_dict = {} | |
i1 = id_search.match(entry) | |
if i1: | |
entry_dict["word"] = i1.group("id") | |
title = "" | |
author = "" | |
# search for title | |
i2 = title_search.search(entry) | |
if i2: | |
title = i2.group("title") | |
else: | |
i3 = booktitle_search.search(entry) | |
if i3: | |
title = i3.group("title") | |
# search for author | |
i4 = author_search.search(entry) | |
if i4: | |
author = i4.group("author") | |
entry_dict["menu"] = " - ".join([author, title]) | |
entries.append(entry_dict) | |
return entries | |
if __name__ == "__main__": | |
args = "-- select{$key title booktitle author editor \"%(query)s\"}'" % {"query": sys.argv[1]} | |
text = Popen(["bibtool", args, sys.argv[2]], stdout=PIPE, stderr=PIPE).communicate()[0] | |
print len(parse_bibfile_for_vim()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment