2020-09-02 22:52:31 +00:00
#!/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/>.
2020-09-10 05:04:51 +00:00
# to customize, modify variables here and modify build_entries()
2020-09-02 22:52:31 +00:00
make_globals( ) {
2020-09-21 14:29:57 +00:00
global_title = "yet another tech gemlog"
global_description = "yatg"
2020-09-10 05:04:51 +00:00
global_url = "gemini://nytpu.com/gemlog/" # link to base url of gemlog
2020-09-02 22:52:31 +00:00
global_author = "nytpu"
2020-09-10 01:40:24 +00:00
global_author_email = "alex@nytpu.com"
2020-09-10 05:04:51 +00:00
global_author_url = "gemini://nytpu.com/" # homepage of author
2020-09-19 15:26:55 +00:00
global_license = "CC BY-SA 4.0"
2020-09-02 22:52:31 +00:00
2020-09-10 01:40:24 +00:00
gemlog_feed = "atom.xml" # filename of the atom feed
2020-09-10 05:04:51 +00:00
number_of_feed_articles = "50" # maximum number of posts added to atom feed
2020-09-03 15:20:58 +00:00
feed_base_url = "https://nytpu.com/files/" # base url that the feed is hosted at
2020-09-02 22:52:31 +00:00
2020-09-10 05:04:51 +00:00
index = "index.gmi" # main page of gemlog, not recommended to change
2020-09-03 02:16:07 +00:00
2020-09-02 22:52:31 +00:00
# don't change these
2020-09-10 05:18:49 +00:00
gemlog_sh_link = "https://tildegit.org/nytpu/gemlog.sh" # link to the utility, you should change this if you modified it substantially
date_format_8601 = "+%Y-%m-%dT%H:%M:%S%:z" # *formal* ISO-8601 format including time zone
2020-09-21 14:29:57 +00:00
date_format_8601_timeless = "+%Y-%m-%dT12:00:00%:z" # *formal* ISO-8601 format including time zone
2020-09-02 22:52:31 +00:00
}
get_post_title( ) {
cat " $1 " | perl -lne 's/#{1,3}\s+(.*)/\1/ or next; print; exit'
}
2020-09-10 01:40:24 +00:00
make_atom( ) {
2020-09-12 16:37:19 +00:00
echo "Bulding atom feed..."
2020-09-02 22:52:31 +00:00
2020-09-10 01:40:24 +00:00
atomfile = " $gemlog_feed . $RANDOM "
while [ [ -f $atomfile ] ] ; do atomfile = " $gemlog_feed . $RANDOM " ; done
2020-09-02 22:52:31 +00:00
{
2020-09-10 01:40:24 +00:00
pubdate = $( date " $date_format_8601 " )
cat << EOF
<?xml version = "1.0" encoding = "utf-8" ?>
<feed xmlns = "http://www.w3.org/2005/Atom" >
<title>$global_title </title>
<subtitle>$global_description </subtitle>
<link rel = 'self' href = '$feed_base_url$gemlog_feed' />
<link rel = 'alternate' href = '$global_url$index' />
<updated>$pubdate </updated>
<author>
<name>$global_author </name>
2020-09-10 05:21:17 +00:00
<email>$global_author_email </email>
2020-09-10 01:40:24 +00:00
<uri>$global_author_url </uri>
</author>
<id>$global_url </id>
<generator uri = '$gemlog_sh_link' >gemlog.sh</generator>
<rights>© $global_author - $global_license </rights>
EOF
2020-09-02 22:52:31 +00:00
n = 0
while IFS = '' read -r i; do
( ( n >= number_of_feed_articles) ) && break
2020-09-10 01:40:24 +00:00
printf "\n <entry>\n <title>"
get_post_title " $i " | tr -d '\n'
printf " </title>\n <id> $global_url ${ i # './' } </id>\n "
printf " <link rel='alternate' href=' $global_url ${ i # './' } '/>\n <updated> "
2020-09-19 15:26:55 +00:00
echo " $i " | perl -ne '/^(\d{4}-\d{2}-\d{2}).*/; print $1' | date " $date_format_8601_timeless " -f - | tr -d '\n'
2020-09-10 05:04:51 +00:00
# change or remove the <summary></summary> block if you want a different description or no description at all
2020-09-10 05:21:17 +00:00
printf " </updated>\n <summary>You need a gemini client to view this post. If you have one installed, here is the link to the post: $global_url ${ i # './' } </summary>\n </entry>\n "
2020-09-03 02:16:07 +00:00
2020-09-02 22:52:31 +00:00
n = $(( n + 1 ))
2020-09-10 01:40:24 +00:00
done < <( ls -r [ [ :digit:] ] *.gmi)
2020-09-03 02:16:07 +00:00
2020-09-10 01:40:24 +00:00
printf '</feed>'
} 3>& 1 >" $atomfile "
2020-09-02 22:52:31 +00:00
2020-09-10 01:40:24 +00:00
mv " $atomfile " " $gemlog_feed "
2020-09-02 22:52:31 +00:00
chmod 644 " $gemlog_feed "
}
build_entries( ) {
2020-09-12 16:37:19 +00:00
echo "Building index..."
indexfile = " $index . $RANDOM "
while [ [ -f $indexfile ] ] ; do indexfile = " $index . $RANDOM " ; done
2020-09-02 22:52:31 +00:00
{
2020-09-10 05:04:51 +00:00
# header of the page (above the posts list)
2020-10-09 20:22:51 +00:00
cat << 'EOF'
2020-09-03 22:49:01 +00:00
` ` ` yet another tech gemlog ( yatg) by nytpu
2020-09-03 05:06:37 +00:00
,───────────────.
( _\ _________ \ __
2020-09-03 02:16:07 +00:00
│ /__| ___| __\ │ __ ______ _/ /_____ _
│ ` . \ / ,' │ / / / / __ ` / __/ __ ` /
│ ` .\ /,' │ / /_/ / /_/ / /_/ /_/ /
│ ` .' │ \_ _, /\_ _,_/\_ _/\_ _, /
│ YET ANOTHER │ /____/ /____/
_│ TECH GEMLOG │ by nytpu
( _/_______________/
` ` `
2020-09-02 22:52:31 +00:00
2020-09-03 02:16:07 +00:00
EOF
2020-09-02 22:52:31 +00:00
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' )
2020-10-09 20:22:51 +00:00
printf " => $global_url $post $pubdate — $title \n "
2020-09-02 22:52:31 +00:00
done < <( ls -r [ [ :digit:] ] *.gmi)
2020-10-09 20:22:51 +00:00
2020-09-10 05:04:51 +00:00
# footer of the page (below the list of posts)
2020-10-09 20:22:51 +00:00
cat << EOF
2020-09-10 05:18:49 +00:00
2020-10-09 20:22:51 +00:00
⁂
2020-09-10 05:18:49 +00:00
= > / go home
= > /gemlog/old/ click here to view an archive of my old gemlog
= > $feed_base_url $gemlog_feed this gemlog has an atom feed at: $feed_base_url $gemlog_feed
= > gemini://nytpu.com/gemlog/$gemlog_feed or through gemini at: gemini://nytpu.com/gemlog/$gemlog_feed
= > https://tildegit.org/nytpu/gemlog.sh generated with gemlog.sh
EOF
2020-09-12 16:37:19 +00:00
} 3>& 1 >" $indexfile "
mv " $indexfile " " $index "
chmod 644 " $index "
2020-09-02 22:52:31 +00:00
}
2020-09-03 02:16:07 +00:00
toot( ) {
filename = $( ls -r ./[ [ :digit:] ] *.gmi | head -n 1 | xargs basename)
title = $( get_post_title " $filename " )
read -r -p "do you want to toot the newest post? [y/N] " response
if [ [ " $response " = ~ ^( [ yY] [ eE] [ sS] | [ yY] ) $ ] ]
then
2020-09-12 16:37:19 +00:00
echo "tooting..."
2020-09-10 05:04:51 +00:00
# change this if you want the toot to have different content
2020-10-09 20:22:51 +00:00
printf " new gemlog post: ${ title } \n\ngemini://nytpu.com/gemlog/ ${ filename } " | toot post
2020-09-03 02:16:07 +00:00
fi
}
2020-09-02 22:52:31 +00:00
make_globals
2020-09-10 01:40:24 +00:00
make_atom
2020-09-02 22:52:31 +00:00
build_entries
2020-09-03 02:16:07 +00:00
toot
2020-09-02 22:52:31 +00:00