コード例 #1
0
def load_manual_references(paths=[], extra_csl_items=[]) -> dict:
    """
    Read manual references from bibliography text files specified by a list of paths.
    Returns a standard_citation to CSL Item dictionary.
    `extra_csl_items` specifies CSL Items stored as a Python object,
    to be used in addition to the CSL Items stored as text in the files specified by `paths`.
    Set `paths=[]` to only use extra_csl_items.
    When multiple references have the same standard_id,
    precedence is given to reference defined last.
    References in `extra_csl_items` take precedence over those from `paths`.
    """
    from manubot.cite.csl_item import CSL_Item

    csl_items = []
    paths = list(dict.fromkeys(paths))  # remove duplicates
    for path in paths:
        path = os.fspath(path)
        path_obj = pathlib.Path(path)
        bibliography = load_bibliography(path)
        for csl_item in bibliography:
            csl_item.note_append_text(
                f"Loaded from an external bibliography file by Manubot."
            )
            csl_item.note_append_dict({"source_bibliography": path_obj.name})
            csl_items.append(csl_item)
    csl_items.extend(map(CSL_Item, extra_csl_items))
    manual_refs = dict()
    for csl_item in csl_items:
        try:
            csl_item.standardize_id()
        except Exception:
            csl_item_str = json.dumps(csl_item, indent=2)
            logging.info(
                f"Skipping csl_item where setting standard_id failed:\n{csl_item_str}",
                exc_info=True,
            )
            continue
        standard_id = csl_item["id"]
        csl_item.set_id(shorten_citekey(standard_id))
        csl_item.clean()
        manual_refs[standard_id] = csl_item
    return manual_refs
コード例 #2
0
def load_manual_references(paths=[], extra_csl_items=[]) -> dict:
    """
    Read manual references (overrides) from files specified by a list of paths.
    Returns a standard_citation to CSL Item dictionary. extra_csl_items specifies
    JSON CSL stored as a Python object, to be used in addition to the CSL JSON
    stored as text in the file specified by path. Set paths=[] to only use extra_csl_items.
    """
    from manubot.cite.csl_item import CSL_Item

    csl_items = []
    paths = list(dict.fromkeys(paths))  # remove duplicates
    for path in paths:
        path = pathlib.Path(path)
        if not path.is_file():
            logging.warning(
                f"process.load_bibliographies is skipping a non-existent path: {path}"
            )
            continue
        for csl_item in load_bibliography(path):
            csl_item.note_append_text(
                f"This CSL JSON Item was loaded by Manubot v{manubot_version} from a manual reference file."
            )
            csl_item.note_append_dict({"manual_reference_filename": path.name})
            csl_items.append(csl_item)
    csl_items.extend(map(CSL_Item, extra_csl_items))
    manual_refs = dict()
    for csl_item in csl_items:
        try:
            csl_item.standardize_id()
        except Exception:
            csl_item_str = json.dumps(csl_item, indent=2)
            logging.info(
                f"Skipping csl_item where setting standard_id failed:\n{csl_item_str}",
                exc_info=True,
            )
            continue
        standard_id = csl_item["id"]
        csl_item.set_id(shorten_citekey(standard_id))
        csl_item.clean()
        manual_refs[standard_id] = csl_item
    return manual_refs
コード例 #3
0
ファイル: bibliography.py プロジェクト: epogrebnyak/manubot
def load_manual_references(paths=[], extra_csl_items=[]):
    """
    Read manual references (overrides) from files specified by a list of paths.
    Returns a standard_citation to CSL Item dictionary. extra_csl_items specifies
    JSON CSL stored as a Python object, to be used in addition to the CSL JSON
    stored as text in the file specified by path. Set paths=[] to only use extra_csl_items.
    """
    csl_items = []
    for path in paths:
        path = pathlib.Path(path)
        if not path.is_file():
            logging.warning(
                f'process.load_bibliographies is skipping a non-existent path: {path}'
            )
            continue
        for csl_item in load_bibliography(path):
            append_to_csl_item_note(
                csl_item,
                text=
                f'This CSL JSON Item was loaded by Manubot v{manubot_version} from a manual reference file.',
                dictionary={'manual_reference_filename': path.name},
            )
            csl_items.append(csl_item)
    csl_items.extend(extra_csl_items)
    manual_refs = dict()
    for csl_item in csl_items:
        try:
            csl_item_set_standard_id(csl_item)
        except Exception:
            csl_item_str = json.dumps(csl_item, indent=2)
            logging.info(
                f'Skipping csl_item where setting standard_id failed:\n{csl_item_str}',
                exc_info=True)
            continue
        standard_id = csl_item['id']
        csl_item = csl_item_passthrough(csl_item,
                                        set_id=shorten_citekey(standard_id))
        manual_refs[standard_id] = csl_item
    return manual_refs
コード例 #4
0
ファイル: test_citekey.py プロジェクト: shuvro-zz/manubot
def test_shorten_citekey(standard_citekey, expected):
    short_citekey = shorten_citekey(standard_citekey)
    assert short_citekey == expected