arcnyxx.net.git
snail.sh
#!/bin/sh
# snail - markdown compiler
# Copyright (C) 2024 ArcNyxx <me@arcnyxx.net>
# see LICENCE file for licensing information
get() {
printf "%s\n" "${2#??}" | cut "-d${3:- }" "-f$1"
}
read -r TITLE
read -r SUBTITLE
read -r META
read -r DATE
[ "$DATE" ] && { DATE="<p><time>$DATE</time></p>" ; read -r NULL ; } || DATE=''
# sed command removes html tags and contents
printf "`cat head.html`\n" \
"`./snail "$META" | sed 's|<[^>]*>||g'`" \
"`./snail "$TITLE" | sed 's|<[^>]*>||g'`" \
"`./snail "$TITLE"`" "`./snail "$SUBTITLE"`" "$DATE"
MODE=
while IFS= read -r LINE ; do
if [ -z "$LINE" ] ; then
echo '</section>\n<section>'
elif [ "${LINE#\ !}" != "$LINE" ] ; then
echo "<h1>`./snail "${LINE#??}"`</h1>"
elif [ "${LINE#\ \?}" != "$LINE" ] ; then
echo "<h2>`./snail "${LINE#??}"`</h2>"
elif [ "${LINE#\ #}" != "$LINE" ] ; then
printf "%s %s\n" "<img src='`get 1 "$LINE"`'" \
"alt='`./snail \`get 2 "$LINE"\``'>"
elif [ "${LINE#\ ^}" != "$LINE" ] ; then
echo '<video controls>'
printf "%s %s\n" "<source src='`get 1 "$LINE"`'" \
"type='video/mp4' alt='`./snail \`get 2 "$LINE"\``'>"
echo '</video>'
elif [ "${LINE#\ \~}" != "$LINE" ] ; then
printf "%s %s\n" "<object data='`get 1 "$LINE"`'" \
"type='application/pdf' height='80%'>"
printf "%s%s\n" "<a href='`get 1 "$LINE"`'>" \
"`./snail \`get 2 "$LINE"\``</a>"
echo '</object>'
elif [ "${LINE#\ \ }" != "$LINE" ] ; then
if [ "$MODE" != '</ul>' ] ; then
[ "$MODE" ] && echo "$MODE"
echo "<ul class='l'>"
MODE='</ul>'
fi
echo "<li>`./snail "${LINE#??}"`</li>"
elif [ "${LINE#\ -}" != "$LINE" ] ; then
if [ "$MODE" != '</ul>' ] ; then
[ "$MODE" ] && echo "$MODE"
echo '<ul>'
MODE='</ul>'
fi
echo "<li>`./snail "${LINE#??}"`</li>"
elif [ "${LINE#\ \`}" != "$LINE" ] ; then
LINE="${LINE#??}"
if [ "$MODE" != '</code></pre>' ] ; then
if [ "`printf '%s' "$LINE" | head -c 1`" = '\' \
-a "`printf '%s' "${LINE#??}" | \
head -c 1`" != '\' ] ; then
CLASS=" class='`printf '%s' "${LINE#?}" \
| head -c 1`'"
LINE="${LINE#??}"
elif [ "`printf '%s' "$LINE" | head -c 2`" \
= '\\' ] ; then
LINE="${LINE#?}"
fi
printf "%s" "<pre><code$CLASS>"
MODE='</code></pre>'
fi
printf "%s\n" "`./snail "$LINE"`"
elif [ "${LINE#\ \$}" != "$LINE" ] ; then
printf '<article>\n<div>\n'
printf "%s%s\n" "<h1><a href='`get 1 "$LINE" \|`'>" \
"`get 3 "$LINE" \|`</a></h1>"
printf '</div>\n<div>\n'
printf "%s\n" "<p>`get 4 "$LINE" \|`</p>"
printf "%s\n" "<p>(<time>`get 2 "$LINE" \|`</time>)</p>"
printf '</div>\n</article>\n'
else
if [ "$MODE" ] ; then
echo "$MODE"
MODE=
fi
printf "%s\n" "<p>`./snail "$LINE"`</p>"
fi
done
cat foot.html