Example #1
0
    def load_manual_references(self, *args, **kwargs):
        """
        Load manual references
        """
        from manubot.process.bibliography import load_manual_references

        manual_refs = load_manual_references(*args, **kwargs)
        self.manual_refs.update(manual_refs)
Example #2
0
def generate_csl_items(args, citekeys_df):
    """
    General CSL (citeproc) items for standard_citekeys in citekeys_df.
    Writes references.json to disk and logs warnings for potential problems.
    """
    # Read manual references (overrides) in JSON CSL
    manual_refs = load_manual_references(args.manual_references_paths)

    requests_cache.install_cache(args.requests_cache_path,
                                 include_get_headers=True)
    cache = requests_cache.get_cache()
    if args.clear_requests_cache:
        logging.info('Clearing requests-cache')
        requests_cache.clear()
    logging.info(
        f'requests-cache starting with {len(cache.responses)} cached responses'
    )

    csl_items = list()
    failures = list()
    for standard_citekey in citekeys_df.standard_citekey.unique():
        if standard_citekey in manual_refs:
            csl_items.append(manual_refs[standard_citekey])
            continue
        elif standard_citekey.startswith('raw:'):
            logging.error(
                f'CSL JSON Data with a standard_citekey of {standard_citekey!r} not found in manual-references.json. '
                'Metadata must be provided for raw citekeys.')
            failures.append(standard_citekey)
        try:
            csl_item = citekey_to_csl_item(standard_citekey)
            csl_items.append(csl_item)
        except Exception:
            logging.exception(
                f'Citeproc retrieval failure for {standard_citekey!r}')
            failures.append(standard_citekey)

    logging.info(
        f'requests-cache finished with {len(cache.responses)} cached responses'
    )
    requests_cache.uninstall_cache()

    if failures:
        message = 'CSL JSON Data retrieval failed for the following standardized citation keys:\n{}'.format(
            '\n'.join(failures))
        logging.error(message)

    # Write JSON CSL bibliography for Pandoc.
    with args.references_path.open('w', encoding='utf-8') as write_file:
        json.dump(csl_items, write_file, indent=2, ensure_ascii=False)
        write_file.write('\n')
    return csl_items
Example #3
0
def _generate_csl_items(args, citekeys_df):
    """
    General CSL (citeproc) items for standard_citekeys in citekeys_df.
    Writes references.json to disk and logs warnings for potential problems.
    """
    # Read manual references (overrides) in JSON CSL
    manual_refs = load_manual_references(args.manual_references_paths)

    # Retrieve CSL Items
    csl_items = generate_csl_items(
        citekeys=citekeys_df.standard_citekey.unique(),
        manual_refs=manual_refs,
        requests_cache_path=args.requests_cache_path,
        clear_requests_cache=args.clear_requests_cache,
    )

    # Write CSL JSON bibliography for Pandoc.
    write_csl_json(csl_items, args.references_path)
    return csl_items
Example #4
0
def test_load_multiple_bibliography_paths():
    citation_to_csl_item = load_manual_references(bibliography_paths)
    print(list(citation_to_csl_item))

    assert 'doi:10.7554/elife.32822' in citation_to_csl_item
    csl_item_1 = citation_to_csl_item['doi:10.7554/elife.32822']
    assert csl_item_1['title'].startswith('Sci-Hub')
    assert 'CSL JSON Item was loaded by Manubot' in csl_item_1['note']
    assert 'manual_reference_filename: bibliography.json' in csl_item_1['note']
    assert 'standard_id: doi:10.7554/elife.32822' in csl_item_1['note']

    # raw id corresponding to bibliography.bib
    assert 'raw:noauthor_techblog:_nodate' in citation_to_csl_item
    csl_item_2 = citation_to_csl_item['raw:noauthor_techblog:_nodate']
    assert csl_item_2['title'].startswith('TechBlog')
    assert 'manual_reference_filename: bibliography.bib' in csl_item_2['note']
    assert 'original_id: noauthor_techblog:_nodate' in csl_item_2['note']

    # id inferred by pandoc-citeproc during bib2json conversion of .nbib file
    assert 'raw:Beaulieu-Jones2017' in citation_to_csl_item
    csl_item_3 = citation_to_csl_item['raw:Beaulieu-Jones2017']
    assert csl_item_3['author'][0]['family'] == 'Beaulieu-Jones'
    assert 'manual_reference_filename: bibliography.nbib' in csl_item_3['note']
    assert 'original_id: Beaulieu-Jones2017' in csl_item_3['note']
Example #5
0
 def setup_method(self):
     bibliography_paths = sorted(
         directory / x
         for x in directory.glob("bibliographies/bibliography.*"))
     self.citation_to_csl_item = load_manual_references(bibliography_paths)
     print(list(self.citation_to_csl_item))
Example #6
0
 def setup_method(self):
     self.citation_to_csl_item = load_manual_references(bibliography_paths)
     print(list(self.citation_to_csl_item))