Skip to content

Commit

Permalink
always rewrite images in document content
Browse files Browse the repository at this point in the history
this ensures that all images, even in AKN HTML, have the correct URL
including the document's full expression FRBR URI.
  • Loading branch information
longhotsummer committed Aug 29, 2024
1 parent 8bfeabb commit 9e1c72b
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions peachjam/views/generic_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
Predicate,
Relationship,
)
from peachjam.xmlutils import parse_html_str
from peachjam_api.serializers import (
CitationLinkSerializer,
PredicateSerializer,
Expand Down Expand Up @@ -280,8 +281,7 @@ def get_context_data(self, **kwargs):
context["display_type"] = (
"akn" if context["document"].content_html_is_akn else "html"
)
if not context["document"].content_html_is_akn:
self.prefix_images(context["document"])
self.prefix_images(context["document"])
elif hasattr(context["document"], "source_file"):
context["display_type"] = "pdf"
else:
Expand Down Expand Up @@ -408,14 +408,14 @@ def get_notices(self):

def prefix_images(self, document):
"""Rewrite image URLs so that we can serve them correctly."""
root = html.fromstring(document.content_html)
root = parse_html_str(document.content_html)

for img in root.xpath(".//img[@src]"):
src = img.attrib["src"]
if not src.startswith("/") and not src.startswith("data:"):
img.attrib["src"] = (
document.expression_frbr_uri + "/media/" + img.attrib["src"]
)
if not src.startswith("media/"):
src = "media/" + src
img.attrib["src"] = document.expression_frbr_uri + "/" + src

document.content_html = html.tostring(root, encoding="unicode")

Expand Down

0 comments on commit 9e1c72b

Please sign in to comment.