Skip to content

Commit

Permalink
Fix attributions being hidden under image contents (#9)
Browse files Browse the repository at this point in the history
An oversight from commit 459e576 meant that attributions are now hidden underneath the content within image blocks.
  • Loading branch information
syeopite authored Feb 19, 2024
1 parent 19ffb46 commit 7219a4a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 48 deletions.
15 changes: 14 additions & 1 deletion src/npf_renderer/format/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dominate.tags
import dominate.util

from . import text, image, misc
from . import text, image, misc, attribution
from .. import objects, helpers, exceptions


Expand Down Expand Up @@ -94,6 +94,19 @@ def _format_image(self, block, row_length=1, override_padding=None):
if block.caption:
figure.add(dominate.tags.figcaption(block.caption, cls="image-caption"))

# Add attribution HTML
if attr := block.attribution:
if isinstance(attr, objects.attribution.LinkAttribution):
figure.add(attribution.format_link_attribution(attr, self.url_handler))
elif isinstance(attr, objects.attribution.PostAttribution):
figure.add(attribution.format_post_attribution(attr, self.url_handler))
elif isinstance(attr, objects.attribution.AppAttribution):
figure.add(attribution.format_app_attribution(attr, self.url_handler))
else:
# TODO Add "Unsupported Attribution HTML"
raise ValueError(f"Unable to format unsupported attribution: \"{attr}\" ")


return figure

def _format_link(self, block):
Expand Down
13 changes: 0 additions & 13 deletions src/npf_renderer/format/image.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import dominate.tags

from . import attribution
from .. import objects


Expand Down Expand Up @@ -91,18 +90,6 @@ def format_image(image_block, row_length=1, url_handler=lambda url: url,
)
)

# Add attribution HTML
if attr := image_block.attribution:
if isinstance(attr, objects.attribution.LinkAttribution):
container.add(attribution.format_link_attribution(attr, url_handler))
elif isinstance(attr, objects.attribution.PostAttribution):
container.add(attribution.format_post_attribution(attr, url_handler))
elif isinstance(attr, objects.attribution.AppAttribution):
container.add(attribution.format_app_attribution(attr, url_handler))
else:
# TODO Add "Unsupported Attribution HTML"
raise ValueError(f"Unable to format unsupported attribution: \"{attr}\" ")

# Similar to the reason above, we won't be able to hide the poster image once the main content loads.
# if poster_srcset:
# container.add(dominate.tags.img(
Expand Down
74 changes: 40 additions & 34 deletions tests/image_block/image_block_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
from npf_renderer import objects


def format_constructor(*children, container_style=""):
return dominate.tags.div(
dominate.tags.figure(dominate.tags.div(*children, cls="image-container", style=container_style), cls="image-block"), cls="post-body"
)
def format_constructor(*children, container_style="", **kwargs):

figure_element = dominate.tags.figure(cls="image-block")
container = dominate.tags.div(*children, cls="image-container", style=container_style)

figure_element.add(container)
if kwargs.get("figure_children"):
figure_element.add(*kwargs["figure_children"])

return dominate.tags.div(figure_element, cls="post-body")


basic_image_block = (
Expand Down Expand Up @@ -297,19 +303,19 @@ def format_constructor(*children, container_style=""):
sizes="(max-width: 540px) 100vh, 540px",
alt="image",
),

dominate.tags.div(
dominate.tags.a(
"From ",
dominate.tags.b(
"example-blog",
container_style="padding-bottom: 48.7395%;",
figure_children=(
dominate.tags.div(
dominate.tags.a(
"From ",
dominate.tags.b(
"example-blog",
),
href="https://example-blog.tumblr.com/post/1234567890/example-gif-post",
),
href="https://example-blog.tumblr.com/post/1234567890/example-gif-post",
cls="post-attribution"
),
cls="post-attribution"
),

container_style="padding-bottom: 48.7395%;"
)
),
)

Expand Down Expand Up @@ -370,16 +376,16 @@ def format_constructor(*children, container_style=""):
sizes="(max-width: 540px) 100vh, 540px",
alt="image",
),

dominate.tags.div(
dominate.tags.a(
"davidragifs.com",
href="https://davidragifs.com",
container_style="padding-bottom: 140.0%;",
figure_children=(
dominate.tags.div(
dominate.tags.a(
"davidragifs.com",
href="https://davidragifs.com",
),
cls="link-attribution"
),
cls="link-attribution"
),

container_style="padding-bottom: 140.0%;"
)
),
)

Expand Down Expand Up @@ -536,18 +542,18 @@ def format_constructor(*children, container_style=""):
alt="image"
),

dominate.tags.div(
dominate.tags.a(
"View on ",
dominate.tags.b(
"Twitter"
),
href="https://twitter.com/example/status/1234567"
cls="image-container", style="padding-bottom: 130.292%;"
),

dominate.tags.div(
dominate.tags.a(
"View on ",
dominate.tags.b(
"Twitter"
),
cls="post-attribution"
href="https://twitter.com/example/status/1234567"
),

cls="image-container", style="padding-bottom: 130.292%;"
cls="post-attribution"
),
cls="image-block"
),
Expand Down

0 comments on commit 7219a4a

Please sign in to comment.