her.esy.fun/engine/mk-index.sh

64 lines
1.6 KiB
Bash
Raw Permalink Normal View History

2021-04-27 13:02:02 +00:00
#!/usr/bin/env zsh
2021-05-25 15:43:43 +00:00
autoload -U colors && colors
2021-04-27 13:02:02 +00:00
cd "$(git rev-parse --show-toplevel)" || exit 1
2021-08-21 15:19:30 +00:00
source ./engine/envvars.sh
2021-04-27 13:02:02 +00:00
# Directory
webdir="_site"
indexfile="$webdir/index.html"
2021-05-08 11:33:14 +00:00
indexdir=".cache/rss"
2021-04-27 13:02:02 +00:00
tmpdir=$(mktemp -d)
2021-04-27 22:22:16 +00:00
2021-04-27 13:02:02 +00:00
echo "Publishing"
2021-04-27 22:22:16 +00:00
2021-05-25 14:27:58 +00:00
dateaccessor='.pubDate'
finddate(){ < $1 hxselect -c $dateaccessor }
2021-05-25 15:43:43 +00:00
# generate files with <DATE>-<FILENAME>.index
2021-05-25 14:27:58 +00:00
for fic in $indexdir/**/*.index; do
d=$(finddate $fic)
echo "${${fic:h}:t} [$d]"
cp $fic $tmpdir/$d-${${fic:h}:t}.index
done
2021-05-25 15:43:43 +00:00
# for every post in reverse order
# generate the body (there is some logic to group by year)
2021-04-27 22:22:16 +00:00
previousyear=""
2021-05-25 14:27:58 +00:00
for fic in $(ls $tmpdir/*.index | sort -r); do
d=$(finddate $fic)
year=$( echo "$d" | perl -pe 's#(\d{4})-.*#$1#')
2021-04-27 22:22:16 +00:00
if (( year != previousyear )); then
if (( previousyear > 0 )); then
echo "</ul>" >> $tmpdir/index
fi
previousyear=$year
echo "<h3 name=\"${year}\" >${year}</h3><ul>" >> $tmpdir/index
fi
2021-04-27 13:02:02 +00:00
cat $fic >> $tmpdir/index
done
2021-05-25 08:25:11 +00:00
echo "</ul>" >> $tmpdir/index
2021-04-27 13:02:02 +00:00
2022-09-28 18:05:46 +00:00
title="Home"
2021-05-25 08:25:11 +00:00
description="Most recent articles"
2021-04-27 13:02:02 +00:00
author="Yann Esposito"
2021-04-27 22:22:16 +00:00
body=$(< $tmpdir/index)
date=$(LC_TIME=en_US date +'%Y-%m-%d')
2021-05-21 17:36:05 +00:00
# A neat trick to use pandoc template within a shell script
2021-04-27 22:22:16 +00:00
# the pandoc templates use $x$ format, we replace it by just $x
# to be used with envsubst
2021-05-25 08:25:11 +00:00
template=$(< templates/index.html | \
2021-05-09 19:41:34 +00:00
sed 's/\$\(header-includes\|table-of-content\)\$//' | \
2021-05-09 10:11:33 +00:00
sed 's/\$if.*\$//' | \
perl -pe 's#(\$[^\$]*)\$#$1#g' )
2021-04-27 13:02:02 +00:00
{
2021-04-27 22:22:16 +00:00
export title
export author
export description
export date
export body
echo ${template} | envsubst
2021-04-27 13:02:02 +00:00
} > "$indexfile"
rm -rf $tmpdir
2021-05-08 08:07:10 +00:00
echo "* HTML INDEX [done]"