def generate_resource_link(pid, resource_path, static=False, title=None):
    """
    Returns a valid html link to a public resource within an autogenerated instance.

    Args:
        pid: the problem id
        resource_path: the resource path
        static: boolean whether or not it is a static resource
        title: the displayed text. Defaults to the path
    Returns:
        The html link to the resource.
    """

    return '<a target=_blank href="/api/autogen/serve/{}?static={}&pid={}">{}</a>'.format(
        resource_path,
        "true" if static else "false",
        pid,
        resource_path if not title else title
    )