def handle_captioned_image(self, attributes, image, fullimage, caption):
        """Handle captioned image.
        """
        klass = attributes['class']
        del attributes['class']
        del attributes['src']
        if 'width' in attributes:
            attributes['width'] = int(attributes['width'])
        if 'height' in attributes:
            attributes['height'] = int(attributes['height'])
        view = fullimage.unrestrictedTraverse('@@images', None)
        if view is not None:
            original_width, original_height = view.getImageSize()
        else:
            original_width, original_height = fullimage.width, fullimage.height
        if image is not fullimage:
            # image is a scale object
            tag = image.tag
            width = image.width
        else:
            if hasattr(aq_base(image), 'tag'):
                tag = image.tag
            else:
                tag = view.scale().tag
            width = original_width
        options = {
            'class':
            klass,
            'originalwidth':
            attributes.get('width', None),
            'originalalt':
            attributes.get('alt', None),
            'url_path':
            fullimage.absolute_url_path(),
            'caption':
            newline_to_br(html_quote(caption)),
            'image':
            image,
            'fullimage':
            fullimage,
            'tag':
            tag(**attributes),
            'isfullsize':
            image is fullimage or (image.width == original_width
                                   and image.height == original_height),
            'width':
            attributes.get('width', width),
        }
        if self.in_link:
            # Must preserve original link, don't overwrite
            # with a link to the image
            options['isfullsize'] = True

        captioned_html = self.captioned_image_template(**options)
        if isinstance(captioned_html, six.text_type):
            captioned_html = captioned_html.encode('utf8')
        self.append_data(captioned_html)
    def handle_captioned_image(self, attributes, image, fullimage,
                               elem, caption):
        """Handle captioned image.

        The img element is replaced by a definition list
        as created by the template ../browser/captioned_image.pt
        """
        klass = ' '.join(attributes['class'])
        del attributes['class']
        del attributes['src']
        if 'width' in attributes:
            attributes['width'] = int(attributes['width'])
        if 'height' in attributes:
            attributes['height'] = int(attributes['height'])
        view = fullimage.unrestrictedTraverse('@@images', None)
        if view is not None:
            original_width, original_height = view.getImageSize()
        else:
            original_width, original_height = fullimage.width, fullimage.height
        if image is not fullimage:
            # image is a scale object
            tag = image.tag
            width = image.width
        else:
            if hasattr(aq_base(image), 'tag'):
                tag = image.tag
            else:
                tag = view.scale().tag
            width = original_width
        options = {
            'class': klass,
            'originalwidth': attributes.get('width', None),
            'originalalt': attributes.get('alt', None),
            'url_path': fullimage.absolute_url_path(),
            'caption': newline_to_br(html_quote(caption)),
            'image': image,
            'fullimage': fullimage,
            'tag': tag(**attributes),
            'isfullsize': image is fullimage or (
                image.width == original_width and
                image.height == original_height),
            'width': attributes.get('width', width),
        }

        captioned = BeautifulSoup(
            self.captioned_image_template(**options), 'html.parser')

        # if we are a captioned image within a link, remove and occurrences
        # of a tags inside caption template to preserve the outer link
        if bool(elem.find_parent('a')):
            captioned.a.unwrap()

        elem.replace_with(captioned)
Beispiel #3
0
    def handle_captioned_image(self, attributes, image, fullimage, caption):
        """Handle captioned image.
        """
        klass = attributes['class']
        del attributes['class']
        del attributes['src']
        if 'width' in attributes:
            attributes['width'] = int(attributes['width'])
        if 'height' in attributes:
            attributes['height'] = int(attributes['height'])
        view = fullimage.unrestrictedTraverse('@@images', None)
        if view is not None:
            original_width, original_height = view.getImageSize()
        else:
            original_width, original_height = fullimage.width, fullimage.height
        if image is not fullimage:
            # image is a scale object
            tag = image.tag
            width = image.width
        else:
            if hasattr(aq_base(image), 'tag'):
                tag = image.tag
            else:
                tag = view.scale().tag
            width = original_width
        options = {
            'class': klass,
            'originalwidth': attributes.get('width', None),
            'originalalt': attributes.get('alt', None),
            'url_path': fullimage.absolute_url_path(),
            'caption': newline_to_br(html_quote(caption)),
            'image': image,
            'fullimage': fullimage,
            'tag': tag(**attributes),
            'isfullsize': image is fullimage or (
                image.width == original_width and
                image.height == original_height),
            'width': attributes.get('width', width),
        }
        if self.in_link:
            # Must preserve original link, don't overwrite
            # with a link to the image
            options['isfullsize'] = True

        captioned_html = self.captioned_image_template(**options)
        if isinstance(captioned_html, unicode):
            captioned_html = captioned_html.encode('utf8')
        self.append_data(captioned_html)
