Skip to content

Instantly share code, notes, and snippets.

@eli-oat
Created January 8, 2023 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eli-oat/a52619aacc92edc76ada1e88f0b9e042 to your computer and use it in GitHub Desktop.
Save eli-oat/a52619aacc92edc76ada1e88f0b9e042 to your computer and use it in GitHub Desktop.
a very simple script to build a static wiki out of a directory of markdown files
#!/usr/bin/env bash
# requires pandoc
# coreutils
# uuidgen
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace
fi
SITENAME="wiki"
SITEURL="https://rad.wiki.cool"
SITEDESCRIPTION="a little wiki"
LASTUPDATE=$(<updated)
TIMESTAMP=$(gdate +"%s")
RFC822=$(gdate -R)
RSSHEADER='<?xml version="1.0" encoding="UTF-8"?>
<rss
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>
<![CDATA[ '${SITENAME}']]>
</title>
<link>'${SITEURL}'</link>
<atom:link href="'${SITEURL}'/feed.rss" rel="self" type="application/rss+xml"/>
<description>
<![CDATA[ '${SITEDESCRIPTION}' ]]>
</description>
'
RSSEND='
</channel>
</rss>'
build_logs() {
# echo -e "# Changelog\n\n$(cat changelog.md)" > changelog.md # FIXME, this doesn't work because I'm not resetting the changelog every update
pandoc changelog.md -o public/changelog.html
cd content
rm ../archive.md
echo -e "# Archive\n" > ../archive.md
for file in *;
do
archive_entry="- [${file%.*}](/${file%.*}.html)"
echo -e "$archive_entry" >> ../archive.md
done
cd ..
pandoc archive.md -o public/archive.html
}
build() {
cd content
i=0
rm ../public/feed.xml
touch ../public/feed.xml
echo -e "${RSSHEADER}" > ../public/feed.xml
for file in *;
do
file_last_updated=$(stat -f %m "$file")
if [ "$file_last_updated" -gt "$LASTUPDATE" ];
then
(( i++ ))
updated_at="- [${file%.*}](/${file%.*}.html), ${RFC822}"
echo -e "$updated_at\n$(cat ../changelog.md)" > ../changelog.md
pandoc "${file}" -o ../public/"${file%.*}".html
rss_entry='<item>
<title>
<![CDATA[ the page "'${file%.*}'" was updated! ]]>
</title>
<link>'${SITEURL}'/'${file%.*}.html'</link>
<guid>'${SITEURL}/${file%.*}.html#$(uuidgen)'</guid>
<pubDate>'${RFC822}'</pubDate>
<description>
<![CDATA[
<p><a href="'${SITEURL}/${file%.*}.html'">'${file%.*}'</a> updated!</p> ]]>
</description>
</item>'
echo -e "$rss_entry" >> ../public/feed.xml
fi
done
cd ..
echo -e "$RSSEND" >> public/feed.xml
if [ $i -gt 0 ];
then
build_logs "$@"
fi
echo "$TIMESTAMP" > updated
echo "updated" $i "items."
}
help_text() {
echo '
Usage: ./wiki.sh
-b(uild)....to (re)build the site
-h(elp).....to display this message
'
}
if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
help_text "$@"
exit
elif [[ "${1-}" =~ ^-*b(uild)?$ ]]; then
build "$@"
exit
fi
cd "$(dirname "$0")"
main() {
help_text "$@"
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment