Skip to content

Commit

Permalink
website: create html redirects for archived rvspoc 2023 challenges (#141
Browse files Browse the repository at this point in the history
)

Signed-off-by: Ryan Tsien <ryan@kubuds.cn>
  • Loading branch information
bekcpear committed Jun 20, 2024
1 parent 1eaa26d commit 2370f74
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/deploy-website.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ jobs:
with:
submodules: recursive
fetch-depth: 0
- name: creating html redirections
run: ./redirections/create.sh
- uses: actions/setup-node@v4
with:
node-version: 22
Expand All @@ -68,7 +70,9 @@ jobs:
_do cp -a "${year_folder}/Website" "./public/${BASH_REMATCH[1]}"
fi
done < <(find ./archives -type d -name '2???')
_do find ./public -type f -exec sed -Ei "s@https://rvspoc.org@${BASE_URL}@" '{}' \;
if [[ $BASE_URL != "https://rvspoc.org" ]]; then
_do find ./public -type f -exec sed -Ei "s@https://rvspoc.org@${BASE_URL}@" '{}' \;
fi
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
Expand Down
6 changes: 5 additions & 1 deletion .github/workflows/test-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ jobs:
with:
submodules: recursive
fetch-depth: 0
- name: creating html redirections
run: ./redirections/create.sh
- uses: actions/setup-node@v4
with:
node-version: 22
Expand All @@ -59,4 +61,6 @@ jobs:
_do cp -a "${year_folder}/Website" "./public/${BASH_REMATCH[1]}"
fi
done < <(find ./archives -type d -name '2???')
_do find ./public -type f -exec sed -Ei "s@https://rvspoc.org@${BASE_URL}@" '{}' \;
if [[ $BASE_URL != "https://rvspoc.org" ]]; then
_do find ./public -type f -exec sed -Ei "s@https://rvspoc.org@${BASE_URL}@" '{}' \;
fi
20 changes: 20 additions & 0 deletions redirections/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Format:
# <from-path> -> <to-path>
# the from-path is relative to the root location.
# for the to-path:
# if staring with a '/', it's relative to the root location
# if staring with no '/', it's relative to the from-path

# challenges of 2023 archives
p2301/ -> /2023/p2301/
p2303/ -> /2023/p2303/
p2304/ -> /2023/p2304/
p2307/ -> /2023/p2307/
p2308/ -> /2023/p2308/
s2302/ -> /2023/s2302/
s2305/ -> /2023/s2305/
s2306/ -> /2023/s2306/
s2309/ -> /2023/s2309/
s2310/ -> /2023/s2310/
s2311/ -> /2023/s2311/
s2312/ -> /2023/s2312/
67 changes: 67 additions & 0 deletions redirections/create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env bash
#

set -e

CURDIR="$(dirname "$(realpath "$0")")"
PUBDIR="$(realpath "${CURDIR}/../public")"

RECORDS_FROM=()
RECORDS_TO=()

_do() {
echo ">>>" "$@" >&2
"$@"
}

_sed_Ei() {
if [[ $IS_BSD_SED == 0 ]]; then
_do sed -Ei "$@"
elif [[ $IS_BSD_SED == 1 ]]; then
_do sed -Ei '' "$@"
else
if sed --version &>/dev/null; then
IS_BSD_SED=0
else
IS_BSD_SED=1
fi
_sed_Ei "$@"
fi
}

read_config() {
local record
while read -r record; do
if [[ $record =~ ^[[:space:]]*# ]] || \
[[ $record =~ ^[[:space:]]*$ ]]; then
# skip comments and empty lines
continue
fi
if [[ $record =~ ^[[:space:]]*([^[:space:]]+)\ -\>\ ([^[:space:]]+)[[:space:]]*$ ]]; then
RECORDS_FROM+=( "${BASH_REMATCH[1]}" )
RECORDS_TO+=( "${BASH_REMATCH[2]}" )
else
echo "unrecognized record '$record', skip it.." >&2
continue
fi
done <"${CURDIR}/config"
}

create_one() {
local from="${1##/}" from_fp to="$2"
from_fp="${PUBDIR}/${from}"
_do mkdir -p "$from_fp"
_do cp "${CURDIR}/tmpl.html" "${from_fp%%/}/index.html"
if ! [[ $to =~ ^/ ]]; then
to="/${from%%/}/${to}"
fi
_sed_Ei "s@\@TO-PATH\@@${to}@g" "${from_fp%%/}/index.html"
}

echo "creating html redirections..."
read_config
for (( i=0; i<${#RECORDS_FROM[@]}; i++ )); do
create_one "${RECORDS_FROM[i]}" "${RECORDS_TO[i]}"
done

# vim:sw=8:ts=8:noexpandtab
15 changes: 15 additions & 0 deletions redirections/tmpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="0;url=https://rvspoc.org@TO-PATH@">
<title>Redirecting...</title>
<script type="text/javascript">
window.location.href = "https://rvspoc.org@TO-PATH@";
</script>
</head>
<body>
<p>如若未自动跳转,请点击<a href="https://rvspoc.org@TO-PATH@">此链接</a></p>
<p>If you are not redirected automatically, please follow this <a href="https://rvspoc.org@TO-PATH@">link</a>.</p>
</body>
</html>

0 comments on commit 2370f74

Please sign in to comment.