Skip to content

Commit

Permalink
Redirect pages: Add 404 pages (#121137)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jkrumbiegel and DilumAluthge authored Dec 18, 2024
1 parent f447bb2 commit f466547
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 30 deletions.
114 changes: 84 additions & 30 deletions .ci/generate_redirects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,52 @@ function package_path(args...)
joinpath("webroot", "packages", "redirect_to_repo", args...)
end

function style_block(backgroundcolor, color)
"""
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f9f9f9;
}
.centered-div {
border: 3px solid $color;
background-color: $backgroundcolor;
border-radius: 10px;
padding: 20px;
margin: 20px;
text-align: center;
color: #333;
max-width: 30em;
word-wrap: break-word;
}
.centered-div a {
color: $color;
font-weight: bold;
}
</style>
"""
end

function create_redirect_page(; name, path)
repo = get_repo(path)
host = get_host(repo)
jlname = name * ".jl"
should_redirect = known_host(host)
meta_redirection = should_redirect ? """<meta http-equiv="refresh" content="0; url=$repo">""" : ""
message = if should_redirect
"""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>"""
"""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>"""
else
"""Click the link below to go to the registered repository for the Julia package $name<br><br><a href="$repo" rel="nofollow">$repo</a>"""
"""Click the link below to go to the registered repository for the Julia package $jlname<br><br><a href="$repo" rel="nofollow">$repo</a>"""
end
title = if should_redirect
"Redirecting to $name..."
"Redirecting to $jlname..."
else
"Confirm redirect to $name"
"Confirm redirect to $jlname"
end

open(package_path(name * ".html"), "w") do io
Expand All @@ -52,32 +84,7 @@ function create_redirect_page(; name, path)
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>$title</title>
$meta_redirection
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
font-family: Arial, sans-serif;
background-color: #f9f9f9;
}
.centered-div {
border: 3px solid #9558B2;
background-color: #f8e9ff;
border-radius: 10px;
padding: 20px;
margin: 20px;
text-align: center;
color: #333;
max-width: 30em;
word-wrap: break-word;
}
.centered-div a {
color: #9558B2;
font-weight: bold;
}
</style>
$(style_block("#f8e9ff", "#9558B2"))
</head>
<body>
<div class="centered-div">
Expand All @@ -89,13 +96,60 @@ function create_redirect_page(; name, path)
end
end

function create_404_page()
open(joinpath("webroot", "404.html"), "w") do io
write(io, """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Page not found</title>
$(style_block("#ffcbc8", "#CB3C33"))
</head>
<body>
<div class="centered-div">
<p>No page exists here.<br><br>Redirection pages for registered Julia packages follow the format packages/redirect_to_repo/SomePackage.</p>
</div>
<script>
// Get the current URL path
const urlPath = window.location.pathname;
// Define the regex pattern to match the URL structure
const pattern = /\\/packages\\/redirect_to_repo\\/([^\\/]+?)(\\.html)?\$/;
// Check if the URL matches the pattern
const match = urlPath.match(pattern);
if (match) {
const packageName = match[1]; // Extract the package name
const messageElement = document.querySelector(".centered-div p");
// Update the paragraph text
messageElement.innerHTML = `\
There is no package \${packageName}.jl registered in the Julia General Registry.\
<br><br>\
Would you like to try searching <a href="https://github.com/search?q=\${packageName}.jl&type=repositories">GitHub</a>, \
<a href="https://about.gitlab.com/search/?searchText=\${packageName}.jl">GitLab</a>, \
<a href="https://www.google.com/search?q=\${packageName}.jl">Google</a>, \
or <a href="https://duckduckgo.com/?q=\${packageName}.jl">DuckDuckGo</a> for it?`;
}
</script>
</body>
</html>
""")
end
return
end

function main()
cd(joinpath(@__DIR__, "..")) do
mkpath(package_path())
packages_info = get_packages_info()
for (; name, path) in packages_info
create_redirect_page(; name, path)
end
create_404_page()
end
end

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/redirects_page.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
- master
paths:
- '**/Package.toml' # avoids redeploys on version or compat changes
- '.github/workflows/redirects_page.yml'
- '.ci/generate_redirects.jl'


jobs:
build-and-deploy:
Expand Down

0 comments on commit f466547

Please sign in to comment.