#!/bin/sh -e
#
# Build the Alice Linux static website into public/.
# Run from the project root.
#
# Dependencies: cmark, curl
cd "$(dirname "$0")/.."
for dep in cmark curl; do
command -v "$dep" >/dev/null 2>&1 || {
printf 'Error: "%s" is required but not found in PATH.\n' "$dep" >&2
exit 1
}
done
rm -rf public
mkdir -p public
html_escape() {
# takes a single string argument, prints escaped to stdout
printf '%s' "$1" | sed 's/\&/\&/g;s/\</g;s/>/\>/g;s/"/\"/g'
}
port_version() {
grep '^version=' "$1/abuild" | cut -d = -f2- || true
}
port_release() {
grep '^release=' "$1/abuild" | cut -d = -f2- || true
}
port_depends() {
[ -f "$1/depends" ] || return 0
grep -Ev '^(#|$)' "$1/depends" | tr '\n' ' ' | sed 's/[[:space:]]*$//' || true
}
generate_ports_page() {
{
sed "s/@TITLE@/ports/g" files/header
echo "
Package ports generated from repos/core, repos/extra, and repos/community.
"
echo ""
echo ""
echo ""
echo ""
echo ""
echo ""
echo "
"
echo "0 / 0 ports
"
echo ""
echo "| repo | name | version | dependencies |
"
echo ""
for repo in core extra community; do
for port in repos/$repo/*; do
[ -f "$port/abuild" ] || continue
name=${port##*/}
version=$(port_version "$port")
release=$(port_release "$port")
depends=$(port_depends "$port")
[ "$release" ] && version=$version-$release
e_repo=$(html_escape "$repo")
e_name=$(html_escape "$name")
e_version=$(html_escape "$version")
e_depends=$(html_escape "$depends")
printf '| %s | %s | %s | %s |
\n' \
"$e_repo" \
"$e_repo" \
"$e_repo" \
"$e_name" \
"$e_name" \
"$e_version" \
"$e_depends"
done
done
echo ""
echo "
"
cat << 'EOF'
EOF
cat files/footer
} > public/ports.html
}
# copy static .html files (from repo root, readme, etc.) into public/
find . -type f -name "*.html" ! -path './public/*' | sed 's|^\./||' | while IFS= read -r i; do
dir=${i%/*}
file=${i##*/}
title=${dir##*/}
[ "$dir" = "$file" ] && {
title=home; dir=
}
mkdir -p "public/$dir"
printf 'copy html for %s...\n' "$i"
{
sed "s/@TITLE@/$title/g" files/header
cat "$i"
cat files/footer
} > "public/$dir/$file"
done
generate_ports_page
generate_commits_page() {
{
sed "s/@TITLE@/commits/g" files/header
cat << 'EOF'
Recent commits from codeberg.org/emmett1/alicelinux.
loading…
EOF
cat files/footer
} > public/commits.html
}
generate_commits_page
generate_download_page() {
{
sed "s/@TITLE@/download/g" files/header
listing=$(curl -sL --max-time 10 https://dl.alicelinux.org/ 2>/dev/null || true)
cat << 'EOF'
Download
Alice Linux installation images and rootfs tarballs. See the installation guide for setup instructions.
loading…
EOF
if [ -n "$listing" ]; then
echo "$listing" | sed -n '//,/<\/tbody>/p' | while IFS= read -r row; do
case $row in
*''*)
href=$(printf '%s' "$row" | sed 's/.*.*/\1/')
name=$(printf '%s' "$row" | sed 's/.*\([^<]*\)<.*/\1/')
size=$(printf '%s' "$row" | sed 's/.*| ]*>\([^<]*\)<.*/\1/')
date=$(printf '%s' "$row" | sed 's/.* | \([^<]*\)<.*/\1/')
type=$(printf '%s' "$row" | sed 's/.* | \([^<]*\)<.*/\1/')
[ "$name" = "../" ] && continue
url="https://dl.alicelinux.org/$href"
e_url=$(html_escape "$url")
e_name=$(html_escape "$name")
e_size=$(html_escape "$size")
e_date=$(html_escape "$date")
if [ "$type" = "Directory" ]; then
printf ' |
| %s/ | %s | %s |
\n' \
"$e_url" "$e_name" "$e_size" "$e_date"
else
printf '| %s | %s | %s |
\n' \
"$e_url" "$e_name" "$e_size" "$e_date"
fi
;;
esac
done
fi
cat << 'EOF'
EOF
cat files/footer
} > public/download.html
}
generate_download_page
# build docs index
cat docs/readme.md > docs/index.md
for f in docs/*.md; do
case $f in */readme.md|*/index.md) continue;; esac
title=$(head -n1 "$f" | sed "s/^#* *//")
file=${f##*/}
file_no_num=$(printf '%s' "$file" | sed 's/^[0-9][0-9]-//')
echo "- [$title](./${file_no_num%.md}.html)" >> docs/index.md
done
cat >> docs/index.md << 'EOF'
Documentation improvements are welcome. Visit the [docs directory](https://codeberg.org/emmett1/alicelinux/src/branch/main/docs) to contribute.
EOF
# convert markdown to html
doc_files=$(ls docs/*.md 2>/dev/null | sort | grep -v /readme.md | grep -v /index.md || true)
find . -type f -name "*.md" ! -path './public/*' | sed 's|^\./||' | while IFS= read -r i; do
dir=${i%/*}
file=${i##*/}
outfile=$(printf '%s' "$file" | sed 's/^[0-9][0-9]-//')
title=${dir##*/}
[ "$dir" = "$file" ] && {
title=home; dir=
}
mkdir -p "public/$dir"
printf 'generating html for %s...\n' "$i"
{
sed "s/@TITLE@/$title/g" files/header
case $i in docs/readme.md|docs/index.md) ;; *) case $i in docs/*) printf '<- back to docs\n\n';; esac; esac
cmark "$i"
case $i in docs/readme.md|docs/index.md) ;; *)
case $i in docs/*)
next=$(printf '%s\n' "$doc_files" | grep -FA1 "$i" | tail -n1)
if [ "$next" ] && [ "$next" != "$i" ]; then
next_title=$(head -n1 "$next" | sed 's/^#* *//')
next_html=$(printf '%s' "${next##*/}" | sed 's/^[0-9][0-9]-//')
printf '\n\n' "${next_html%.md}.html" "$next_title"
else
printf '\n<- back to docs\n'
fi
;;
esac
;;
esac
cat files/footer
} > "public/$dir/${outfile%.md}.html"
done
# move readme.html to index.html
find public -type f -name "readme.html" | while IFS= read -r i; do
mv -n "$i" "${i%/*}/index.html"
done
rm -f docs/index.md
if [ -d files ]; then
cp -ra files public/
fi
echo alicelinux.org > public/.domains
echo alicelinux.emmett1.my >> public/.domains
exit 0