From d52106454cb10a448ecc486bee1d8b9b92ab16cc Mon Sep 17 00:00:00 2001
From: EuAndreh <eu@euandre.org>
Date: Mon, 10 Apr 2023 17:58:46 -0300
Subject: v2: <link /> to rel="{prev,next}" in <head> of collection items

---
 TODOs.md                                   |   1 -
 v2/.gitignore                              |   3 +
 v2/dynamic.mk                              |   5 ++
 v2/src/bin/html                            |  16 +++++
 v2/src/bin/makemake                        |  14 +++-
 v2/src/bin/series                          | 102 +++++++++++++++++++++++++++++
 v2/src/content/en/pastebin/guix-shebang.md |  15 +++++
 v2/src/lib/preamble.html                   |   2 +
 8 files changed, 155 insertions(+), 3 deletions(-)
 create mode 100755 v2/src/bin/series
 create mode 100644 v2/src/content/en/pastebin/guix-shebang.md

diff --git a/TODOs.md b/TODOs.md
index 9068ad8..22e3a8c 100644
--- a/TODOs.md
+++ b/TODOs.md
@@ -539,5 +539,4 @@ FIXMEs:
 - render slides
 - sitemap
 - shrink CSS
-- link to next and/or previous in <head>
 - build makemake output in parallel
diff --git a/v2/.gitignore b/v2/.gitignore
index fe51060..84d3bd9 100644
--- a/v2/.gitignore
+++ b/v2/.gitignore
@@ -15,8 +15,11 @@
 /src/content/**/*.html
 /src/content/**/*.extrahtml
 /src/content/**/*.i18n
+/src/content/**/*.md.next
+/src/content/**/*.md.prev
 /src/content/**/*.md.tmp
 /src/content/**/*.htmlbody
+/src/content/**/*.series
 /src/content/**/*.snippets
 /src/content/**/*.html.*.txt
 /src/content/**/*.sortdata
diff --git a/v2/dynamic.mk b/v2/dynamic.mk
index ac06c20..ef7d24c 100644
--- a/v2/dynamic.mk
+++ b/v2/dynamic.mk
@@ -61,6 +61,9 @@ $(all-generated.index): src/lib/generated.conf
 $(all-generated.xml):
 	feed $@
 
+$(all-generated.series):
+	series $@
+
 i18n-index.sentinel: po/i18n.mappings
 	i18n < po/i18n.mappings
 	touch $@
@@ -78,6 +81,8 @@ clean:
 		src/content/*/*.i18n          src/content/*/*/*.i18n           \
 		src/content/*/*.sortdata      src/content/*/*/*.sortdata       \
 		src/content/*/feed.*.xml      src/content/*/*/feed.*.xml       \
