Skip to content

Commit

Permalink
Introducing pandoc-citeproc for bibliographic rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus Thoden committed Jan 22, 2018
1 parent cd4a016 commit 994c826
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions eoaconvert.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
# -*- coding: utf-8; mode: python -*-
# Time-stamp: <2018-01-22 13:22:15 (kthoden)>
# Time-stamp: <2018-01-22 16:45:16 (kthoden)>

# license?
__version__= "1.0"
Expand All @@ -21,6 +21,7 @@
import re
import string
import shlex
import json
import subprocess
import sys
import shutil
Expand Down Expand Up @@ -261,7 +262,9 @@ def createBibEntryAuthorYear(bibEntry, boolSameAuthor):
if bibEntry.entrytype() == "newspaper":
strBibEntry = strAuthor + " (" + bibEntry.labelyear() + bibEntry.labelyearsuffix() + "). <i>" + bibEntry.title() + "</i>"

return(sanitize_bibentry(strBibEntry))
print(strBibEntry)

return sanitize_bibentry(strBibEntry)
# def createBibEntryAuthorYear ends here

def createBibEntryNumeric(bibEntry):
Expand Down Expand Up @@ -293,7 +296,7 @@ def createBibEntryNumeric(bibEntry):
strBibEntry = strAuthor + " (" + bibEntry.labelyear() + bibEntry.labelyearsuffix() + ") <i>" + bibEntry.title() + "</i>."

return strBibEntry
# def createBibEntryAuthorYear ends here
# def createBibEntryNumeric ends here

def pdf_burst(input_file, tmpDir):
"""Split PDF file into single pages"""
Expand Down Expand Up @@ -893,24 +896,28 @@ def cleanup():
print("-----------------------------------------------------")
print("Working on Page Numbers for References")
listAuxFiles = glob.glob(os.getcwd() + "/*.aux")
for strFile in listAuxFiles:
tmpFile = open(strFile, "r")
lines = tmpFile.readlines()
tmpFile.close()
for line in lines:
# hyperref makes the lines much much longer
# \newlabel{BL}{{1.1}{4}{Forschungsüberblick zur Literatur über Alvarus Thomas}{section.1.1}{}}
# \newlabel{BL}{{1.1}{4}}
matched_label = re.match(r'\\newlabel\{(.*?)\}\{\{(.*?)\}\{(.*?)\}', line)
# matchObjectLabel = re.match(r'\newlabel\{(.*?)\}', line)
if matched_label:
# matchObjectPage = re.match(r'(.*?)\}\{(\d{1,})\}\}$', line)
# if matchObjectPage:
dictPagelabels[matched_label.group(1)] = matched_label.group(3)
# parsing out information on cite works
matched_citation = re.match(r'\\abx@aux@cite{(.*?)}', line)
if matched_citation is not None:
set_citations.add(matched_citation.group(1))
if len(listAuxFiles) == 0:
print("No aux file found. Exiting")
sys.exit(1)
else:
for strFile in listAuxFiles:
tmpFile = open(strFile, "r")
lines = tmpFile.readlines()
tmpFile.close()
for line in lines:
# hyperref makes the lines much much longer
# \newlabel{BL}{{1.1}{4}{Forschungsüberblick zur Literatur über Alvarus Thomas}{section.1.1}{}}
# \newlabel{BL}{{1.1}{4}}
matched_label = re.match(r'\\newlabel\{(.*?)\}\{\{(.*?)\}\{(.*?)\}', line)
# matchObjectLabel = re.match(r'\newlabel\{(.*?)\}', line)
if matched_label:
# matchObjectPage = re.match(r'(.*?)\}\{(\d{1,})\}\}$', line)
# if matchObjectPage:
dictPagelabels[matched_label.group(1)] = matched_label.group(3)
# parsing out information on cite works
matched_citation = re.match(r'\\abx@aux@cite{(.*?)}', line)
if matched_citation is not None:
set_citations.add(matched_citation.group(1))

print(dictPagelabels)
print(set_citations)
Expand Down Expand Up @@ -949,6 +956,20 @@ def cleanup():
HAS_BIBLIOGRAPHY = False
input()

# the new solution: pandoc-citeproc
interim_bib_json_file = (options.filename) + "-bib.json"
citeproc_command = "pandoc-citeproc --bib2json %s" % bib_database + ".bib"
logging.debug(citeproc_command)
citeproc_arguments = shlex.split(citeproc_command)
citeproc_process = subprocess.Popen(citeproc_arguments, stdout=subprocess.PIPE)
citeproc_json = citeproc_process.stdout.read()

jsonObj = json.loads(citeproc_json)

for x in jsonObj:
print(x["title"])

# the old solution
# Copy interim .bbl-File to interim bib.tex file
interim_bibtex_file = (options.filename) + "bib.tex"
try:
Expand Down

0 comments on commit 994c826

Please sign in to comment.