Skip to content

Commit f65a5c7

Browse files
authored
Add old version alert to docs views (boostorg#1795) (boostorg#1960)
1 parent 7d59cb4 commit f65a5c7

File tree

10 files changed

+108
-18
lines changed

10 files changed

+108
-18
lines changed

core/views.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
23
from django.utils import timezone
34

45
from urllib.parse import urljoin
@@ -24,12 +25,14 @@
2425

2526
from config.settings import ENABLE_DB_CACHE
2627
from libraries.constants import LATEST_RELEASE_URL_PATH_STR
28+
from libraries.mixins import VersionAlertMixin
2729
from libraries.utils import (
2830
legacy_path_transform,
2931
get_prioritized_library_view,
3032
generate_canonical_library_uri,
33+
set_selected_boost_version,
3134
)
32-
from versions.models import Version
35+
from versions.models import Version, docs_path_to_boost_name
3336

3437
from .asciidoc import convert_adoc_to_html
3538
from .boostrenderer import (
@@ -343,14 +346,30 @@ def get_context_data(self, **kwargs):
343346
if content_type == "text/asciidoc":
344347
content_type = "text/html"
345348

346-
context.update({"content": content, "content_type": content_type})
347-
349+
context.update(
350+
{
351+
"content": content,
352+
"content_type": content_type,
353+
"selected_version": self.get_selected_version(),
354+
}
355+
)
348356
logger.info(
349357
"get_content_from_s3_view_success", key=self.kwargs.get("content_path")
350358
)
351359

352360
return context
353361

362+
def get_selected_version(self) -> Version | None:
363+
content_path = self.kwargs.get("content_path")
364+
boost_name = docs_path_to_boost_name(content_path)
365+
if not boost_name:
366+
return None
367+
try:
368+
version = Version.objects.get(name=boost_name)
369+
except Version.DoesNotExist:
370+
version = None
371+
return version
372+
354373
def get_from_cache(self, static_content_cache, cache_key):
355374
cached_result = static_content_cache.get(cache_key)
356375
return cached_result if cached_result else None
@@ -487,20 +506,28 @@ def normalize_boost_doc_path(content_path: str) -> str:
487506
return f"/archives/{content_path}"
488507

489508

490-
class DocLibsTemplateView(BaseStaticContentTemplateView):
509+
class DocLibsTemplateView(VersionAlertMixin, BaseStaticContentTemplateView):
491510
allowed_db_save_types = {
492511
"text/asciidoc",
493512
"text/html",
494513
"text/html; charset=utf-8",
495514
"text/css; charset=utf-8",
496515
}
497516

517+
def dispatch(self, request, *args, **kwargs):
518+
response = super().dispatch(request, *args, **kwargs)
519+
set_selected_boost_version(
520+
self.kwargs.get("content_path").split("/", 1)[0], response
521+
)
522+
return response
523+
498524
def get_from_s3(self, content_path):
499525
legacy_url = normalize_boost_doc_path(content_path)
500526
return super().get_from_s3(legacy_url)
501527

502528
def process_content(self, content: bytes):
503529
"""Replace page header with the local one."""
530+
context = super().get_context_data()
504531
content_type = self.content_dict.get("content_type")
505532
modernize = self.request.GET.get("modernize", "med").lower()
506533
if (
@@ -522,10 +549,12 @@ def process_content(self, content: bytes):
522549

523550
soup = self._required_modernization_changes(soup)
524551

525-
context = {
526-
"content": str(soup),
527-
"canonical_uri": canonical_uri if canonical_uri != req_uri else None,
528-
}
552+
context.update(
553+
{
554+
"content": str(soup),
555+
"canonical_uri": canonical_uri if canonical_uri != req_uri else None,
556+
}
557+
)
529558
template_name = "original_docs.html"
530559

531560
if is_in_fully_modernized_libs(self.request.path):

frontend/styles.css

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@
117117
}
118118

119119
#docsiframe {
120-
@apply w-full mx-0 my-auto overflow-hidden p-0 mt-[1px];
120+
@apply w-full mx-0 my-auto overflow-hidden p-0;
121121
height: calc(100vh - 2.6rem)
122122
}
123+
124+
.version_alert {
125+
@apply py-2 px-3 mt-2 mb-3 text-center rounded-sm bg-yellow-200/70
126+
}
127+
.version_alert p {
128+
@apply p-0 m-0 text-center;
129+
}
130+
.version_alert a {
131+
@apply font-semibold underline dark:text-white text-charcoal
132+
}

libraries/github.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ def handle_commit(commit: ParsedCommit):
506506
)
507507

508508
except KeyError:
509-
logger.error(f"KeyError {commit.version=}")
509+
logger.warning(f"KeyError {commit.version=}")
510510
return None
511511

512512
def handle_version_diff_stat(diff: VersionDiffStat):

libraries/mixins.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import structlog
24
from types import SimpleNamespace
35

