feat: display license information on article page

Added functionality to fetch and display license information for wiki
articles. This enhancement provides users with clarity on content usage
permissions by showing a link to the article's license in the footer.

Fixes #35.
This commit is contained in:
Kumi 2024-08-30 17:35:40 +02:00
parent 70c40fd979
commit 87a4f07285
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 23 additions and 3 deletions

View file

@ -387,8 +387,6 @@ def wiki_article(
if parent.attrs.get("data-mw-group", None):
span["class"] = span.get("class", []) + [parent.attrs["data-mw-group"]]
processed_html = str(body)
rtl = bool(soup.find("div", class_="mw-parser-output", dir="rtl"))
# Edge case: When passing the `ku-arab` variant, the article is in Arabic
@ -399,6 +397,20 @@ def wiki_article(
processed_html = str(body)
# Get license information from the article
mediawiki_api_request = urllib.request.Request(
f"{base_url}/w/rest.php/v1/page/{quote(escape(title.replace(' ', '_'), False))}",
headers=HEADERS,
)
try:
with urllib.request.urlopen(mediawiki_api_request) as response:
data = json.loads(response.read().decode())
license = data["license"]
except Exception as e:
logger.error(f"Error fetching license information: {e}")
license = None
return render_template(
"article.html",
title=title.replace("_", " "),
@ -406,6 +418,7 @@ def wiki_article(
lang=lang,
project=project,
rtl=rtl,
license=license,
)

View file

@ -3,4 +3,10 @@
{% block content %}
<h1 class="title{% if rtl %} title-rtl{% endif %}">{{ title }}</h1>
{{ content|safe }}
{% endblock %}
{% endblock %}
{% block license %}
{% if license %}
<p class="license">This content is licensed under the <a href="{{ license.url }}">{{ license.title }}</a>.</p>
{% endif %}
{% endblock %}

View file

@ -28,6 +28,7 @@
{% block content %}{% endblock %}
</div>
<div id="footer">
{% block license %}{% endblock %}
<p>Brought to you by <a href="https://git.private.coffee/privatecoffee/wikimore">Wikimore</a>, a <a href="https://private.coffee">Private.coffee</a> project</p>
</div>
</body>