def to_snippet(self, style): if style == '': self.errors = 'No style specified' return None try: style_file = os.path.join(style_folder, style) bib_style = citeproc.CitationStylesStyle(style=style_file, validate=False) except AttributeError: self.errors = 'The style "{style}" is not available'.format( style=style) return None except lxml.etree.XMLSyntaxError: self.errors = 'The style "{style}" could not be processed'.format( style=style) return None bib_source = citeproc.source.BibliographySource() bib_source.add(self) bibliography = citeproc.CitationStylesBibliography( bib_style, bib_source, citeproc.formatter.plain) citation = citeproc.Citation([citeproc.CitationItem('refkey')]) bibliography.register(citation) if len(bibliography.bibliography()) > 0: snippet = str(bibliography.bibliography()[0]) else: snippet = None return snippet
def preprocess(self, nb, resources): """ Preprocessing to apply on each notebook. Must return modified nb, resources. If you wish to apply your preprocessing to each cell, you might want to override preprocess_cell method instead. Parameters ---------- nb : NotebookNode Notebook being converted resources : dictionary Additional resources used in the conversion process. Allows preprocessors to pass variables into the Jinja engine. """ if not "cite2c" in nb["metadata"]: print("Did not find cite2c metadata") return nb, resources if not "citations" in nb["metadata"]["cite2c"]: print("Did not find cite2c metadata") return nb, resources if "html" in resources["output_extension"]: newrefs = [] for key, value in nb["metadata"]["cite2c"]["citations"].items(): temp = value temp["id"] = key newrefs.append(temp) bib_source = citeproc.source.json.CiteProcJSON(newrefs) bib_style = citeproc.CitationStylesStyle("harvard1", validate=False) self.bibliography = citeproc.CitationStylesBibliography( bib_style, bib_source, citeproc.formatter.html ) if "tex" in resources["output_extension"]: self.references = nb["metadata"]["cite2c"]["citations"] self.create_bibfile( resources, resources["output_files_dir"] + "/" + resources["unique_key"] + ".bib", ) for index, cell in enumerate(nb.cells): nb.cells[index], resources = self.preprocess_cell(cell, resources, index) return nb, resources
def open_bibliography(db_type='mendeley', db=None, db_encoding='utf-8', style='harvard1', formatter_name=None): ''' :param str db_type: Bibliography database type. :param str db: Bibliography database. :param str db_encoding: Bibliography database encoding. :param str style: CSL style to use. ''' # Import the formatter if formatter_name is None: print('error: non formatter specified', file=sys.stderr) return None formatter = importlib.import_module('bibgen.formatter.' + formatter_name) # Select a default bibliography database if db is None and db_type == 'mendeley': db = bibgen.default_mendeley_database() # Setup a citeproc context bib_style = cp.CitationStylesStyle(style) if db_type == 'mendeley': from bibgen.citeproc.mendeley import CiteProcMendeley bib_src = CiteProcMendeley(db) elif db_type == 'json': json_data = json.load(open(db)) bib_src = citeproc.source.json.CiteProcJSON(json_data) # Default to BibTex else: # Check for bibtexparser try: import bibtexparser with codecs.open(db, 'r', db_encoding) as fp: from bibgen.citeproc.bibtexparser import CiteProcBibTeX bib_src = CiteProcBibTeX(fp) # Use minimalistic bibtex parser from citeproc-py except ImportError: print( 'warning: defaulting to citeproc-py’s bibtex parser, you may want to install bibtexparser', file=sys.stderr) with codecs.open(db, 'r', db_encoding) as fp: import citeproc bib_src = citeproc.source.bibtex.BibTeX(fp) biblio = cp.CitationStylesBibliography(bib_style, bib_src, formatter) return biblio
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 add_publications(generator): """ Populates context with a list of BibTeX publications. Configuration ------------- generator.settings['PUBLICATIONS_SRC']: local path to the BibTeX file to read. Output ------ generator.context['publications']: List of tuples (key, entry, record, external). See Readme.md for more details. """ if "PUBLICATIONS_SRC" not in generator.settings: return if "PUBLICATIONS_STYLE" not in generator.settings: return refs_file = generator.settings["PUBLICATIONS_SRC"] refs_style = generator.settings["PUBLICATIONS_STYLE"] refs_sort = generator.settings.get("PUBLICATIONS_SORT", "date") supress_warning = generator.settings.get( "PUBLICATIONS_SUPRESS_BIBTEX_WARNING", True) with open(refs_file, "r") as fp: bibtex_data = bibtexparser.load(fp) try: entries = sort_entries(bibtex_data.entries, refs_sort) except ValueError as e: logger.error("%s", e) return unknown_bib_field = "Unsupported BibTeX field" with warnings.catch_warnings(record=True) as w_list: citeproc_data = citeproc.source.bibtex.BibTeX(refs_file) for w in w_list: if supress_warning and unknown_bib_field in str(w.message): logger.info("Warning in citeproc-py '%s'", w.message) else: logger.warning("Warning in citeproc-py '%s'", w.message) citeproc_style = citeproc.CitationStylesStyle(refs_style, validate=False) if not citeproc_style.has_bibliography(): logger.warning("Style '%s' does not include a bibliography", refs_style) return bibliography = citeproc.CitationStylesBibliography(citeproc_style, citeproc_data, citeproc.formatter.html) cite = {} for record in entries: key = record["ID"].lower() cite["key"] = citeproc.Citation([citeproc.CitationItem(key)]) bibliography.register(cite["key"]) for key in bibliography.keys: bibliography.cite(cite["key"], cite_warn) bibitems = {} for key, item in zip(bibliography.keys, bibliography.bibliography()): bibitems[key] = str(item) publications = [] for record in entries: key = record["ID"].lower() entry = bibitems[key] external = {} if "eprinttype" in record and "eprint" in record: if record["eprinttype"] == "arxiv": if "eprintclass" in record: external[ "ARXIV"] = "http://arxiv.org/abs/{eprintclass}/{eprint}".format( **record) else: external["ARXIV"] = "http://arxiv.org/abs/{eprint}".format( **record) if "doi" in record and record["doi"] not in entry: external["DOI"] = record["doi"] publications.append((key, entry, record, external)) generator.context["publications"] = publications
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)