Skip to content

Commit

Permalink
Fix broken links in reviews hypertext
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenwardy committed May 2, 2024
1 parent 4ac0016 commit 8e719e3
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions app/utils/minetest_hypertext.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ def make_indent(w):


class MinetestHTMLParser(HTMLParser):
def __init__(self, page_url: str, include_images: bool):
def __init__(self, page_url: str, include_images: bool, link_prefix: str):
super().__init__()
self.page_url = page_url
self.include_images = include_images
self.link_prefix = link_prefix

self.completed_text = ""
self.current_line = ""
Expand Down Expand Up @@ -101,7 +102,7 @@ def handle_starttag(self, tag, attrs):
if self.last_id is not None:
url = url + "#" + self.last_id

name = f"link_{len(self.links)}"
name = f"{self.link_prefix}{len(self.links)}"
self.links[name] = url
self.current_line += f"<action name={name}><u>"
self.current_line += escape_hypertext(gettext("(view table in browser)"))
Expand All @@ -117,7 +118,7 @@ def handle_starttag(self, tag, attrs):
self.current_line += "<b>"
elif tag == "a":
if "href" in attr_by_name:
name = f"link_{len(self.links)}"
name = f"{self.link_prefix}{len(self.links)}"
self.links[name] = self.resolve_url(attr_by_name["href"])
self.current_line += f"<action name={name}><u>"
else:
Expand Down Expand Up @@ -223,8 +224,8 @@ def handle_entityref(self, name):
self.current_line += f"&{name};"


def html_to_minetest(html, page_url: str, formspec_version: int = 7, include_images: bool = True):
parser = MinetestHTMLParser(page_url, include_images)
def html_to_minetest(html, page_url: str, formspec_version: int = 7, include_images: bool = True, link_prefix: str = "link_"):
parser = MinetestHTMLParser(page_url, include_images, link_prefix)
parser.feed(html)
parser.finish_line()

Expand Down Expand Up @@ -324,7 +325,8 @@ def make_link(url: str, label: str):
for review in package.reviews:
review: PackageReview
html = render_markdown(review.thread.first_reply.comment)
content = html_to_minetest(html, package.get_url("packages.view", absolute=True), formspec_version)["body"].strip()
content = html_to_minetest(html, package.get_url("packages.view", absolute=True),
formspec_version, False, f"review_{review.id}_")["body"].strip()
author = make_link(abs_url_for("users.profile", username=review.author.username), review.author.display_name)
rating = ["👎", "👎", "-", "👍", "👍"][review.rating - 1]
comments = make_link(abs_url_for("threads.view", id=review.thread.id), "Comments")
Expand Down

0 comments on commit 8e719e3

Please sign in to comment.