Skip to content

Commit

Permalink
Merge pull request #1982 from laws-africa/images
Browse files Browse the repository at this point in the history
always rewrite images in document content
  • Loading branch information
longhotsummer authored Aug 29, 2024
2 parents eb1bd8f + 9e1c72b commit c11594f
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 c11594f

Please sign in to comment.