def format_bibtex(bibtex_entry, style="harvard1"): key = bibtex_entry.get_key() # need to save it temporarily to use citeproc-py fname = tempfile.mktemp(suffix=".bib") try: with open(fname, "wt") as f: bibtex = bibtex_entry.rawentry bibtex = bibtex.replace(u"\u2013", "--") + "\n" # TODO: manage to save/use UTF-8 if PY2: bibtex = bibtex.encode("ascii", "ignore") f.write(bibtex) # We need to avoid cpBibTex spitting out warnings old_filters = warnings.filters[:] # store a copy of filters warnings.simplefilter("ignore", UserWarning) try: bib_source = cpBibTeX(fname) except Exception as e: lgr.error("Failed to process BibTeX file %s" % fname) return "ERRORED: %s" % str(e) finally: # return warnings back warnings.filters = old_filters bib_style = cp.CitationStylesStyle(style, validate=False) # TODO: specify which tags of formatter we want bibliography = cp.CitationStylesBibliography(bib_style, bib_source, cp.formatter.plain) citation = cp.Citation([cp.CitationItem(key)]) bibliography.register(citation) finally: os.unlink(fname) biblio_out = bibliography.bibliography() assert len(biblio_out) == 1 biblio_out = "".join(biblio_out[0]) return biblio_out # if biblio_out else str(bibtex_entry)
def format_bibtex(bibtex_entry, style='harvard1'): try: from citeproc.source.bibtex import BibTeX as cpBibTeX import citeproc as cp except ImportError as e: raise RuntimeError( "For formatted output we need citeproc and all of its dependencies " "(such as lxml) but there is a problem while importing citeproc: %s" % str(e)) key = bibtex_entry.get_key() # need to save it temporarily to use citeproc-py fname = tempfile.mktemp(suffix='.bib') try: with open(fname, 'wt') as f: bibtex = bibtex_entry.rawentry bibtex = bibtex.replace(u'\u2013', '--') + "\n" # TODO: manage to save/use UTF-8 if PY2: bibtex = bibtex.encode('ascii', 'ignore') f.write(bibtex) # We need to avoid cpBibTex spitting out warnings old_filters = warnings.filters[:] # store a copy of filters warnings.simplefilter('ignore', UserWarning) try: bib_source = cpBibTeX(fname) except Exception as e: lgr.error("Failed to process BibTeX file %s: %s" % (fname, e)) return "ERRORED: %s" % str(e) finally: # return warnings back warnings.filters = old_filters bib_style = cp.CitationStylesStyle(style, validate=False) # TODO: specify which tags of formatter we want bibliography = cp.CitationStylesBibliography(bib_style, bib_source, cp.formatter.plain) citation = cp.Citation([cp.CitationItem(key)]) bibliography.register(citation) finally: if not os.environ.get("DUECREDIT_KEEPTEMP"): os.unlink(fname) biblio_out = bibliography.bibliography() assert(len(biblio_out) == 1) biblio_out = ''.join(biblio_out[0]) return biblio_out # if biblio_out else str(bibtex_entry)
def format_bibtex(bibtex_entry, style='harvard1'): try: from citeproc.source.bibtex import BibTeX as cpBibTeX import citeproc as cp except ImportError as e: raise RuntimeError( "For formatted output we need citeproc and all of its dependencies " "(such as lxml) but there is a problem while importing citeproc: %s" % str(e)) decode_exceptions = UnicodeDecodeError try: from citeproc.source.bibtex.bibparse import BibTeXDecodeError decode_exceptions = (decode_exceptions, BibTeXDecodeError) except ImportError: # this version doesn't yet have this exception defined pass key = bibtex_entry.get_key() # need to save it temporarily to use citeproc-py fname = tempfile.mktemp(suffix='.bib') try: with open(fname, 'wb') as f: f.write(condition_bibtex(bibtex_entry.rawentry)) # We need to avoid cpBibTex spitting out warnings old_filters = warnings.filters[:] # store a copy of filters warnings.simplefilter('ignore', UserWarning) try: try: bib_source = cpBibTeX(fname) except decode_exceptions as e: # So .bib must be having UTF-8 characters. With # a recent (not yet released past v0.3.0-68-g9800dad # we should be able to provide encoding argument bib_source = cpBibTeX(fname, encoding='utf-8') except Exception as e: lgr.error("Failed to process BibTeX file %s: %s" % (fname, e)) return "ERRORED: %s" % str(e) finally: # return warnings back warnings.filters = old_filters bib_style = cp.CitationStylesStyle(style, validate=False) # TODO: specify which tags of formatter we want bibliography = cp.CitationStylesBibliography(bib_style, bib_source, cp.formatter.plain) citation = cp.Citation([cp.CitationItem(key)]) bibliography.register(citation) finally: if not os.environ.get("DUECREDIT_KEEPTEMP"): exceptions = (OSError, WindowsError) if on_windows else OSError for i in range(50): try: os.unlink(fname) except exceptions: if i < 49: sleep(0.1) continue else: raise break biblio_out = bibliography.bibliography() assert (len(biblio_out) == 1) biblio_out = ''.join(biblio_out[0]) return biblio_out # if biblio_out else str(bibtex_entry)
def format_bibtex(bibtex_entry, style='harvard1'): try: from citeproc.source.bibtex import BibTeX as cpBibTeX import citeproc as cp except ImportError as e: raise RuntimeError( "For formatted output we need citeproc and all of its dependencies " "(such as lxml) but there is a problem while importing citeproc: %s" % str(e)) decode_exceptions = UnicodeDecodeError try: from citeproc.source.bibtex.bibparse import BibTeXDecodeError decode_exceptions = (decode_exceptions, BibTeXDecodeError) except ImportError: # this version doesn't yet have this exception defined pass key = bibtex_entry.get_key() # need to save it temporarily to use citeproc-py fname = tempfile.mktemp(suffix='.bib') try: with open(fname, 'wb') as f: f.write(condition_bibtex(bibtex_entry.rawentry)) # We need to avoid cpBibTex spitting out warnings old_filters = warnings.filters[:] # store a copy of filters warnings.simplefilter('ignore', UserWarning) try: try: bib_source = cpBibTeX(fname) except decode_exceptions as e: # So .bib must be having UTF-8 characters. With # a recent (not yet released past v0.3.0-68-g9800dad # we should be able to provide encoding argument bib_source = cpBibTeX(fname, encoding='utf-8') except Exception as e: msg = "Failed to process BibTeX file %s: %s." % (fname, e) citeproc_version = external_versions['citeproc'] if 'unexpected keyword argument' in str(e) and \ citeproc_version and citeproc_version < '0.4': err = "need a newer citeproc-py >= 0.4.0" msg += " You might just " + err else: err = str(e) lgr.error(msg) return "ERRORED: %s" % err finally: # return warnings back warnings.filters = old_filters bib_style = cp.CitationStylesStyle(style, validate=False) # TODO: specify which tags of formatter we want bibliography = cp.CitationStylesBibliography(bib_style, bib_source, cp.formatter.plain) citation = cp.Citation([cp.CitationItem(key)]) bibliography.register(citation) finally: if not os.environ.get("DUECREDIT_KEEPTEMP"): exceptions = (OSError, WindowsError) if on_windows else OSError for i in range(50): try: os.unlink(fname) except exceptions: if i < 49: sleep(0.1) continue else: raise break biblio_out = bibliography.bibliography() assert(len(biblio_out) == 1) biblio_out = ''.join(biblio_out[0]) return biblio_out # if biblio_out else str(bibtex_entry)