def init(app, config_file): global metadata config = topicexplorer.config.read(config_file) try: filename = config.get('bibtex', 'path') except ConfigParserError: model_path = config.get('main', 'path') filename = os.path.join(model_path, 'library.bib') print("Loading Bibtex metadata from", filename) bib = parse_file(filename) metadata = dict() for entry in bib.entries: key = '/' + bib.entries[entry].fields.get('file', '').replace( ':pdf', '')[1:] if 'C$\\backslash$:' in key: key = key.replace('C$\\backslash$:', '') key = key[1:] key = os.path.normpath(key) key = os.path.basename(key) try: citation = pybtex.format_from_file(filename, style='plain', output_backend='text', citations=[entry])[3:] metadata[key] = citation except PybtexError: metadata[key] = filename
def init(viewer, config, args): global metadata try: filename = args.bibtex or config.get('bibtex', 'path') except ConfigParser.Error: model_path = config.get('main','path') filename = os.path.join(model_path, 'library.bib') print "Loading Bibtex metadata from", filename bib = parse_file(filename) metadata = dict() for entry in bib.entries: key = '/' + bib.entries[entry].fields.get('file','').replace(':pdf','')[1:] if 'C$\\backslash$:' in key: key = key.replace('C$\\backslash$:', '') key = key[1:] key = os.path.normpath(key) key = os.path.basename(key) try: citation = pybtex.format_from_file( filename, style='plain', output_backend='text', citations=[entry])[3:] metadata[key] = citation except PybtexError: metadata[key] = filename
def print(self, filename=None, output_format="text"): """Print all citations that were used for running simulations. Parameters ---------- filename : str, optional Filename to which to print citations. If None, citations are printed to the terminal. """ citations = "" citations_file = os.path.join(pybamm.root_dir(), "pybamm", "CITATIONS.txt") if output_format == "text": citations = pybtex.format_from_file( citations_file, "plain", citations=self._papers_to_cite, output_backend="plaintext", ) elif output_format == "bibtex": for key in self._papers_to_cite: citations += self._all_citations[key] + "\n" else: raise pybamm.OptionError( "Output format {} not recognised." "It should be 'text' or 'bibtex'.".format(output_format)) if filename is None: print(citations) else: with open(filename, "w") as f: f.write(citations)
def generate_ps_doc(parameter_set_dict): """ Generates docstring of parameter_sets.py from the given dictionary """ output_list = [DOC_INTRO] citations_file = os.path.join(pybamm.root_dir(), "pybamm", "CITATIONS.txt") for ps_chemistry in sorted(parameter_set_dict.keys()): output_list.append("") ps_citations = parameter_set_dict[ps_chemistry] chem_name = ps_chemistry.capitalize().replace("_", "-") + " " + "parameter sets" output_list.append(chem_name) dashes = "-" * len(ps_chemistry) + "-" * 15 output_list.append(dashes) for ps_name, ps_citation in sorted(ps_citations): citations = pybtex.format_from_file( citations_file, style="plain", output_backend="plaintext", citations=ps_citation, nocite=True, ) # Remove citation labels "[3]" citations = re.split(r"(?:^|\n)\[\d+\]\s", citations) # Remove empty strings citations = filter(bool, citations) fmt_citations = [] for citation in citations: # Break line at the first space before 80 characters citation_parts = re.findall(r"(.{1,79})(?:\s|$)", citation) # first_line = citation.split('\n') indent_citation_parts = [] for idx, citation_part in enumerate(citation_parts): if idx == 0: citation_part = "- " + citation_part else: citation_part = " " + citation_part indent_citation_parts.append(" " * 7 + citation_part) # Join to create a single citation paragraph citation = "\n".join(indent_citation_parts) fmt_citations.append(citation) fmt_citations = "\n".join(fmt_citations) ps_doc = f" * {ps_name:} :\n{fmt_citations}" output_list.append(ps_doc) output = "\n".join(output_list) output += "\n" return output
from pybtex import format_from_file with open('references.bib') as bib_file: s_formatted = format_from_file( bib_file, style='unsrt', abbreviate_names=True, output_backend='text', capfirst=False, ) print(s_formatted.replace('Csadorf', 'csadorf'))
class YearSortingStyle(SortingStyle): def sorting_key(self, entry): if entry.type in ('book', 'inbook'): author_key = self.author_editor_key(entry) elif 'author' in entry.persons: author_key = self.persons_key(entry.persons['author']) else: author_key = '' return (-int(entry.fields.get('year', '')), author_key, entry.fields.get('title', '')) register_plugin('pybtex.style.sorting', 'year_author_title', YearSortingStyle) markdown = pybtex.format_from_file(sys.argv[1], style='plain', output_backend='markdown', sorting_style='year_author_title', abbreviate_names=True) markdown = re.sub('(\[\d+\])', '\n\\1', markdown) # markdown, _ = re.subn(r'\\textbf \\underline N\\., Sonnenschein', '**<u>N., Sonnenschein</u>**', markdown) markdown, _ = re.subn(r'\\\\textbf.*underline.*Sonnenschein', '**<u>N. Sonnenschein</u>**', markdown) markdown, _ = re.subn(r'\\\\textbf.*Sonnenschein', '**N. Sonnenschein**', markdown) markdown, _ = re.subn(r'\\\\emph\s*', '', markdown) print(markdown) # print(re.sub(r'\\textbf \\underline N\., Sonnenschein\.', '!@#$', markdown))