#!/usr/bin/python
"""
A small script to post-process the Bibtex output of Mendeley. Mendeley does 
some stupid things in its output (in particular, double-bracketing all 
titles) and outputs a lot of fields that you wouldn't actually want 
in your bibliography. This script fixes the problems and outputs some 
cleaner Bibtex.

Requires Pybtex.

"""

import sys
from pybtex.database.input.bibtex import Parser
from pybtex.database.output.bibtex import Writer
from io import StringIO
from optparse import OptionParser

usage = "mendmend.py [<options>] <bibfile.bib>"
description = "Fix the Bibtex output from Mendeley"
optparser = OptionParser(usage=usage, description=description)
options, arguments = optparser.parse_args()

# Could make this an option
REMOVE_FIELDS = [
    'mendeley-tags',
    'keywords',
    'abstract',
    'file', 
    'annote',
    'doi',
]

if len(arguments) == 0:
    print "Specify a bibtext file"
    sys.exit(1)
filename = arguments[0]

bib_parser = Parser()
# Read in the data
with open(filename) as f:
    bstr = f.read()
# Assume the input is utf8 encoded
bstr = bstr.decode('utf8')

# Parse the bibtex file
bib_data = bib_parser.parse_stream(StringIO(bstr))

for (key,entry) in bib_data.entries.items():
    # Look for the title field to fix Mendeley's double-bracketing
    if 'title' in entry.fields:
        title = entry.fields['title']
        if title.startswith("{") and title.endswith("}"):
            entry.fields['title'] = title[1:-1]
    # Remove any fields Mendeley outputs that we don't want
    for field_name in REMOVE_FIELDS:
        if field_name in entry.fields:
            del entry.fields[field_name]
    # Get rid of URLs if there's a publisher or journal
    if ('publisher' in entry.fields or 'journal' in entry.fields or 
            'school' in entry.fields or 'booktitle' in entry.fields) \
            and 'url' in entry.fields:
        del entry.fields['url']

# Output the resulting Bibtex
writer = Writer()
strm = StringIO()
writer.write_stream(bib_data, strm)
print strm.getvalue().encode('utf8')
