aboutsummaryrefslogtreecommitdiff
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  index FILENAME
		  index -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:

		    $ index src/en/pastebin/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_absolute="$(url-for "$FILENAME" | 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_name:?}")"
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

feed_title="$(eval "echo \"\$feed_${collection}_title\"")"
collection_head_html="$(printf '    <link rel="alternate"  type="application/atom+xml" href="%s" title="%s" />' \
	"$feed_url" \
	"$(htmlesc "$feed_title")")"

translations_head_html="$(langs |
	awk -v lang="${lang:?}" -v coll="$collection" '$0 != lang {
		"coll2path " $0 " " coll    | getline coll_path
		"url-for " $0 "/" coll_path | getline url
		printf "    <link rel=\"alternate\"  type=\"text/html\"            href=\"%s\" hreflang=\"%s\" />\n",
			url, $0
	}'
)"
translations_body_html="$(cat <<-EOF
	        <ul class="translation-list">
	$(langs | awk -v coll="$collection" '{
		"coll2path " $0 " " coll    | getline coll_path
		"url-for " $0 "/" coll_path | getline url
		printf "          <li><a href=\"%s\">%s</a></li>\n", url, $0
	}')
	        </ul>
EOF
)"
export collection_head_html translations_head_html translations_body_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"