Skip to content

Commit f466547

Browse files
Redirect pages: Add 404 pages (#121137)
Adds a special error mode if the path matches the required format but the package is wrong: <img width="711" alt="grafik" src="https://github.com/user-attachments/assets/3849e47c-1eae-4cf8-bdc3-c8e9211dd0c8"> And a generic 404 page otherwise <img width="672" alt="grafik" src="https://github.com/user-attachments/assets/55fc62ae-e7bd-4512-bb2c-8ecf26bb8613"> --------- Co-authored-by: Dilum Aluthge <dilum@aluthge.com>
1 parent f447bb2 commit f466547

File tree

2 files changed

+87
-30
lines changed

2 files changed

+87
-30
lines changed

.ci/generate_redirects.jl

Lines changed: 84 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,52 @@ function package_path(args...)
2727
joinpath("webroot", "packages", "redirect_to_repo", args...)
2828
end
2929

30+
function style_block(backgroundcolor, color)
31+
"""
32+
<style>
33+
body {
34+
display: flex;
35+
justify-content: center;
36+
align-items: center;
37+
min-height: 100vh;
38+
margin: 0;
39+
font-family: Arial, sans-serif;
40+
background-color: #f9f9f9;
41+
}
42+
.centered-div {
43+
border: 3px solid $color;
44+
background-color: $backgroundcolor;
45+
border-radius: 10px;
46+
padding: 20px;
47+
margin: 20px;
48+
text-align: center;
49+
color: #333;
50+
max-width: 30em;
51+
word-wrap: break-word;
52+
}
53+
.centered-div a {
54+
color: $color;
55+
font-weight: bold;
56+
}
57+
</style>
58+
"""
59+
end
60+
3061
function create_redirect_page(; name, path)
3162
repo = get_repo(path)
3263
host = get_host(repo)
64+
jlname = name * ".jl"
3365
should_redirect = known_host(host)
3466
meta_redirection = should_redirect ? """<meta http-equiv="refresh" content="0; url=$repo">""" : ""
3567
message = if should_redirect
36-
"""Redirecting to $name...<br><br>Click the link below if you are not redirected automatically to the registered repository for the Julia package $name<br><br><a href="$repo" rel="nofollow">$repo</a>"""
68+
"""Redirecting to $jlname...<br><br>Click the link below if you are not redirected automatically to the registered repository for the Julia package $jlname<br><br><a href="$repo" rel="nofollow">$repo</a>"""
3769
else
38-
"""Click the link below to go to the registered repository for the Julia package $name<br><br><a href="$repo" rel="nofollow">$repo</a>"""
70+
"""Click the link below to go to the registered repository for the Julia package $jlname<br><br><a href="$repo" rel="nofollow">$repo</a>"""
3971
end
4072
title = if should_redirect
41-
"Redirecting to $name..."
73+
"Redirecting to $jlname..."
4274
else
43-
"Confirm redirect to $name"
75+
"Confirm redirect to $jlname"
4476
end
4577

4678
open(package_path(name * ".html"), "w") do io
@@ -52,32 +84,7 @@ function create_redirect_page(; name, path)
5284
<meta name="viewport" content="width=device-width, initial-scale=1.0">
5385
<title>$title</title>
5486
$meta_redirection
55-
<style>
56-
body {
57-
display: flex;
58-
justify-content: center;
59-
align-items: center;
60-
min-height: 100vh;
61-
margin: 0;
62-
font-family: Arial, sans-serif;
63-
background-color: #f9f9f9;
64-
}
65-
.centered-div {
66-
border: 3px solid #9558B2;
67-
background-color: #f8e9ff;
68-
border-radius: 10px;
69-
padding: 20px;
70-
margin: 20px;
71-
text-align: center;
72-
color: #333;
73-
max-width: 30em;
74-
word-wrap: break-word;
75-
}
76-
.centered-div a {
77-
color: #9558B2;
78-
font-weight: bold;
79-
}
80-
</style>
87+
$(style_block("#f8e9ff", "#9558B2"))
8188
</head>
8289
<body>
8390
<div class="centered-div">
@@ -89,13 +96,60 @@ function create_redirect_page(; name, path)
8996
end
9097
end
9198

99+
function create_404_page()
100+
open(joinpath("webroot", "404.html"), "w") do io
101+
write(io, """
102+
<!DOCTYPE html>
103+
<html lang="en">
104+
<head>
105+
<meta charset="UTF-8">
106+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
107+
<title>Page not found</title>
108+
$(style_block("#ffcbc8", "#CB3C33"))
109+
</head>
110+
<body>
111+
<div class="centered-div">
112+
<p>No page exists here.<br><br>Redirection pages for registered Julia packages follow the format packages/redirect_to_repo/SomePackage.</p>
113+
</div>
114+
115+
<script>
116+
// Get the current URL path
117+
const urlPath = window.location.pathname;
118+
119+
// Define the regex pattern to match the URL structure
120+
const pattern = /\\/packages\\/redirect_to_repo\\/([^\\/]+?)(\\.html)?\$/;
121+
122+
// Check if the URL matches the pattern
123+
const match = urlPath.match(pattern);
124+
125+
if (match) {
126+
const packageName = match[1]; // Extract the package name
127+
const messageElement = document.querySelector(".centered-div p");
128+
// Update the paragraph text
129+
messageElement.innerHTML = `\
130+
There is no package \${packageName}.jl registered in the Julia General Registry.\
131+
<br><br>\
132+
Would you like to try searching <a href="https://github.com/search?q=\${packageName}.jl&type=repositories">GitHub</a>, \
133+
<a href="https://about.gitlab.com/search/?searchText=\${packageName}.jl">GitLab</a>, \
134+
<a href="https://www.google.com/search?q=\${packageName}.jl">Google</a>, \
135+
or <a href="https://duckduckgo.com/?q=\${packageName}.jl">DuckDuckGo</a> for it?`;
136+
}
137+
</script>
138+
</body>
139+
</html>
140+
""")
141+
end
142+
return
143+
end
144+
92145
function main()
93146
cd(joinpath(@__DIR__, "..")) do
94147
mkpath(package_path())
95148
packages_info = get_packages_info()
96149
for (; name, path) in packages_info
97150
create_redirect_page(; name, path)
98151
end
152+
create_404_page()
99153
end
100154
end
101155

.github/workflows/redirects_page.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ on:
99
- master
1010
paths:
1111
- '**/Package.toml' # avoids redeploys on version or compat changes
12+
- '.github/workflows/redirects_page.yml'
13+
- '.ci/generate_redirects.jl'
14+
1215

1316
jobs:
1417
build-and-deploy:

0 commit comments

Comments
 (0)