gemlog.sh/gemlog.sh

102 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Copyright 2020 nytpu
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# fill out variables here
make_globals() {
global_title="yatb"
global_description="yet another tech blog"
global_url="gemini://nytpu.com/gemlog/" # link to base url of blog
global_author="nytpu"
global_license="CC by"
gemlog_feed="feed.rss" # filename of the rss feed
number_of_feed_articles="50" # number of posts in rss feed
feed_base_url="https://nytpu.com/blog/" # base url that the feed is hosted at
index="index.gmi" # main page, not recommended to change
# don't change these
date_format_full="%a, %d %b %Y %H:%M:%S %z"
date_format_timestamp="%Y%m%d%H%M.%S"
}
get_post_title() {
cat "$1" | perl -lne 's/#{1,3}\s+(.*)/\1/ or next; print; exit'
}
# from bashblog (https://tildegit.org/team/bashblog)
# bashblog is licensed under the gnu gplv3
make_rss() {
echo "Making RSS"
rssfile="$gemlog_feed.$RANDOM"
while [[ -f $rssfile ]]; do rssfile="feed.rss.$RANDOM"; done
{
pubdate=$(LC_ALL=C date +"$date_format_full")
echo '<?xml version="1.0" encoding="UTF-8" ?>'
echo '<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">'
echo "<channel><title>$global_title</title><link>$global_url$index</link>"
echo "<description>$global_description</description><language>en</language>"
echo "<lastBuildDate>$pubdate</lastBuildDate>"
echo "<pubDate>$pubdate</pubDate>"
echo "<atom:link href=\"$feed_base_url/$blog_feed\" rel=\"self\" type=\"application/rss+xml\">"
n=0
while IFS='' read -r i; do
((n >= number_of_feed_articles)) && break
echo "<item><title>"
get_post_title "$i"
echo "</title><description></description><link>$global_url${i#'./'}</link>"
echo "<guid>$global_url${i#'./'}</guid>"
echo "<dc:creator>$global_author</dc:creator>"
echo "<pubDate>$(LC_ALL=C date -r "$i" +"$date_format_full")</pubDate></item>"
n=$(( n + 1 ))
done < <(ls -r ./[[:digit:]]*.gmi)
echo '</channel></rss>'
} 3>&1 >"$rssfile"
mv "$rssfile" "$gemlog_feed"
chmod 644 "$gemlog_feed"
}
# change this to what you want
build_entries() {
echo "Building entries"
{
printf "yatb (yet another tech gemlog)\r\n\r\n"
while IFS='' read -r i; do
post=$(basename $i)
title=$(get_post_title "$i")
pubdate=$(echo $i | perl -ne '/^(\d{4}-\d{2}-\d{2}).*/; print $1')
printf "=> $global_url$post $pubdate$title\r\n"
done < <(ls -r [[:digit:]]*.gmi)
printf "\r\n\r\n"
printf "this gemlog/phlog/blog is mirrored on gopher at:\r\n"
printf "=> gopher://nytpu.com/phlog/\r\n\r\n"
} 3>&1 >"$index"
}
make_globals
make_rss
build_entries