Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTango committed Jun 3, 2022
1 parent 5befed3 commit 76705bb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 28 deletions.
29 changes: 5 additions & 24 deletions plone/outputfilters/filters/resolveuid_and_caption.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from Acquisition import aq_acquire
from Acquisition import aq_base
from Acquisition import aq_inner
from Acquisition import aq_parent
from bs4 import BeautifulSoup
from DocumentTemplate.html_quote import html_quote
Expand Down Expand Up @@ -29,13 +30,6 @@
import six


HAS_LINGUAPLONE = True
try:
from Products.LinguaPlone.utils import translated_references
except ImportError:
HAS_LINGUAPLONE = False


appendix_re = re.compile('^(.*)([?#].*)$')
resolveuid_re = re.compile('^[./]*resolve[Uu]id/([^/]*)/?(.*)$')

Expand Down Expand Up @@ -146,7 +140,6 @@ def _render_resolveuid(self, href):
def __call__(self, data):
data = re.sub(r'<([^<>\s]+?)\s*/>', self._shorttag_replace, data)
soup = BeautifulSoup(safe_unicode(data), 'html.parser')

for elem in soup.find_all(['a', 'area']):
attributes = elem.attrs
href = attributes.get('href')
Expand Down Expand Up @@ -192,10 +185,9 @@ def __call__(self, data):
# we could get the width/height (aspect ratio) without the scale
# from the image field: width, height = fullimage.get("image").getImageSize()
# XXX: refacture resolve_image to not create scales
if not image:
return
attributes["width"] = image.width
attributes["height"] = image.height
if image and hasattr(image, "width"):
attributes["width"] = image.width
attributes["height"] = image.height
if fullimage is not None:
# Check to see if the alt / title tags need setting
title = safe_unicode(aq_acquire(fullimage, 'Title')())
Expand All @@ -204,7 +196,6 @@ def __call__(self, data):
attributes['alt'] = description or ""
if 'title' not in attributes:
attributes['title'] = title

for picture_elem in soup.find_all('picture'):
if 'captioned' not in picture_elem.attrs.get('class', []):
continue
Expand Down Expand Up @@ -237,16 +228,6 @@ def __call__(self, data):

return six.text_type(soup)

def lookup_uid(self, uid):
context = self.context
if HAS_LINGUAPLONE:
# If we have LinguaPlone installed, add support for language-aware
# references
uids = translated_references(context, context.Language(), uid)
if len(uids) > 0:
uid = uids[0]
return uuidToObject(uid)

def resolve_scale_data(self, url):
""" return scale url, width and height
"""
Expand All @@ -271,7 +252,7 @@ def resolve_link(self, href):
match = resolveuid_re.match(subpath)
if match is not None:
uid, _subpath = match.groups()
obj = self.lookup_uid(uid)
obj = uuidToObject(uid)
if obj is not None:
subpath = _subpath

Expand Down
8 changes: 4 additions & 4 deletions plone/outputfilters/tests/test_resolveuid_and_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ def test_image_captioning_in_news_item(self):
# Test captioning
output = news_item.text.output
text_out = """<span><figure class="captioned">
<img alt="My caption" height="331" src="http://nohost/plone/image.jpg/@@images/...jpeg" title="Image" width="500"/>
<img height="331" src="http://nohost/plone/image.jpg/@@images/...jpeg" title="Image" width="500"/>
<figcaption class="image-caption">My caption</figcaption>
</figure>
</span>"""
Expand All @@ -354,7 +354,7 @@ def test_image_captioning_absolute_path(self):
self._assertTransformsTo(text_in, text_out)

def test_image_captioning_relative_path(self):
text_in = """<img class="captioned" src="image.jpg"/>"""
text_in = """<img class="captioned" src="./image.jpg"/>"""
text_out = """<figure class="captioned">
<img alt="My caption" height="331" src="http://nohost/plone/image.jpg/@@images/...jpeg" title="Image" width="500"/>
<figcaption class="image-caption">My caption</figcaption>
Expand Down Expand Up @@ -423,15 +423,15 @@ def test_image_captioning_resolveuid_new_scale_plone_namedfile(self):
def test_image_captioning_resolveuid_no_scale(self):
text_in = """<img class="captioned" src="resolveuid/%s/@@images/image"/>""" % self.UID
text_out = """<figure class="captioned">
<img alt="My caption" height="331" src="http://nohost/plone/image.jpg/@@images/...jpeg" title="Image" width="500"/>
<img alt="My caption" class="captioned" height="331" src="http://nohost/plone/image.jpg/@@images/...jpeg" title="Image" width="500"/>
<figcaption class="image-caption">My caption</figcaption>
</figure>"""
self._assertTransformsTo(text_in, text_out)

def test_image_captioning_resolveuid_with_srcset_and_src(self):
text_in = """<img class="captioned" src="resolveuid/%s/@@images/image" srcset="resolveuid/%s/@@images/image 480w,resolveuid/%s/@@images/image 360w"/>""" % (self.UID, self.UID, self.UID)
text_out = """<figure class="captioned">
<img alt="My caption" height="331" src="http://nohost/plone/image.jpg/@@images/...jpeg" srcset="http://nohost/plone/image.jpg/@@images/...jpeg 480w,http://nohost/plone/image.jpg/@@images/...jpeg 360w" title="Image" width="500"/>
<img alt="My caption" class="captioned" height="331" src="http://nohost/plone/image.jpg/@@images/...jpeg" srcset="http://nohost/plone/image.jpg/@@images/...jpeg 480w,http://nohost/plone/image.jpg/@@images/...jpeg 360w" title="Image" width="500"/>
<figcaption class="image-caption">My caption</figcaption>
</figure>"""
self._assertTransformsTo(text_in, text_out)
Expand Down

0 comments on commit 76705bb

Please sign in to comment.