# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Graz University of Technology.
#
# invenio-records-lom is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Filters for Jinja templates."""

import typing as t

import idutils
from flask import current_app


def pid_url(identifier: str, scheme: t.Optional[str] = None, url_scheme: str = "https"):
    """Convert persistent identifier into a link."""
    if scheme is None:
        try:
            scheme = idutils.detect_identifier_schemes(identifier)[0]
        except IndexError:
            scheme = None
    try:
        if scheme and identifier:
            return idutils.to_url(identifier, scheme, url_scheme=url_scheme)
    except Exception:
        current_app.logger.warning(
            f"URL generation for identifier {identifier} failed.",
            exc_info=True,
        )
    return ""

# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Graz University of Technology.
#
# invenio-records-lom is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Filters for Jinja templates."""

import typing as t

import idutils
from flask import current_app


def pid_url(identifier: str, scheme: t.Optional[str] = None, url_scheme: str = "https"):
    """Convert persistent identifier into a link."""
    if scheme is None:
        try:
            scheme = idutils.detect_identifier_schemes(identifier)[0]
        except IndexError:
            scheme = None
    try:
        if scheme and identifier:
            return idutils.to_url(identifier, scheme, url_scheme=url_scheme)
    except Exception:
        current_app.logger.warning(
            f"URL generation for identifier {identifier} failed.",
            exc_info=True,
        )
    return ""

# -*- coding: utf-8 -*- # # Copyright (C) 2021 Graz University of Technology. # # invenio-records-lom is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. Filters for Jinja templates. Convert persistent identifier into a link.