From 99e0eb4c2a96be9fe0e77f54d89c27937d0465a6 Mon Sep 17 00:00:00 2001 From: Konstantin Nazarov Date: Sun, 12 Sep 2021 17:16:30 +0100 Subject: [PATCH] Add rss feed --- Makefile | 5 ++- bin/rss.sh | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100755 bin/rss.sh diff --git a/Makefile b/Makefile index c9c27f7..af84264 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ PROJECTS_DST := $(patsubst content/projects/%,$(ODIR)/projects/%/index.html,$(PR POSTS_SRC := $(wildcard content/posts/*) POSTS_DST := $(patsubst content/posts/%,$(ODIR)/posts/%/index.html,$(POSTS_SRC)) -all: $(PAGES_DST) $(PROJECTS_DST) $(POSTS_DST) $(ODIR)/style.css $(ODIR)/index.html $(ODIR)/posts/index.html +all: $(PAGES_DST) $(PROJECTS_DST) $(POSTS_DST) $(ODIR)/style.css $(ODIR)/index.html $(ODIR)/posts/index.html $(ODIR)/rss.xml $(ODIR)/%/index.html: content/pages/%/note.md mkdir -p "$(dir $@)" @@ -25,6 +25,9 @@ $(ODIR)/posts/%/index.html: content/posts/%/note.md $(ODIR)/posts/index.html: $(POSTS_SRC) ./bin/toc.sh content/posts "/posts" | awk -f ./bin/markdown.awk | ./bin/page.sh Posts > "$@" +$(ODIR)/rss.xml: $(POSTS_SRC) + ./bin/rss.sh content/posts "https://knazarov.com" "/posts" > "$@" + $(ODIR)/index.html: content/index.md cat content/index.md | ./bin/mdpage.sh --notitle > "$@" diff --git a/bin/rss.sh b/bin/rss.sh new file mode 100755 index 0000000..ead0b66 --- /dev/null +++ b/bin/rss.sh @@ -0,0 +1,113 @@ +#!/bin/bash + +remove_nbsp() { + sed 's#\ # #g' +} + +date_rfc_822() { + date -j '+%a, %d %b %Y %H:%M:%S %z' +} + +date_to_rfc_822() { + date -f "%Y-%m-%dT%H:%M:%SZ" -j '+%a, %d %b %Y %H:%M:%S %z' "$1T00:00:00Z" +} + +get_header() { + HFILE="$1" + HEADER="$2" + + FILTER="{ \ + if (match(\$0, /^$HEADER: /)) {print substr(\$0, RLENGTH+1, length(\$0) - RLENGTH)}; \ + if (\$0~/^$/) {exit}; \ + if (!\$0~/^[^ ]*: .*$/) {exit}; \ + }" + + awk "$FILTER" "$HFILE" +} + +list_items() { + DIR="$1" + + pushd "$DIR" > /dev/null + find . ! -path . -type d | while read -r FN + do + PAGE="$(echo "$FN" | sed 's/^\.\///g')" + #echo "FILE: $PAGE" + + MD="$PAGE/note.md" + if [ ! -f "$MD" ]; then + echo "$MD doesn't exist" + exit 1; + fi + + SUBJECT="$(get_header "$MD" "Subject")" + FDATE="$(get_header "$MD" "X-Date" | sed 's/T.*$//g')" + + if [ -z "$SUBJECT" ]; then + echo "No subject in $MD" + exit 1 + fi + + if [ -z "$FDATE" ] && [ "$NODATE" == "0" ]; then + echo "No date in $MD" + exit 1 + fi + + echo "$FDATE $PAGE $SUBJECT" + done + popd > /dev/null +} + +render_item() { + base_url="$1" + item="$2" + + site_url="$(echo "$base_url"| sed 's#\(.*//.*\)/.*#\1#')" + + date=$(echo "$item"|awk '{print $1}') + url=$(echo "$item"|awk '{print $2}') + title=$(echo "$item"| cut -d ' ' -f 3- ) + + guid="$base_url/$(echo "$url" | sed 's#^/##')" + + cat <<-EOF + + $guid + $guid + $(date_to_rfc_822 "$date") + $title + + + EOF +} + +render_items() { + base_url="$1" + + while read -r i + do render_item "$1" "$i" + done +} + +render_feed() { + url="$1" + title=$(echo "$2" | remove_nbsp) + description="$3" + + base_url="$(echo "$url" | cut -d '/' -f1-3)" + + cat <<-EOF + + + + + $title + $description + $base_url/ + $(date_rfc_822) + $(cat) + + EOF +} + +list_items "$1" | sort | render_items "$2$3" | render_feed "$2" "Konstantin Nazarov" "test"