commit 39d4a040e1e279cabe59ccc260daf4b5b4056f82
parent 6f844f85363f4129187e79aa9fbb279aae8eefc2
Author: Jake Bauer <jbauer@paritybit.ca>
Date: Mon, 14 Feb 2022 22:55:02 -0500
Add command to generate atom feed from posts
Diffstat:
M | sbs | | | 80 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------- |
1 file changed, 63 insertions(+), 17 deletions(-)
diff --git a/sbs b/sbs
@@ -5,31 +5,16 @@
# Copyright (C) 2022 Jake Bauer
# Licensed under the terms of the ISC License, see LICENSE for details.
-# TODO: Creation of Atom feed from blog posts
-
set -o errexit
set -o nounset
-if [ "$1" = "new" ]; then
- if [ "$2" = "page" ]; then
- printf "Title: \nAuthor: \nDate: \nSummary: \n\n# [%%title]\n\n**Author:** [%%author] | **Published:** [%%date]\n\n" > "content/$3"
- elif [ "$2" = "site" ]; then
- mkdir -p "$3/content/" "$3/static/" "$3/templates/"
- touch "$3/static/style.css"
- printf "siteName = $3\nlanguageCode = en\nbuildOptions = -Thtml --html-no-skiphtml --html-no-escapehtml\n" > "$3/config.ini"
- printf '<!DOCTYPE html>\n<html lang="">\n<head>\n\t<meta charset="utf-8">\n\t<meta name="viewport" content="width=device-width, initial-scale=1.0">\n\t<meta name="description" content="">\n\t<link rel="stylesheet" href="/style.min.css">\n\t<link rel="alternate" type="application/rss+xml" title="RSS feed" href="/feed.xml">\n\t<title></title>\n</head>\n<body>\n\t<header></header>\n\t<main>\n' > "$3/templates/header.html"
- printf '\t</main>\n\t<footer></footer>\n</body>\n</html>\n' > "$3/templates/footer.html"
- fi
- exit 0
-fi
-
-# Check that all the required programs are installed
if [ ! -x "$(command -v lowdown)" ]; then
echo "The program 'lowdown' is needed but was not found."
exit 1
fi
-options="buildOptions siteName languageCode"
+# Parse the config file
+options="siteURL siteName blogDir languageCode buildOptions"
for key in $options; do
value=$(grep "$key" config.ini | cut -d'=' -f2 | xargs)
if [ -n "$value" ]; then
@@ -40,6 +25,67 @@ for key in $options; do
fi
done
+# Create a new page or a new site
+if [ "$1" = "new" ]; then
+ if [ "$2" = "page" ]; then
+ printf "Title: \nAuthor: \nDate: \nSummary: \n\n# [%%title]\n\n**Author:** [%%author] | **Published:** [%%date]\n\n" > "content/$3"
+ printf "Created: content/%s" "$3"
+ elif [ "$2" = "site" ]; then
+ mkdir "$3/content/" "$3/static/" "$3/templates/"
+ touch "$3/static/style.css"
+ printf "siteURL = https://example.com\nsiteName = $3\nblogDir = blog\nlanguageCode = en\nbuildOptions = -Thtml --html-no-skiphtml --html-no-escapehtml\n" > "$3/config.ini"
+ printf '<!DOCTYPE html>\n<html lang="">\n<head>\n\t<meta charset="utf-8">\n\t<meta name="viewport" content="width=device-width, initial-scale=1.0">\n\t<meta name="description" content="">\n\t<link rel="stylesheet" href="/style.min.css">\n\t<link rel="alternate" type="application/rss+xml" title="RSS feed" href="/feed.xml">\n\t<title></title>\n</head>\n<body>\n\t<header></header>\n\t<main>\n' > "$3/templates/header.html"
+ printf '\t</main>\n\t<footer></footer>\n</body>\n</html>\n' > "$3/templates/footer.html"
+ printf "Created: %s\n" "$3"
+ fi
+ exit 0
+# Construct a complete atom feed
+elif [ "$1" = "genfeed" ]; then
+ printf '<?xml version="1.0" encoding="utf-8"?>\n' > static/feed.xml
+ printf '<feed xmlns="http://www.w3.org/2005/Atom">\n' >> static/feed.xml
+ printf "\t<title>$siteName</title>\n" >> static/feed.xml
+ printf "\t<link href=\"$siteURL\" />\n" >> static/feed.xml
+ printf "\t<icon>/favicon.png</icon>\n" >> static/feed.xml
+ printf "\t<updated>$(date +"%Y-%m-%dT%H:%M:%S%:z")</updated>\n" >> static/feed.xml
+ printf "\t<id>$siteURL</id>\n" >> static/feed.xml
+ printf "\t<generator>sbs</generator>\n\n" >> static/feed.xml
+
+ numEntries=0
+ tmp=$(mktemp)
+ for file in $(find content/"$blogDir" -type f -name *.md); do
+ if [ -n "$(lowdown -X draft "$file" 2>/dev/null)" ]; then
+ continue
+ fi
+ printf "%s %s\n" "$(date -d $(lowdown -X date "$file") +"%s")" \
+ "$file" >> "$tmp"
+ done
+ for file in $(sort -rn "$tmp" | cut -d' ' -f2); do
+ fileName=$(basename "$file" .md).html
+ subDir=$(dirname "$file" | sed "s/^content\///")
+
+ title=$(lowdown -X title "$file")
+ author=$(lowdown -X author "$file")
+ date=$(lowdown -X date "$file")
+
+ printf "\t<entry>\n" >> static/feed.xml
+ printf "\t\t<title>$title</title>\n" >> static/feed.xml
+ printf "\t\t<author><name>$author</name></author>\n" >> static/feed.xml
+ printf "\t\t<link>$siteURL/$subDir/$fileName</link>\n" >> static/feed.xml
+ printf "\t\t<id>$siteURL/$subDir/$fileName</id>\n" >> static/feed.xml
+ printf "\t\t<updated>$(date -d "$date" +"%Y-%m-%dT%H:%M:%S%:z")</updated>\n" >> static/feed.xml
+ printf "\t\t<content type=\"html\">\n%s\n\t\t</content>\n" "$(lowdown $buildOptions "$file")" >> static/feed.xml
+ printf "\t</entry>\n\n" >> static/feed.xml
+
+ numEntries=$((numEntries+1))
+ done
+
+ printf '</feed>\n' >> static/feed.xml
+ printf "Created: static/feed.xml with %s entries.\n" "$numEntries"
+ rm "$tmp"
+ exit 0
+fi
+
+# Build the pages given as arguments
for file in "$@"; do
if [ -d "$file" ]; then
"$0" "$file/*"