aboutsummaryrefslogtreecommitdiff
path: root/v2/src/bin/index
blob: 6212544c18bd5aa33b3badd800980180026acd51 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/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"