@@ -32,11 +34,27 @@ def get_context_data(self, **kwargs):
3234
if url_name in {"libraries", "releases-most-recent"}:
3335
return context
3436
current_version_kwargs = self.kwargs.copy()
35-
current_version_kwargs.update({"version_slug": LATEST_RELEASE_URL_PATH_STR})
37+
38+
if url_name == "docs-libs-page":
39+
alert_visible = not current_version_kwargs.get("content_path").startswith(
40+
LATEST_RELEASE_URL_PATH_STR
41+
)
42+
current_version_kwargs.update(
43+
{
44+
"content_path": re.sub(
45+
r"([_0-9]+)/(\S+)",
46+
rf"{LATEST_RELEASE_URL_PATH_STR}/\2",
47+
current_version_kwargs.get("content_path"),
48+
)
49+
}
50+
)
51+
else:
52+
current_version_kwargs.update({"version_slug": LATEST_RELEASE_URL_PATH_STR})
53+
alert_visible = (
54+
self.kwargs.get("version_slug") != LATEST_RELEASE_URL_PATH_STR
55+
)
3656
context["version_alert_url"] = reverse(url_name, kwargs=current_version_kwargs)
37-
context["version_alert"] = (
38-
self.kwargs.get("version_slug") != LATEST_RELEASE_URL_PATH_STR
39-
)
57+
context["version_alert"] = alert_visible
4058
return context
4159

4260

templates/docsiframe.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
{% endblock %}
1111

1212
{% block content %}
13+
{# alert for non-current Boost versions #}
14+
<div class="bg-white dark:text-white text-slate dark:bg-charcoal dark:bg-neutral-700 py-1">
15+
{% include "libraries/includes/version_alert.html" %}
16+
</div>
1317
<iframe
1418
{% if iframe_url %}
1519
src="{{ iframe_url }}"

templates/includes/_legacy_docs_header.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,5 @@ <h1 class="heading-title">
2727
</div>
2828
</div>
2929
</div>
30+
{# alert for non-current Boost versions #}
31+
{% include "libraries/includes/version_alert.html" %}

templates/libraries/_library_vertical_list_item.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<td class="w-8 align-top">
2222
{% include "libraries/includes/_documentation_link_icon.html" %}
2323
</td>
24-
<td class="align-top hidden md:block mt-2 pl-3 md:h-20 md:items-center h-full">{{ library_version.description|default:"No description provided for this version." }}</td>
24+
<td class="align-top hidden md:block mt-2 pl-3 md:items-center h-full">{{ library_version.description|default:"No description provided for this version." }}</td>
2525
</tr>
2626
<tr class="block md:hidden pl-1 align-top">
2727
<td>{{ library_version.description|default:"No description provided for this version." }}</td>

templates/libraries/includes/version_alert.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% if version_alert %}
2-
<div role="alert" class="py-2 px-3 mb-3 text-center rounded-sm bg-yellow-200/70">
3-
<p class="p-0 m-0">
2+
<div role="alert" class="version_alert">
3+
<p>
44
<i class="fas fa-exclamation-circle"></i>
55
{% if selected_version == current_version %}
66
You've currently chosen the {{ current_version.display_name }} version. If a newer release comes out, you will continue to view the {{ current_version.display_name }} version, not the new <a href="{{ version_alert_url }}" class="font-semibold underline dark:text-white text-charcoal">latest release</a>.
@@ -12,7 +12,7 @@
1212
{% else %}
1313
This version of Boost is under active development.
1414
{% endif %}
15-
The <a href="{{ version_alert_url }}" class="font-semibold underline dark:text-white text-charcoal">current version</a> is {{ current_version.display_name }}.
15+
The <a href="{{ version_alert_url }}">current version</a> is {{ current_version.display_name }}.
1616
{% endif %}
1717
</p>
1818
</div>

templates/original_docs.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<head>
77
<meta charset="utf-8">
88
<link href="{% static 'css/header.css' %}">
9+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.6.0/css/all.min.css">
910
<script defer data-domain="boost.org" src="https://plausible.io/js/script.manual.js"></script>
1011
<script src="{% static 'js/boost-gecko/main.C3hPHS6-.js' %}" defer></script>
1112
{% if canonical_uri %}<link rel="canonical" href="{{ canonical_uri }}">{% endif %}
@@ -217,6 +218,23 @@
217218
margin-left: 300px;
218219
}
219220
}
221+
.version_alert {
222+
background-color: hsla(53,98%,77%,.7);
223+
border-radius: 0.125rem;
224+
font-family: ui-sans-serif,system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Arial,Noto Sans,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol,Noto Color Emoji;
225+
font-size: 1rem !important;
226+
margin-bottom: 0.75rem !important;
227+
padding: 0.5rem !important;
228+
text-align: center;
229+
}
230+
.version_alert p {
231+
text-align: center;
232+
margin: 0px;
233+
}
234+
.version_alert a {
235+
font-weight: 600;
236+
text-decoration-line: underline;
237+
}
220238
</style>
221239

222240
{% if no_wrapper %}

versions/models.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,12 @@ def display_name(self):
319319

320320
def __str__(self):
321321
return self.version
322+
323+
324+
def docs_path_to_boost_name(content_path):
325+
"""
326+
Convert a documentation content path to the Boost version name.
327+
e.g. "1_79_0/doc/html/accumulators.html" to "boost-1.79.0"
328+
"""
329+
result = re.sub(r"^([_0-9]+)(/\S+)", r"boost-\1", content_path)
330+
return result.replace("_", ".")

0 commit comments

Comments
 (0)