+		src/content/*/*.md.next       src/content/*/*/*.md.next        \
+		src/content/*/*.md.prev       src/content/*/*/*.md.prev        \
 		src/lib/generated.conf src/lib/generated.*.conf po/*.mo        \
 
 
diff --git a/v2/src/bin/html b/v2/src/bin/html
index 578c539..a54ab68 100755
--- a/v2/src/bin/html
+++ b/v2/src/bin/html
@@ -213,6 +213,22 @@ emit_body() {
 		warn_duplicate_ids
 }
 
+if [ -r "$FILENAME".prev ]; then
+	collection_head_prev_html="$(
+		printf '    <link rel="prev"       type="text/html"            href="%s" />' \
+			"$(url-for < "$FILENAME".prev)"
+	)"
+	export collection_head_prev_html
+fi
+
+if [ -r "$FILENAME".next ]; then
+	collection_head_next_html="$(
+		printf '    <link rel="next"       type="text/html"            href="%s" />' \
+			"$(url-for < "$FILENAME".next)"
+	)"
+	export collection_head_next_html
+fi
+
 envsubst < src/lib/preamble.html
 emit_body | tee "${FILENAME%.*}.htmlbody"
 envsubst < src/lib/postamble.html
diff --git a/v2/src/bin/makemake b/v2/src/bin/makemake
index 3f4679d..22e92c9 100755
--- a/v2/src/bin/makemake
+++ b/v2/src/bin/makemake
@@ -136,6 +136,10 @@ for lang in $(langs); do
 
 		echo "$CONTENT_PREFIX/$lang/${coll_path}index.extrahtml:  $CONTENT_PREFIX/$lang/${coll_path}index.html"
 		echo "$CONTENT_PREFIX/$lang/${coll_path}index.extrafeeds: $CONTENT_PREFIX/$lang/${coll_path}feed.xml"
+
+		echo "$CONTENT_PREFIX/$lang/${coll_path}index.series: \$($c.$lang.conf)"
+		echo "\$($c.$lang.html): $CONTENT_PREFIX/$lang/${coll_path}index.series"
+
 		printf '\n\n'
 	done
 
@@ -150,6 +154,11 @@ for lang in $(langs); do
 		varlist "all-generated.$lang$e"
 	done
 
+	collections |
+		xargs -I% coll2path "$lang" % |
+		sed "s|^\(.*\)\$|$CONTENT_PREFIX/$lang/\1index.series|" |
+		varlist "all-generated.$lang.series"
+
 	collections |
 		xargs -I% coll2path "$lang" % |
 		sed "s|^\(.*\)\$|$CONTENT_PREFIX/$lang/\1index.categories|" |
@@ -175,10 +184,11 @@ for lang in $(langs); do
 		sed "s|^\(.*\)\$|$CONTENT_PREFIX/$lang/\1feed.xml|" |
 		varlist "all-generated.$lang.xml"
 
-	extensions '.categories' '.extrahtml' '.extrafeeds' '.index' '.xml' |
+	extensions '.series' '.categories' '.extrahtml' '.extrafeeds' '.index' '.xml' |
 		sed "s|^\(.*\)\$|\$(all-generated.$lang\1)|" |
 		varlist "all-generated.$lang"
 
+
 	printf '\n'
 done
 
@@ -201,7 +211,7 @@ printf 'svgs = $(svgs.uncolored:.uncolored=)\n\n'
 } > "$CONTENT_PREFIX"/.gitignore
 
 
-for e in $(extensions '.categories' '.extrahtml' '.extrafeeds' '.index' '.xml'); do
+for e in $(extensions '.series' '.categories' '.extrahtml' '.extrafeeds' '.index' '.xml'); do
 	langs |
 		sed "s|^\(.*\)\$|\$(all-generated.\1$e)|" |
 		varlist "all-generated$e"
diff --git a/v2/src/bin/series b/v2/src/bin/series
new file mode 100755
index 0000000..6712368
--- /dev/null
+++ b/v2/src/bin/series
@@ -0,0 +1,102 @@
+#!/bin/sh
+set -eu
+
+usage() {
+	cat <<-'EOF'
+		Usage:
+		  series FILENAME
+		  series -h
+	EOF
+}
+
+help() {
+	cat <<-'EOF'
+		Options:
+		  -h, --help    show this message
+
+		  FILENAME      the sentinel file to be touched
+
+
+		  Enumerate and sort all items of a collection type, and notify
+		  them about their '<link rel="next" ... />' and '<link rel="prev" .../>.
+		  The collection type and language are inferred by FILENAME.
+
+		  The "notifying" part is done via a "$TARGET.next" file, that
+		  is, a ".next" file is created to notify "$TARGET" about which
+		  item is next to it via the content of the file.  The same
+		  applies for ".prev" files.
+
+
+		Examples:
+
+		  Generate the ".next" and ".prev" files for english pastebins:
+
+		    $ index src/en/pastebin/index.series
+	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")"
+
+
+find "$DIR"/*.sortdata 2>/dev/null |
+		sort -n |
+		xargs cat |
+		awk '
+		BEGIN { split("", items) }
+		{ items[NR] = $0 }
+		END {
+			first = 1
+			last = length(items)
+			for (i in items) {
+				item = items[i]
+				if (i != first) {
+					file = item ".prev"
+					prev = items[i-1]
+					print prev > file
+				}
+				if (i != last) {
+					file = item ".next"
+					nextt = items[i+1]
+					print nextt > file
+				}
+			}
+		}
+		'
+
+touch "$FILENAME"
diff --git a/v2/src/content/en/pastebin/guix-shebang.md b/v2/src/content/en/pastebin/guix-shebang.md
new file mode 100644
index 0000000..5be9ada
--- /dev/null
+++ b/v2/src/content/en/pastebin/guix-shebang.md
@@ -0,0 +1,15 @@
+---
+
+title: Guix shebang
+
+date: 2020-02-14
+
+---
+
+```shell
+#!/usr/bin/env -S guix environment --ad-hoc bash -- bash
+set -Eeuo pipefail
+cd "$(dirname "${BASH_SOURCE[0]}")"
+
+pwd
+```
diff --git a/v2/src/lib/preamble.html b/v2/src/lib/preamble.html
index f08eee8..17efced 100644
--- a/v2/src/lib/preamble.html
+++ b/v2/src/lib/preamble.html
@@ -8,6 +8,8 @@
     <link rel="canonical"  type="text/html"            href="$url_absolute" />
 $translations_head_html
 $collection_head_html
+$collection_head_prev_html
+$collection_head_next_html
 
     <title>$title_html</title>
 
-- 
cgit v1.2.3