Beispiel #4
0
            def replaceImage(match):
                tag = match.group(1) or match.group(2)
                attrs = ATTR_PATTERN.match(tag)
                src = attrs.group('src')
                klass = attrs.group('class')
                width = attrs.group('width')
                if src:
                    d = attrs.groupdict()
                    target = at_tool.reference_catalog.lookupObject(src)
                    if target:
                        d['caption'] = newline_to_br(target.Description())
                        d['tag'] = CLASS_PATTERN.sub('', d['tag'])
                        if not width:
                            d['width'] = target.getWidth()

                        return IMAGE_TEMPLATE % d
                return match.group(0) # No change
    def handle_captioned_image(self, attributes, image, fullimage, caption):
        """Handle captioned image.
        """
        klass = attributes["class"]
        del attributes["class"]
        del attributes["src"]
        if "width" in attributes:
            attributes["width"] = int(attributes["width"])
        if "height" in attributes:
            attributes["height"] = int(attributes["height"])
        view = fullimage.unrestrictedTraverse("@@images", None)
        if view is not None:
            original_width, original_height = view.getImageSize()
        else:
            original_width, original_height = fullimage.width, fullimage.height
        if image is not fullimage:
            # image is a scale object
            tag = image.tag
            width = image.width
        else:
            if hasattr(aq_base(image), "tag"):
                tag = image.tag
            else:
                tag = view.scale().tag
            width = original_width
        options = {
            "class": klass,
            "originalwidth": attributes.get("width", None),
            "originalalt": attributes.get("alt", None),
            "url_path": fullimage.absolute_url_path(),
            "caption": newline_to_br(html_quote(caption)),
            "image": image,
            "fullimage": fullimage,
            "tag": tag(**attributes),
            "isfullsize": image is fullimage or (image.width == original_width and image.height == original_height),
            "width": attributes.get("width", width),
        }
        if self.in_link:
            # Must preserve original link, don't overwrite
            # with a link to the image
            options["isfullsize"] = True

        captioned_html = self.captioned_image_template(**options)
        if isinstance(captioned_html, unicode):
            captioned_html = captioned_html.encode("utf8")
        self.append_data(captioned_html)
Beispiel #6
0
    def handle_captioned_image(self, attributes, image, fullimage, elem,
                               caption):
        """Handle captioned image.

        The img element is replaced by a definition list
        as created by the template ../browser/captioned_image.pt
        """
        klass = ' '.join(attributes['class'])
        del attributes['class']
        del attributes['src']
        if 'width' in attributes and attributes['width']:
            attributes['width'] = int(attributes['width'])
        if 'height' in attributes and attributes['height']:
            attributes['height'] = int(attributes['height'])
        view = fullimage.unrestrictedTraverse('@@images', None)
        if view is not None:
            original_width, original_height = view.getImageSize()
        else:
            original_width, original_height = fullimage.width, fullimage.height
        if image is not fullimage:
            # image is a scale object
            tag = image.tag
            width = image.width
        else:
            if hasattr(aq_base(image), 'tag'):
                tag = image.tag
            else:
                tag = view.scale().tag
            width = original_width
        options = {
            'class':
            klass,
            'originalwidth':
            attributes.get('width', None),
            'originalalt':
            attributes.get('alt', None),
            'url_path':
            fullimage.absolute_url_path(),
            'caption':
            newline_to_br(html_quote(caption)),
            'image':
            image,
            'fullimage':
            fullimage,
            'tag':
            tag(**attributes),
            'isfullsize':
            image is fullimage or (image.width == original_width
                                   and image.height == original_height),
            'width':
            attributes.get('width', width),
        }

        captioned = BeautifulSoup(self.captioned_image_template(**options),
                                  'html.parser')

        # if we are a captioned image within a link, remove and occurrences
        # of a tags inside caption template to preserve the outer link
        if bool(elem.find_parent('a')):
            captioned.a.unwrap()

        elem.replace_with(captioned)
Beispiel #7
0
            def replaceImage(match):
                """ Replace image
                """
                tag = match.group('pat0') or match.group('pat1')
                attrs = ATTR_PATTERN.match(tag)
                atag = match.group('atag0') or match.group('atag1')
                src = attrs.group('src')
                subtarget = None
                m = SRC_TAIL.match(tag, attrs.end('src'))
                if m is not None:
                    srctail = m.group(1)
                else:
                    srctail = None
                if src is not None:
                    d = attrs.groupdict()
                    target = self.resolveuid(context, rc, src)
                    if target is not None:
                        d['class'] = attrs.group('class')
                        d['originalwidth'] = attrs.group('width')
                        d['originalalt'] = attrs.group('alt')
                        d['url_path'] = target.absolute_url_path()
                        d['caption'] = \
                             newline_to_br(html_quote(target.Description()))
                        d['image'] = d['fullimage'] = target
                        d['tag'] = None
                        d['isfullsize'] = True
                        d['width'] = target.width
                        if srctail:
                            if isinstance(srctail, unicode):
                                # restrictedTraverse doesn't accept unicode
                                srctail = srctail.encode('utf8')
                            try:
                                subtarget = target.restrictedTraverse(srctail)
                            except Exception:
                                subtarget = getattr(target, srctail, None)
                            if subtarget is not None:
                                d['image'] = subtarget

                            if srctail.startswith('image_'):
                                d['tag'] = \
                               target.getField('image').tag(target,
                                                            scale=srctail[6:])
                            elif subtarget:
                                d['tag'] = subtarget.tag()

                        if d['tag'] is None:
                            d['tag'] = target.tag()

                        if subtarget is not None:
                            d['isfullsize'] = subtarget.width == \
                               target.width and subtarget.height == \
                               target.height
                            d['width'] = subtarget.width

                        # Strings that may contain non-ascii characters
                        # need to be decoded to unicode
                        for key in ('caption', 'tag'):
                            if isinstance(d[key], str):
                                d[key] = d[key].decode('utf8')

                        # Must preserve original link, don't overwrite with
                        # a link to the image
                        if atag is not None:
                            d['isfullsize'] = True
                            d['tag'] = "%s%s</a>" % (atag, d['tag'])

                        result = template(**d)
                        if isinstance(result, str):
                            result = result.decode('utf8')

                        return result

                return match.group(0) # No change