gemlog.sh/gemlog.sh

144 lines
5.4 KiB
Bash
Raw Normal View History

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
# change configuration variables here to suit you
### be sure to also change the header and footer in build_entries! ###
2020-09-02 22:52:31 +00:00
make_globals() {
2020-10-29 03:05:35 +00:00
global_title="swiftmandolin's gemlog"
global_description="a gemlog about nonsense you probably don't care about"
2022-07-22 01:55:15 +00:00
global_url="gemini://rawtext.club/~swiftmandolin/gemlog/" # link to base url of gemlog
2020-10-29 03:05:35 +00:00
global_author="swiftmandolin"
global_author_email="swiftmandolin@tilde.team"
2022-07-22 01:55:15 +00:00
global_author_url="gemini://rawtext.club/~swiftmandolin/index.gmi" # homepage of author
global_license="CC BY 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
2022-07-22 01:55:15 +00:00
feed_base_url="gemini://rawtext.club/~swiftmandolin/gemlog/" # base url that the feed is hosted at
2020-10-29 03:05:35 +00:00
feed_web_url="https://swiftmandolin.tilde.team/files/" # base url that the feed is hosted at on the web
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
use_year_divider=true # separate posts from different years with a heading?
# these 2 are exclusive, and use_month_divider_nl takes priority if both are set
use_month_divider_nl=true # separate posts from different months with a newline?
use_month_divider_hd=false # separate posts from different months with a heading?
gemlog_sh_link="https://tildegit.org/nytpu/gemlog.sh" # link to the utility, you should change this if you've made substantial non-configuration changes
2020-09-02 22:52:31 +00:00
# don't change these
date_format_8601="+%Y-%m-%dT%H:%M:%S%:z" # *formal* ISO-8601 format including time zone
date_format_8601_timeless="+%Y-%m-%dT12:00:00%:z" # *formal* ISO-8601 format with fixed preset time
2020-10-14 16:09:00 +00:00
monthnames=( "invalid" "January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December" )
2020-09-02 22:52:31 +00:00
}
get_post_title() {
cat "$1" | perl -lne 's/#{1,3}\s+(.*)/\1/ or next; print; exit'
}
build_entries() {
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)
cat << 'EOF'
2020-10-29 03:05:35 +00:00
```some crazy looking ASCII art that says gemlog
.-'''-.
.---. ' _ \
__.....__ __ __ ___ | | / /` '. \
.--./) .-'' '. | |/ `.' `. | |. | \ ' .--./)
/.''\\ / .-''"'-. `. | .-. .-. '| || ' | '/.''\\
| | | / /________\ \| | | | | || |\ \ / /| | | |
\`-' /| || | | | | || | `. ` ..' / \`-' /
/("'` \ .-------------'| | | | | || | '-...-'` /("'`
\ '---.\ '-.____...---.| | | | | || | \ '---.
/'""'.\`. .' |__| |__| |__|| | /'""'.\
|| || `''-...... -' '---' || ||
\'. __// \'. __//
`'---' `'---'
2020-09-03 02:16:07 +00:00
```
2020-09-02 22:52:31 +00:00
2020-10-29 03:05:35 +00:00
by switmandolin
2020-09-03 02:16:07 +00:00
EOF
curyear=""
curmonth=""
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')
newcuryear=$(echo $i | perl -ne '/^(\d{4})-\d{2}-\d{2}.*/; print $1')
newcurmonth=$(echo $i | perl -ne '/^\d{4}-(\d{2})-\d{2}.*/; print $1')
if [ "$newcuryear" != "$curyear" ] && $use_year_divider; then
printf "\n\n## $newcuryear\n"
curyear="$newcuryear"
fi
if [ "$newcurmonth" != "$curmonth" ]; then
if $use_month_divider_nl; then
printf "\n"
elif $use_month_divider_hd; then
printf "\n### ${monthnames[${newcurmonth#0}]}\n\n"
fi
curmonth="$newcurmonth"
fi
printf "=> $global_url$post $pubdate$title\n"
2020-09-02 22:52:31 +00:00
done < <(ls -r [[:digit:]]*.gmi)
2020-09-10 05:04:51 +00:00
# footer of the page (below the list of posts)
cat << EOF
──────────────────
2020-10-29 03:05:35 +00:00
=> $global_author_url go home
=> $feed_base_url$gemlog_feed this gemlog has an atom feed at: $feed_base_url$gemlog_feed
=> $feed_web_url$gemlog_feed or through the web at: $feed_web_url$gemlog_feed
=> $gemlog_sh_link generated with gemlog.sh
2020-10-29 03:05:35 +00:00
=> https://patorjk.com/software/taag/#p=display&h=2&v=0&f=Crazy&t=gemlog ASCII Art by Patrick Gillespie
EOF
} 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
echo "tooting..."
2020-09-10 05:04:51 +00:00
# change this if you want the toot to have different content
printf "new gemlog post: ${title}\n\n${global_url}${filename}\n" | toot post
2020-09-03 02:16:07 +00:00
fi
}
2020-09-02 22:52:31 +00:00
make_globals
build_entries