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

93 lines
2.3 KiB
Bash
Raw Normal View History

2021-04-27 13:02:02 +00:00
#!/usr/bin/env zsh
cd "$(git rev-parse --show-toplevel)" || exit 1
# Directory
webdir="_site"
postsdir="$webdir/posts"
indexfile="$webdir/index.html"
2021-05-08 11:33:14 +00:00
indexdir=".cache/rss"
2021-04-27 13:02:02 +00:00
2021-04-27 22:22:16 +00:00
# maximal number of articles to put in the index homepage
maxarticles=1000
2021-04-27 13:02:02 +00:00
# HTML Accessors (similar to CSS accessors)
dateaccessor='.yyydate'
# title and keyword shouldn't be changed
titleaccessor='title'
keywordsaccessor='meta[name=keywords]::attr(content)'
formatdate() {
# format the date for RSS
local d="$1"
# echo "DEBUG DATE: $d" >&2
LC_TIME=en_US date --date $d +'%a, %d %b %Y %H:%M:%S %z'
}
finddate(){ < $1 hxselect -c $dateaccessor | sed 's/\[//g;s/\]//g;s/ .*$//' }
findtitle(){ < $1 hxselect -c $titleaccessor }
findkeywords(){ < $1 hxselect -c $keywordsaccessor | sed 's/,/ /g' }
2021-05-21 17:36:05 +00:00
mktaglist(){
2021-04-27 13:02:02 +00:00
for keyword in $*; do
2021-05-25 08:25:11 +00:00
printf "<span class=\"tag\">%s</span>" $keyword
done | sed 's#><#>, <#g'
2021-04-27 13:02:02 +00:00
}
autoload -U colors && colors
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
# building the body
2021-05-25 14:27:58 +00:00
dateaccessor='.pubDate'
finddate(){ < $1 hxselect -c $dateaccessor }
previousyear=""
for fic in $indexdir/**/*.index; do
d=$(finddate $fic)
echo "${${fic:h}:t} [$d]"
cp $fic $tmpdir/$d-${${fic:h}:t}.index
done
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)
2021-04-27 13:02:02 +00:00
echo "${fic:t}"
2021-05-25 14:27:58 +00:00
year=$( echo "$d" | perl -pe 's#(\d{4})-.*#$1#')
2021-04-27 22:22:16 +00:00
if (( year != previousyear )); then
echo $year
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
2021-05-25 08:25:11 +00:00
title="Y"
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]"