From ae7db177ba5d62bc97c1f720ad47d24e6439abce Mon Sep 17 00:00:00 2001 From: kthoden Date: Thu, 24 Jun 2021 20:17:19 +0200 Subject: [PATCH] Expand bibtex functionality to provide suggested citation string --- build_frontmatter.py | 56 ++++++- data/eoasc.csl | 356 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 405 insertions(+), 7 deletions(-) create mode 100644 data/eoasc.csl diff --git a/build_frontmatter.py b/build_frontmatter.py index 23dec63..ae78438 100644 --- a/build_frontmatter.py +++ b/build_frontmatter.py @@ -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() @@ -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"]) @@ -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 @@ -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: diff --git a/data/eoasc.csl b/data/eoasc.csl new file mode 100644 index 0000000..69a1405 --- /dev/null +++ b/data/eoasc.csl @@ -0,0 +1,356 @@ + +