def _populate_video_source_tags(self, source, options): """ Helper function for video tag, populates source tags from provided options. source_types and sources are mutually exclusive, only one of them can be used. If both are not provided, source types are used (for backwards compatibility) :param source: The public ID of the video :param options: Additional options :return: Resulting source tags (may be empty) """ source_tags = [] # Consume all relevant options, otherwise they are left and passed as attributes video_sources = options.pop('sources', []) source_types = options.pop('source_types', []) source_transformation = options.pop('source_transformation', {}) if video_sources and isinstance(video_sources, list): # processing new source structure with codecs for source_data in video_sources: transformation = options.copy() transformation.update(source_data.get("transformations", {})) source_type = source_data.get("type", '') src = utils.cloudinary_url(source, format=source_type, **transformation)[0] codecs = source_data.get("codecs", []) source_tags.append( "<source {attributes}>".format(attributes=utils.html_attrs( { 'src': src, 'type': self._video_mime_type(source_type, codecs) }))) return source_tags # processing old source_types structure with out codecs if not source_types: source_types = self.default_source_types() if not isinstance(source_types, (list, tuple)): return source_tags for source_type in source_types: transformation = options.copy() transformation.update(source_transformation.get(source_type, {})) src = utils.cloudinary_url(source, format=source_type, **transformation)[0] source_tags.append( "<source {attributes}>".format(attributes=utils.html_attrs( { 'src': src, 'type': self._video_mime_type(source_type) }))) return source_tags
def video(self, **options): public_id = options.get('public_id', self.public_id) source = re.sub("\.{0}$".format("|".join(self.default_source_types())), '', public_id) source_types = options.pop('source_types', []) source_transformation = options.pop('source_transformation', {}) fallback = options.pop('fallback_content', '') options['resource_type'] = options.pop('resource_type', self.resource_type or self.default_resource_type or 'video') if len(source_types) == 0: source_types = self.default_source_types() video_options = options.copy() if 'poster' in video_options: poster_options = video_options['poster'] if isinstance(poster_options, dict): if 'public_id' in poster_options: video_options['poster'] = utils.cloudinary_url(poster_options['public_id'], **poster_options)[0] else: video_options['poster'] = self.video_thumbnail(public_id=source, **poster_options) else: video_options['poster'] = self.video_thumbnail(public_id=source, **options) if not video_options['poster']: del video_options['poster'] html = '<video '; nested_source_types = isinstance(source_types, list) and len(source_types) > 1 if not nested_source_types: source = source + '.' + utils.build_array(source_types)[0]; video_url = utils.cloudinary_url(source, **video_options) video_options = video_url[1] if not nested_source_types: video_options['src'] = video_url[0] if 'html_width' in video_options: video_options['width'] = video_options.pop('html_width') if 'html_height' in video_options: video_options['height'] = video_options.pop('html_height') html = html + utils.html_attrs(video_options) + '>' if nested_source_types: for source_type in source_types: transformation = options.copy() transformation.update(source_transformation.get(source_type, {})) src = utils.cloudinary_url(source, format = source_type, **transformation)[0] video_type = "ogg" if source_type == 'ogv' else source_type mime_type = "video/" + video_type html = html + '<source ' + utils.html_attrs({'src': src, 'type': mime_type}) + '>' html = html + fallback html = html + '</video>' return html
def video(self, **options): public_id = options.get('public_id', self.public_id) source = re.sub("\.({0})$".format("|".join(self.default_source_types())), '', public_id) source_types = options.pop('source_types', []) source_transformation = options.pop('source_transformation', {}) fallback = options.pop('fallback_content', '') options['resource_type'] = options.pop('resource_type', self.resource_type or self.default_resource_type or 'video') if len(source_types) == 0: source_types = self.default_source_types() video_options = options.copy() if 'poster' in video_options: poster_options = video_options['poster'] if isinstance(poster_options, dict): if 'public_id' in poster_options: video_options['poster'] = utils.cloudinary_url(poster_options['public_id'], **poster_options)[0] else: video_options['poster'] = self.video_thumbnail(public_id=source, **poster_options) else: video_options['poster'] = self.video_thumbnail(public_id=source, **options) if not video_options['poster']: del video_options['poster'] html = '<video '; nested_source_types = isinstance(source_types, list) and len(source_types) > 1 if not nested_source_types: source = source + '.' + utils.build_array(source_types)[0]; video_url = utils.cloudinary_url(source, **video_options) video_options = video_url[1] if not nested_source_types: video_options['src'] = video_url[0] if 'html_width' in video_options: video_options['width'] = video_options.pop('html_width') if 'html_height' in video_options: video_options['height'] = video_options.pop('html_height') html = html + utils.html_attrs(video_options) + '>' if nested_source_types: for source_type in source_types: transformation = options.copy() transformation.update(source_transformation.get(source_type, {})) src = utils.cloudinary_url(source, format = source_type, **transformation)[0] video_type = "ogg" if source_type == 'ogv' else source_type mime_type = "video/" + video_type html = html + '<source ' + utils.html_attrs({'src': src, 'type': mime_type}) + '>' html = html + fallback html = html + '</video>' return html
def image(self, **options): """ Generates HTML img tag :param options: Additional options :return: Resulting img tag """ if options.get("resource_type", self.resource_type) == "video": self.default_poster_options(options) custom_attributes = options.pop("attributes", dict()) srcset_option = options.pop("srcset", dict()) srcset_data = dict() if isinstance(srcset_option, dict): srcset_data = config().srcset or dict() srcset_data = srcset_data.copy() srcset_data.update(srcset_option) else: if "srcset" not in custom_attributes: custom_attributes["srcset"] = srcset_option src, attrs = self.__build_url(**options) client_hints = attrs.pop("client_hints", config().client_hints) responsive = attrs.pop("responsive", False) hidpi = attrs.pop("hidpi", False) if (responsive or hidpi) and not client_hints: attrs["data-src"] = src classes = "cld-responsive" if responsive else "cld-hidpi" if "class" in attrs: classes += " " + attrs["class"] attrs["class"] = classes src = attrs.pop("responsive_placeholder", config().responsive_placeholder) if src == "blank": src = CL_BLANK responsive_attrs = self._generate_image_responsive_attributes( custom_attributes, srcset_data, **options) if responsive_attrs: # width and height attributes override srcset behavior, they should be removed from html attributes. for key in {"width", "height"}: attrs.pop(key, None) attrs.update(responsive_attrs) # Explicitly provided attributes override options attrs.update(custom_attributes) if src: attrs["src"] = src return u"<img {0}/>".format(utils.html_attrs(attrs))
def image(self, **options): if options.get("resource_type", self.resource_type) == "video": self.default_poster_options(options) src, attrs = self.__build_url(**options) responsive = attrs.pop("responsive", False) hidpi = attrs.pop("hidpi", False) if responsive or hidpi: attrs["data-src"] = src classes = "cld-responsive" if responsive else "cld-hidpi" if "class" in attrs: classes += " " + attrs["class"] attrs["class"] = classes src = attrs.pop("responsive_placeholder", config().responsive_placeholder) if src == "blank": src = CL_BLANK if src: attrs["src"] = src return mark_safe(u"<img {0}/>".format(utils.html_attrs(attrs)))
def image(self, **options): if options.get("resource_type", self.resource_type or self.default_resource_type) == "video": self.default_poster_options(options) src, attrs = self.__build_url(**options) responsive = attrs.pop("responsive", False) hidpi = attrs.pop("hidpi", False) if responsive or hidpi: attrs["data-src"] = src classes = "cld-responsive" if responsive else "cld-hidpi" if "class" in attrs: classes += " " + attrs["class"] attrs["class"] = classes src = attrs.pop("responsive_placeholder", config().responsive_placeholder) if src == "blank": src = CL_BLANK if src: attrs["src"] = src return u"<img {0}/>".format(utils.html_attrs(attrs))
def image(self, **options): if options.get("resource_type", self.resource_type) == "video": self.default_poster_options(options) src, attrs = self.__build_url(**options) client_hints = attrs.pop("client_hints", config().client_hints) responsive = attrs.pop("responsive", False) hidpi = attrs.pop("hidpi", False) if (responsive or hidpi) and not client_hints: attrs["data-src"] = src classes = "cld-responsive" if responsive else "cld-hidpi" if "class" in attrs: classes += " " + attrs["class"] attrs["class"] = classes src = attrs.pop("responsive_placeholder", config().responsive_placeholder) if src == "blank": src = CL_BLANK if "srcset" in options: srcset_data = options["srcset"] attrs["srcset"] = self.__generate_image_srcset_attribute( srcset_data, **options) if "sizes" in srcset_data and srcset_data["sizes"] is True: attrs["sizes"] = self.__generate_image_sizes_attribute( srcset_data) # width and height attributes override srcset behavior, they should be removed from html attributes. for key in {"width", "height"}: attrs.pop(key, None) if "attributes" in attrs and attrs["attributes"]: # Explicitly provided attributes override options attrs.update(attrs.pop("attributes")) if src: attrs["src"] = src return u"<img {0}/>".format(utils.html_attrs(attrs))
def source(self, **options): attrs = options.get("attributes") or {} srcset_data = config().srcset or dict() srcset_data = srcset_data.copy() srcset_data.update(options.pop("srcset", dict())) responsive_attrs = self._generate_image_responsive_attributes(attrs, srcset_data, **options) attrs.update(responsive_attrs) # `source` tag under `picture` tag uses `srcset` attribute for both `srcset` and `src` urls if "srcset" not in attrs: attrs["srcset"], _ = self.__build_url(**options) if "media" not in attrs: media_attr = self.__generate_media_attr(**(options.get("media", {}))) if media_attr: attrs["media"] = media_attr return u"<source {0}>".format(utils.html_attrs(attrs))
def video(self, **options): """ Creates an HTML video tag for the provided +source+ Examples: CloudinaryResource("mymovie.mp4").video() CloudinaryResource("mymovie.mp4").video(source_types = 'webm') CloudinaryResource("mymovie.ogv").video(poster = "myspecialplaceholder.jpg") CloudinaryResource("mymovie.webm").video(source_types = ['webm', 'mp4'], poster = {'effect': 'sepia'}) :param options: * <tt>source_types</tt> - Specify which source type the tag should include. defaults to webm, mp4 and ogv. * <tt>sources</tt> - Similar to source_types, but may contain codecs list. source_types and sources are mutually exclusive, only one of them can be used. If both are not provided, default source types are used. * <tt>source_transformation</tt> - specific transformations to use for a specific source type. * <tt>poster</tt> - override default thumbnail: * url: provide an ad hoc url * options: with specific poster transformations and/or Cloudinary +:public_id+ :return: Video tag """ public_id = options.get('public_id', self.public_id) source = re.sub( r"\.({0})$".format("|".join(self.default_source_types())), '', public_id) custom_attributes = options.pop("attributes", dict()) fallback = options.pop('fallback_content', '') # Save source types for a single video source handling (it can be a single type) source_types = options.get('source_types', "") poster_options = options.copy() if "poster" not in custom_attributes: options["poster"] = self._generate_video_poster_attr( source, poster_options) if "resource_type" not in options: options["resource_type"] = self.resource_type or "video" # populate video source tags source_tags = self._populate_video_source_tags(source, options) if not source_tags: source = source + '.' + utils.build_array(source_types)[0] video_url, video_options = utils.cloudinary_url(source, **options) if not source_tags: custom_attributes['src'] = video_url attributes = self._collect_video_tag_attributes(video_options) attributes.update(custom_attributes) sources_str = ''.join(str(x) for x in source_tags) html = "<video {attributes}>{sources}{fallback}</video>".format( attributes=utils.html_attrs(attributes), sources=sources_str, fallback=fallback) return html
def video(self, **options): """ Creates an HTML video tag for the provided +source+ Examples: CloudinaryResource("mymovie.mp4").video() CloudinaryResource("mymovie.mp4").video(source_types = 'webm') CloudinaryResource("mymovie.ogv").video(poster = "myspecialplaceholder.jpg") CloudinaryResource("mymovie.webm").video(source_types = ['webm', 'mp4'], poster = {'effect': 'sepia'}) :param options: * <tt>source_types</tt> - Specify which source type the tag should include. defaults to webm, mp4 and ogv. * <tt>source_transformation</tt> - specific transformations to use for a specific source type. * <tt>poster</tt> - override default thumbnail: * url: provide an ad hoc url * options: with specific poster transformations and/or Cloudinary +:public_id+ :return: Video tag """ public_id = options.get('public_id', self.public_id) source = re.sub(r"\.({0})$".format("|".join(self.default_source_types())), '', public_id) source_types = options.pop('source_types', []) source_transformation = options.pop('source_transformation', {}) fallback = options.pop('fallback_content', '') options['resource_type'] = options.pop('resource_type', self.resource_type or 'video') if not source_types: source_types = self.default_source_types() video_options = options.copy() if 'poster' in video_options: poster_options = video_options['poster'] if isinstance(poster_options, dict): if 'public_id' in poster_options: video_options['poster'] = utils.cloudinary_url(poster_options['public_id'], **poster_options)[0] else: video_options['poster'] = self.video_thumbnail( public_id=source, **poster_options) else: video_options['poster'] = self.video_thumbnail(public_id=source, **options) if not video_options['poster']: del video_options['poster'] nested_source_types = isinstance(source_types, list) and len(source_types) > 1 if not nested_source_types: source = source + '.' + utils.build_array(source_types)[0] video_url = utils.cloudinary_url(source, **video_options) video_options = video_url[1] if not nested_source_types: video_options['src'] = video_url[0] if 'html_width' in video_options: video_options['width'] = video_options.pop('html_width') if 'html_height' in video_options: video_options['height'] = video_options.pop('html_height') sources = "" if nested_source_types: for source_type in source_types: transformation = options.copy() transformation.update(source_transformation.get(source_type, {})) src = utils.cloudinary_url(source, format=source_type, **transformation)[0] video_type = "ogg" if source_type == 'ogv' else source_type mime_type = "video/" + video_type sources += "<source {attributes}>".format(attributes=utils.html_attrs({'src': src, 'type': mime_type})) html = "<video {attributes}>{sources}{fallback}</video>".format( attributes=utils.html_attrs(video_options), sources=sources, fallback=fallback) return html