aboutsummaryrefslogblamecommitdiff
path: root/v2/src/bin/index
blob: 36750b3332ccbd493ebe026d6654b9fed3f12f07 (plain) (tree)































































































































                                                                                             
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  collection FILENAME
		  collection -h
	EOF
}

help() {
	cat <<-'EOF'
		Options:
		  -h, --help    show this message

		  FILENAME      the target collection HTML page to be generated


		  Generate FILENAME as a collection index.  The collection type
		  and language are inferred by the name of FILENAME.


		Examples:

		  Generate an index.html entry for english pastebins:

		    $ collection src/en/pastebins/index.html
	EOF
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

while getopts 'h' flag; do
	case "$flag" in
		h)
			usage
			help
			exit
			;;
		*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

. src/lib.sh

FILENAME="${1:-}"
eval "$(assert_arg "$FILENAME" 'FILENAME')"
DIR="$(dirname "$FILENAME")"


l="$(lang-for "$FILENAME")"
. src/lib/base.conf
. src/lib/generated.conf
# shellcheck source=/dev/null
. src/lib/generated."$l".conf
# shellcheck source=/dev/null
. src/lib/base."$l".conf

url_part="$(printf '%s' "${FILENAME#"$CONTENT_PREFIX"/}" | sed "s|\.md$|.html|")"
url_absolute="$(url-for "$url_part" | absolute)"
collection="$(collection-for "$FILENAME")"
feed_url="$(url-for "${DIR#"$CONTENT_PREFIX"/}"/feed.xml)"
by_category_url="$(url-for "${DIR#"$CONTENT_PREFIX"/}/${by_category_url_part:?}")"
title_html="$(eval "echo \"\$index_${collection}_title\"" | htmlesc)"
index_recent_title_html="$(eval "echo \"\$index_recent_${collection}_title\"" | htmlesc)"
index_category_title_html="$(eval "echo \"\$index_category_${collection}_title\"" | htmlesc)"
export url_absolute feed_url by_category_url title_html index_recent_title_html \
	index_category_title_html



mkdir -p "$DIR"
{
	cat src/lib/preamble.html src/lib/index-preamble.html | envsubst
	find "$DIR"/*.sortdata 2>/dev/null |
		sort -nr |
		xargs cat |
		sed 's|\.md$|.indexentry|' |
		xargs cat
	cat src/lib/index-postamble.html src/lib/postamble.html | envsubst
} > "$FILENAME"


CATEGORY_FILENAME="$CONTENT_PREFIX$by_category_url"
echo "$CATEGORY_FILENAME" > "${FILENAME%.html}.extrahtml"

url_absolute="$(absolute "$by_category_url")"
title_html="$(eval "echo \"\$index_category_${collection}_title\"" | htmlesc)"
export url_absolute title_html


DIR="$(dirname "$CATEGORY_FILENAME")"
mkdir -p "$DIR"
{
	envsubst < src/lib/preamble.html
	while read -r category; do
		feed_url="$(url-for "${DIR#"$CONTENT_PREFIX"/}/feed.$category.xml")"
		index_recent_title_html="$category"
		export category feed_url index_recent_title_html

		envsubst < src/lib/category-header.html
		echo '        <ul>'
		< "$DIR/$category.category" \
			sed 's|\.md$|.categoryentry|' |
			xargs cat
		echo '        </ul>'
	done < "$DIR"/index.categories
	envsubst < src/lib/postamble.html
} > "$CATEGORY_FILENAME"