Skip to content

Commit

Permalink
Expand bibtex functionality to provide suggested citation string
Browse files Browse the repository at this point in the history
  • Loading branch information
kthoden committed Jun 24, 2021
1 parent 840ae6d commit ae7db17
Show file tree
Hide file tree
Showing 2 changed files with 405 additions and 7 deletions.
56 changes: 49 additions & 7 deletions build_frontmatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,11 +1127,16 @@ def write_json_file(json_object, filename):
# def write_json_file ends here


def create_bibtex(eoa_publication, eoaclassic, eoa_cursor, server_data):
def create_bibtex(eoa_publication, eoaclassic, eoa_cursor, server_data, out_dir):
"""Use the database infos to create BibTeX files"""

import html
import shlex
import subprocess
from bibtexparser.bwriter import BibTexWriter
from bibtexparser.bibdatabase import BibDatabase
from lxml import html as lxhtml
from lxml.html import clean

db = BibDatabase()

Expand Down Expand Up @@ -1161,15 +1166,22 @@ def create_bibtex(eoa_publication, eoaclassic, eoa_cursor, server_data):

db.entries.append(main_item)

nocitestring = f"---\ntitle: Citations\nnocite: |\n @{eoa_publication}"

chapter_files = get_chapters(eoa_pub_id, eoa_cursor, eoaclassic, include_pdfless=True)
chaptercounter = 1
for chapter in chapter_files:
chap_item = {}
chapter_creators = format_authors(chapter, "chapter")[1]
authors_concat = " and ".join(chapter_creators)

if len(chapter_creators) == 0:
authors_concat = " and ".join(creators)
else:
authors_concat = " and ".join(chapter_creators)
chap_item["date"] = item_for_template["pubdate"]
chap_item["title"] = format_title(chapter[1].rstrip())
chap_item["booktitle"] = f"{item_for_template['booktitle']}{format_title(eoa_publication_info['Subtitle'], is_book_subtitle=True, unformatted=True)[1:]}"
chap_item["location"] = "Berlin"
chap_item["title"] = "{{" + format_title(chapter[1].rstrip()) + "}}"
chap_item["booktitle"] = "{{" + f"{item_for_template['booktitle']}{format_title(eoa_publication_info['Subtitle'], is_book_subtitle=True, unformatted=True)[1:]}" + "}}"
chap_item["url"] = f"{item_for_template['url']}{chapter[0]}/index.html"
# chap_item["series"] = item_for_template["series"]
# chap_item["number"] = str(item_for_template["number"])
Expand All @@ -1179,15 +1191,45 @@ def create_bibtex(eoa_publication, eoaclassic, eoa_cursor, server_data):
chap_item["langid"] = chapter[9]
chap_item["doi"] = chapter[8]
# chap_item["crossref"] = eoa_publication
chap_item["ID"] = f"{eoa_publication}_chap{chaptercounter:02}"
citekey = f"{eoa_publication}_chap{chaptercounter:02}"
chap_item["ID"] = citekey
chap_item["ENTRYTYPE"] = "incollection"
chaptercounter += 1

db.entries.append(chap_item)

nocitestring += f", @{citekey}"

nocitestring += "\n---\n"

writer = BibTexWriter()
with open(f'./generated_files/{eoa_publication}.bib', 'w') as bibfile:
bib_filename = str(out_dir) + f"/{eoa_publication}.bib"
with open(bib_filename, 'w') as bibfile:
bibfile.write(writer.write(db))
logging.info("Wrote %s", bib_filename)

md_filename = str(out_dir) + f"/{eoa_publication}-citations.md"
with open(md_filename, 'w') as mdfile:
mdfile.write(nocitestring)
logging.info("Wrote %s", md_filename)

pandoc_command = f"pandoc -s -o tmp.html -t html --citeproc --csl=data/eoasc.csl --bibliography={bib_filename} {md_filename}"
pandoc_command_arguments = shlex.split(pandoc_command)
pandoc_result = subprocess.check_output(pandoc_command_arguments)
pandoc_html = lxhtml.parse("tmp.html").getroot()
entries = pandoc_html.find_class("csl-entry")

md_filename = str(out_dir) + f"/{eoa_publication}.md"
mdfile = open(md_filename, 'w')
for entry in entries:
citekey = entry.get('id')[4:]
cc = clean.Cleaner(remove_tags=["span", "div"])
clean_string = cc.clean_html(entry)
citestring = lxhtml.tostring(clean_string)[5:-7].decode('utf-8')
mdfile.write(f"{citekey}: {html.unescape(citestring)} \n")
mdfile.close
logging.info("Wrote %s", md_filename)
os.unlink("tmp.html")
# def create_bibtex ends here


Expand Down Expand Up @@ -1294,7 +1336,7 @@ def main():
elif args.omp:
create_omp_native_xml(args.publication, args.classic, db_cursor, server_data, out_dir)
elif args.bibtex:
create_bibtex(args.publication, args.classic, db_cursor, server_data)
create_bibtex(args.publication, args.classic, db_cursor, server_data, out_dir)
elif args.xhtml:
create_xhtml(args.publication, args.edited, args.classic, db_cursor, server_data, out_dir)
else:
Expand Down
Loading

0 comments on commit ae7db17

Please sign in to comment.