#!/usr/bin/env python3

import bibtexparser
from bibtexparser.bparser import BibTexParser
from bibtexparser.bwriter import BibTexWriter
from bibtexparser.bibdatabase import BibDatabase
from bibtexparser.customization import homogenize_latex_encoding
from bibtexparser.customization import convert_to_unicode

import json
from yaml import dump
from yaml import CDumper as Dumper

import sys

def toString(e):
    writer = BibTexWriter()
    writer.indent = '    '     # indent entries with 4 spaces instead of one
    writer.comma_first = True  # place the comma at the beginning of the line

    return writer._entry_to_bibtex(e)

def parseEntry(s):
    # normalize unicode by reparsing
    parser = BibTexParser()
    parser.customization = convert_to_unicode
    db1 = bibtexparser.loads(s, parser=parser)
    es = db1.entries
    return es[0]

extra = sys.argv[1]
with open(extra) as data_file:
    extra = json.load(data_file)

bib = sys.argv[2]
with open(bib) as bibtex_file:
    parser = BibTexParser()
    parser.customization = homogenize_latex_encoding
    db = bibtexparser.load(bibtex_file, parser=parser)

for e in db.entries:
    if e['ID'] in extra:
        info = extra[e['ID']]
        link = info['file'] if 'file' in info else info['slides']
        e['file'] = link
        if 'slides' in info: e['slides'] = info['slides']

    name = e['ar_shortname']
    filename = name.replace(' ','_').replace('-','_')
    e['ar_file'] = filename
    filename = '_papers/' + filename + '.md'

    s = toString(e)
    # print(s)
    e2 = parseEntry(s)
    e2['bibtex'] = s

    v = e2['title']
    v = v.replace('\n',' ')
    v = v.replace("{", "").replace("}", "")
    # v = v.replace(":", "")
    e['ar_title'] = v

    # print(toString(e2))
    # print(dump(e, Dumper=Dumper))
    with open(filename, 'w') as file:
        file.write("---\n")
        file.write("layout: paper\n")
        for k,v in e2.items():
            if k == 'author': v = v.replace('\n',' ').replace(' and ', ', ')
            if k == 'editor': v = v.replace('\n',' ').replace(' and ', ', ')
            if k == 'abstract' or k == 'bibtex':
                v = v.rstrip().split('\n\n')
                v = '\n<p>\n'.join(v)
                v = v.split('\n')
                v = '|\n    ' + ('\n    '.join(v))
            else:
                v = v.replace('\n',' ')
                v = v.replace("{", "").replace("}", "")
                v = v.replace(":", "")
            file.write(k + ": " + v +"\n")
        file.write("---\n")

print(json.dumps(db.entries, indent=4))

# with open('normalized.bib', 'w') as bibfile:
#     bibfile.write(writer.write(db))

@@ -1670,17 +1670,37 @@
 %22%22)%0A    
-#
+e%5B'ar_title'%5D = v%0A   
  v = v.r
@@ -1708,28 +1708,32 @@
 place(%22:
+ 
 %22, %22
+ - 
 %22)%0A    e
 %5B'ar_tit
@@ -1716,37 +1716,35 @@
  %22, %22 - %22)%0A    e
+2
 %5B'
-ar_
 title'%5D = v%0A%0A   

Tweak handling of colon in paper titles