Example #1
0
def image(field: TemplateField) -> (str, str):
    """ Transform an image field into an image tag, unless field specifies otherwise.  """

    image_path = field.name

    no_transform = False

    width = None
    height = None

    if field.context is not None:
        if field.context == TemplateFieldDescriptors.COPY_ONLY:
            no_transform = True
        else:
            width, height = image_size(image_path, field.context)

    if not is_image(image_path):
        # the file is not an image; or something has gone wrong
        if no_transform or (width is not None or height is not None):
            # if either of these attributes exist, then it likely was supposed to be an image
            # but we could not resolve it properly- so warn about it
            WarningDisplay.unresolved_image_reference_error(
                image_reference=image_path,
                closest_resolution_value=field.name)

        return None, None  # no image, no tag

    resource_path = image_path

    if is_resource(image_path):
        image_name = os.path.basename(image_path)
        # transform the path so that it is relative within the output directory,
        # this way we can keep every resource contained
        resource_path = get_resource_path(image_name)

    if no_transform:
        return image_path, resource_path  # image path in resources, no tag

    return image_path, get_image_tag(resource_path, width, height)
Example #2
0
def image(field: TemplateField) -> (str, str):
    """ Transform an image field into an image tag, unless field specifies otherwise.  """

    image_path = field.name

    no_transform = False

    width = None
    height = None

    if field.context is not None:
        if field.context == TemplateFieldDescriptors.COPY_ONLY:
            no_transform = True
        else:
            width, height = image_size(image_path, field.context)

    if not is_image(image_path):
        # the file is not an image; or something has gone wrong
        if no_transform or (width is not None or height is not None):
            # if either of these attributes exist, then it likely was supposed to be an image
            # but we could not resolve it properly- so warn about it
            WarningDisplay.unresolved_image_reference_error(
                image_reference=image_path,
                closest_resolution_value=field.name)

        return None, None  # no image, no tag

    image_name = os.path.basename(image_path)

    # transform the path so that it is relative within the output directory,
    # this way we can keep every resource contained
    resource_path = get_resource_path(image_name)

    if no_transform:
        return image_path, resource_path  # image path in resources, no tag

    return image_path, get_image_tag(resource_path, width, height)