From 208f7cf839d0dd8caaa2bec5b2df5791a993dd87 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sat, 7 Oct 2023 18:34:20 +0200 Subject: [PATCH] Added rss feed generator, which generates one main rss feed and one feed for each feed topic --- .woodpecker.yml | 7 +- build.sh | 103 +++++++++++++++++++++++- {el => templates}/gruvbox.theme | 0 el/html.template => templates/page.html | 0 templates/rss-footer.xml | 2 + templates/rss-header.xml | 6 ++ templates/rss-post.xml | 13 +++ 7 files changed, 124 insertions(+), 7 deletions(-) rename {el => templates}/gruvbox.theme (100%) rename el/html.template => templates/page.html (100%) create mode 100644 templates/rss-footer.xml create mode 100644 templates/rss-header.xml create mode 100644 templates/rss-post.xml diff --git a/.woodpecker.yml b/.woodpecker.yml index ef8ac8d..d0a076b 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -1,14 +1,15 @@ +--- pipeline: # build static content build: image: alpine commands: - - apk add sed pandoc - - sh ./build.sh + - apk add bash sed pandoc + - bash ./build.sh https://ccw.icu # publish content publish: image: alpine - secrets: [ deploy_pass ] + secrets: [deploy_pass] commands: - apk add rsync - export RSYNC_PASSWORD="$${DEPLOY_PASS}" diff --git a/build.sh b/build.sh index f87e40a..c186ca4 100755 --- a/build.sh +++ b/build.sh @@ -1,13 +1,17 @@ #!/bin/bash # depends: pandoc, sed, awk +host="${1:-https://example.com}" root="$(dirname $0)" srcdir="$root/src" assetdir="$root/res" +eldir="$root/el" +templatedir="$root/templates" builddir="$root/build" +feedsdir="$srcdir/feeds" # pandoc syntax highlighting theme -pandocTheme="./el/gruvbox.theme" +pandocTheme="$templatedir/gruvbox.theme" # pandoc export target pandocTarget="html5" pandocTargetExtension="html" @@ -24,7 +28,9 @@ function convertOrg() { -t $pandocTarget \ -c '/base.css' \ --toc --toc-depth=1 \ - --template ./el/html.template \ + -V postlink="$3" \ + -M date-meta="$4" \ + --template $2 \ --highlight-style="$pandocTheme" \ - | # pandoc does not autoreplace the ->.org links, @@ -42,12 +48,23 @@ mkdir -p $builddir cp $assetdir/* -r $builddir prefix="s/^$(echo $srcdir | sed 's/\//\\\//')\///" - # loop through all source files find $srcdir -type f | \ # remove leading $srcdir sed "$prefix" | \ + # create a publishing date + path id to sort the posts while read file; do + ts="$(cat $srcdir/$file | \ + grep -E '#\+DATE: <([^<>]+)>' | \ + sed -r 's/#\+DATE: <([^<>]+)>/\1/' | \ + head -n 1)" + echo "$ts;$file" + done | \ + # sort the posts by date + sort -r | \ + while read mark; do + file="$(echo $mark | awk -F';' '{ print $2 }')" + ts="$(echo $mark | awk -F';' '{ print $1 }')" # initialise builddir path mkdir -p "$(dirname "$builddir/$file")" @@ -58,10 +75,88 @@ find $srcdir -type f | \ newName="$(echo $file | sed "s/\.org$/\.$pandocTargetExtension/")" echo $newName # me need to convert .org files - convertOrg $srcdir/$file \ + convertOrg $srcdir/$file $templatedir/page.html \ > $builddir/$newName + + if [ ! "$(basename $file)" = "index.org" ] && [ ! "$ts" = "" ]; then + root="$(echo $file | awk -F'/' '{ print $1 }')" + if [ $root = "feeds" ]; then + # is a blog post + # so we have to generate an rss feed compatible version + + # id of the subfeed + feed="$(echo $file | awk -F'/' '{ print $2 }')" + + filename=$(basename $file | sed -r 's/org$/html/') + cont="$(convertOrg \ + $srcdir/$file \ + $templatedir/rss-post.xml \ + $host/feeds/$feed/$filename \ + $ts + )" + + echo "$cont" >> $builddir/feeds/rss + echo "$cont" >> $builddir/feeds/$feed/rss + fi + fi else # all other files can just be copied to the build dir cp $srcdir/$file $builddir/$file fi done + +# RSS FEED +function read_arg() { + file=$1 + arg=$2 + + reg="#\+$arg: ([^\n]+)" + + # print file + cat $1 | \ + # only select lines with a reasonable beginning + grep -E "$reg" | \ + # only use the first entry + head -n 1 | \ + # extract the text + sed -r "s/$reg/\1/" +} +function escape() { + echo $1 | \ + sed -r 's/\//\\\//g' | \ + sed -r 's/\./\\\./g' +} +rssheader="$(cat $templatedir/rss-header.xml)" +rssfooter="$(cat $templatedir/rss-footer.xml)" +# list all feeds +find $feedsdir -type d -maxdepth 1 | \ + # remove ugly prefixes + sed "$prefix" | + # loop over feeds + while read feed; do + rsspath="$builddir/$feed/rss" + infopath="$srcdir/$feed/index.org" + webpath="$host/$feed/index.html" + + # only publish rss feeds with at least one post + if [ -f $rsspath ]; then + rsscont="$(cat $rsspath)" + + title="$(read_arg $infopath TITLE)" + description="$(read_arg $infopath DESCRIPTION)" + + # overwrite file with header + echo "$rssheader" | \ + # replace weblink + sed -r "s/\\\$host\\\$/$(escape "$webpath")/g" | \ + # replace feed title + sed -r "s/\\\$title\\\$/$(escape "$title")/g" | \ + # replace feed description + sed -r "s/\\\$description\\\$/$(escape "$description")/g" \ + > $rsspath + # add content back to feed + echo "$rsscont" >> $rsspath + # close missing tags + echo "$rssfooter" >> $rsspath + fi + done diff --git a/el/gruvbox.theme b/templates/gruvbox.theme similarity index 100% rename from el/gruvbox.theme rename to templates/gruvbox.theme diff --git a/el/html.template b/templates/page.html similarity index 100% rename from el/html.template rename to templates/page.html diff --git a/templates/rss-footer.xml b/templates/rss-footer.xml new file mode 100644 index 0000000..68ddf9d --- /dev/null +++ b/templates/rss-footer.xml @@ -0,0 +1,2 @@ + + diff --git a/templates/rss-header.xml b/templates/rss-header.xml new file mode 100644 index 0000000..ad7c5d0 --- /dev/null +++ b/templates/rss-header.xml @@ -0,0 +1,6 @@ + + + $title$ + $description$ + $host$ + pandoc + bash script diff --git a/templates/rss-post.xml b/templates/rss-post.xml new file mode 100644 index 0000000..79bea1c --- /dev/null +++ b/templates/rss-post.xml @@ -0,0 +1,13 @@ + + $if(title-prefix)$$title-prefix$ - $endif$$pagetitle$ + + $body$ + + + $postlink$ + + + $postlink$ + + $date-meta$ + -- 2.38.5