Add rss feed

This commit is contained in:
Konstantin Nazarov 2021-09-12 17:16:30 +01:00
parent 937ca58145
commit 99e0eb4c2a
Signed by: knazarov
GPG key ID: 4CFE0A42FA409C22
2 changed files with 117 additions and 1 deletions

View file

@ -8,7 +8,7 @@ PROJECTS_DST := $(patsubst content/projects/%,$(ODIR)/projects/%/index.html,$(PR
POSTS_SRC := $(wildcard content/posts/*) POSTS_SRC := $(wildcard content/posts/*)
POSTS_DST := $(patsubst content/posts/%,$(ODIR)/posts/%/index.html,$(POSTS_SRC)) 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 $(ODIR)/%/index.html: content/pages/%/note.md
mkdir -p "$(dir $@)" mkdir -p "$(dir $@)"
@ -25,6 +25,9 @@ $(ODIR)/posts/%/index.html: content/posts/%/note.md
$(ODIR)/posts/index.html: $(POSTS_SRC) $(ODIR)/posts/index.html: $(POSTS_SRC)
./bin/toc.sh content/posts "/posts" | awk -f ./bin/markdown.awk | ./bin/page.sh Posts > "$@" ./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 $(ODIR)/index.html: content/index.md
cat content/index.md | ./bin/mdpage.sh --notitle > "$@" cat content/index.md | ./bin/mdpage.sh --notitle > "$@"

113
bin/rss.sh Executable file
View file

@ -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
<item>
<guid>$guid</guid>
<link>$guid</link>
<pubDate>$(date_to_rfc_822 "$date")</pubDate>
<title>$title</title>
</item>
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
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="$url" rel="self" type="application/rss+xml" />
<title>$title</title>
<description>$description</description>
<link>$base_url/</link>
<lastBuildDate>$(date_rfc_822)</lastBuildDate>
$(cat)
</channel></rss>
EOF
}
list_items "$1" | sort | render_items "$2$3" | render_feed "$2" "Konstantin Nazarov" "test"