sbrs

Simple blog and RSS system
git clone git://git.christosmarg.xyz/sbrs.git
Log | Files | Refs | README | LICENSE

commit 839f02755e2661c497fd7d458ae700fbd11bb4ba
parent 48aa2f5de05cab199e36ae5be3a0679117a20659
Author: Christos Margiolis <christos@margiolis.net>
Date:   Fri, 23 Oct 2020 03:34:00 +0300

cleanup code a bit, fixed delete bug

Diffstat:
MMakefile | 3++-
Mautoblog | 174++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
2 files changed, 103 insertions(+), 74 deletions(-)

diff --git a/Makefile b/Makefile @@ -16,11 +16,12 @@ TAR = tar -cf GZIP = gzip all: ${BIN} + chmod +x ${BIN} dist: ${MKDIR} ${DIST} ${CP} -R ${BIN} ${MAN1} blogindex.html index.html LICENSE Makefile\ - README.md rss.xml styles.css template.html ${DIST} + README.md rss.xml styles.css template.html ${DIST} ${TAR} ${DIST}.tar ${DIST} ${GZIP} ${DIST}.tar ${RM_DIR} ${DIST} diff --git a/autoblog b/autoblog @@ -1,5 +1,4 @@ -#!/usr/local/bin/bash -# make POSIX +#!/bin/sh # See LICENSE file for copyright and license details. website="https://christosmarg.xyz" @@ -10,49 +9,84 @@ blogindex="blogindex.html" index="index.html" rssfile="rss.xml" template="template.html" -[ -z "$EDITOR" ] && EDITOR="vi" +[ -z "$EDITOR" ] && EDITOR="vim" +[ "$OSTYPE" = "freebsd" ] && alias sed="gsed" -confirm_action() { - read -erp "$1" confirm && [ "$confirm" = "$2" ] || exit +main() { + missing_check + case $1 in + -n*) newpost ;; + -p*) listposts $draftdir && publish ;; + -e*) listposts $draftdir && $EDITOR $draftdir/$blogpost.html ;; + -v*) listposts $draftdir && view ;; + -t*) listposts $draftdir && delete $draftdir ;; + -r*) listposts $blogdir && $EDITOR $blogdir/$blogpost.html ;; + -c*) listposts $blogdir && title_change ;; + -o*) listposts $blogdir && $BROWSER $blogdir/$blogpost.html ;; + -d*) listposts $blogdir && delete $blogdir ;; + -l*) listposts $blogdir ;; + *) usage ;; + esac +} + +list() { + find $1 -type f ! -name '*final*' | awk -F/ '{print $NF}' } listposts() { - printf "Listing posts in %s (total: %d)\n" "$1" "$(gnuls $1 -I "*final*" | wc -l)" - gnuls -rc $1 -I "*-final*" | awk -F '/' '{print $NF}' | nl - [ $(($(gnuls $1 | wc -l))) -eq 0 ] && echo "No posts available in $1" && exit - read -erp "Choose a post to by number: " num \ - && [ -z "$(echo $num | grep -E "^[1-9]+$")" ] \ - || [ $(($(echo "$blogpost" | wc -l))) -gt 1 ] \ - || [ $(($num)) -gt $(($(gnuls $1 | wc -l))) ] \ - || [ -z "$num" ] \ - && echo "No post selected." && exit - blogpost=$(gnuls -rc $1 -I "*-final*" | nl | grep -w " $num" | awk '{print $2}' | gsed "s/\..*//") + printf "Listing posts in %s (total: %d)\n" "$1" "$(list $1 | wc -l)" + nposts=$(expr $(list $1 | wc -l)) + list $1 | nl + [ $nposts -eq 0 ] && echo "No posts available in $1" && exit + read -erp "Choose a post to by number: " num \ + && [ -z "$(echo $num | grep -E "^[1-9]+$")" ] \ + || [ $(expr $num) -gt $nposts ] || [ -z "$num" ] \ + && echo "No post selected." && exit + blogpost=$(list $1 | nl | grep -w "$num" | awk '{print $2}') + blogpost=$(basename -s ".html" $blogpost) } newpost() { mkdir -p $draftdir - read -erp "Title: " title && [ -z "$title" ] && echo "Please specify a title." && exit - blogpost=$(echo $title | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-') + read -erp "Title: " title && [ -z "$title" ] && + echo "Please specify a title." && exit + blogpost=$( + echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | \ + tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' + ) [ -f "$blogdir/$blogpost.html" ] && echo "File exists already." && exit $EDITOR "$draftdir/$blogpost.html" - gsed -i "s/^/\ \ \ \ \ \ \ \ /" $draftdir/$blogpost.html # bad? - gsed "s/TITLE/$title/g;s/HEADER/$title/g;s/AUTHOR/$author/g;" $template > $draftdir/$blogpost-final.html + sed "s/TITLE/$title/g;s/HEADER/$title/g;s/AUTHOR/$author/g;" $template \ + > $draftdir/$blogpost-final.html +} + +confirm_action() { + read -erp "$1" confirm && [ "$confirm" = "$2" ] || exit } publish() { confirm_action "Publish post (y/N)? " "y" - title=$(grep "<title>" $draftdir/$blogpost-final.html | gsed "s/<title>//;s/<\/title>//;s/ *//;") - gsed -i "/<\!--BLOG-->/r $draftdir/$blogpost.html" $draftdir/$blogpost-final.html - gsed "s/</\&lt;/g;s/>/\&gt;/g;" "$draftdir/$blogpost.html" > $draftdir/$blogpost.xml - cp $draftdir/$blogpost-final.html $blogdir && mv $blogdir/$blogpost-final.html $blogdir/$blogpost.html - - printf "\t\t\t\t<li>%s &ndash; <a href=\"%s\">%s</a></li>\n" "$(date '+%Y %b %d')" "$blogdir/$blogpost.html" "$title" | expand -t4 > $draftdir/$blogpost-htmlentry - printf "<item>\n\t<title>%s</title>\n\t<guid>%s</guid>\n\t<pubDate>%s</pubDate>\n\t<description>\n\t\t%s\n \t</description>\n</item>\n" "$title" "$website/$blogdir/$blogpost.html" "$(date '+%a, %d %b %Y')" "$(cat $draftdir/$blogpost.xml)" | expand -t4 > $draftdir/$blogpost-rssentry + title=$( + grep "<title>" $draftdir/$blogpost-final.html | \ + sed "s/<title>//;s/<\/title>//;s/ *//;" + ) + sed -i "s/^/\ \ \ \ \ \ \ \ /" $draftdir/$blogpost.html # bad? + sed -i "/<\!--BLOG-->/r $draftdir/$blogpost.html" $draftdir/$blogpost-final.html + sed "s/</\&lt;/g;s/>/\&gt;/g;" "$draftdir/$blogpost.html" > $draftdir/$blogpost.xml + cp $draftdir/$blogpost-final.html $blogdir && + mv $blogdir/$blogpost-final.html $blogdir/$blogpost.html + + printf "\t\t\t\t<li>%s &ndash; <a href=\"%s\">%s</a></li>\n" \ + "$(date '+%Y %b %d')" "$blogdir/$blogpost.html" "$title" | \ + expand -t4 > $draftdir/$blogpost-final-htmlentry + + printf "<item>\n\t<title>%s</title>\n\t<guid>%s</guid>\n\t<pubDate>%s</pubDate>\n\t<description>\n\t\t%s\n \t</description>\n</item>\n" "$title" "$website/$blogdir/$blogpost.html" "$(date '+%a, %d %b %Y')" "$(cat $draftdir/$blogpost.xml)" | expand -t4 > $draftdir/$blogpost-final-rssentry blogindex_update - gsed -i "/<\!--BLOG $(date '+%B %Y')-->/r $draftdir/$blogpost-htmlentry" $blogindex && echo "Blogindex... done." - gsed -i "/<\!--BLOG-->/r $draftdir/$blogpost-htmlentry" $index && echo "Index... done" - gsed -i "/<\!--BLOG-->/r $draftdir/$blogpost-rssentry" $rssfile && echo "RSS... done" + sed -i "/<\!--BLOG $(date '+%B %Y')-->/r $draftdir/$blogpost-final-htmlentry" $blogindex && + echo "Blogindex... done." + sed -i "/<\!--BLOG-->/r $draftdir/$blogpost-final-htmlentry" $index && echo "Index... done" + sed -i "/<\!--BLOG-->/r $draftdir/$blogpost-final-rssentry" $rssfile && echo "RSS... done" remove_last_index_entry && echo "Removing last entry from index file... done" remove_contents $draftdir && echo "Cleaning up .drafts... done" echo "Published $blogpost." @@ -60,7 +94,7 @@ publish() { # i dont like how this looks... blogindex_update() { - dateid=$(date '+%b %Y' | gsed 's/\ //' | tr '[:upper:]' '[:lower:]') + dateid=$(date '+%b %Y' | sed 's/\ //' | tr '[:upper:]' '[:lower:]') if [ -z "$(grep "$dateid" $blogindex)" ]; then datename=$(date '+%B %Y') monthheader=$(printf "\\ @@ -68,46 +102,58 @@ blogindex_update() { <ul> \\ \t<\!--BLOG %s--> \\ <\/ul>" \ - "$dateid" "$datename" "$datename" | gsed 's/^/\t\t\t/' | expand -t4) + "$dateid" "$datename" "$datename" | sed 's/^/\t\t\t/' | expand -t4) - gsed -i "/<\!--BLOG-->/a $monthheader" $blogindex + sed -i "/<\!--BLOG-->/a $monthheader" $blogindex fi } +remove_last_index_entry() { + indexentries=$(sed "1,/<\!--BLOG-->/d" $index | grep "<li>") && + [ $(($(echo "$indexentries" | wc -l))) -gt 7 ] && + lastentry=$(echo "$indexentries" | tail -2 | head -1) && + sed -i "s|$lastentry||;" $index +} + delete() { - [ $(($(echo "$blogpost" | wc -l))) -gt 1 ] && echo "Invalid choice" && exit confirm_action "Are you sure you want to delete \"$blogpost\" (y/N)? " "y" - [ "$1" = "$blogdir" ] && - gsed -i "/$blogpost/d" $index $blogindex && - gsed -ni "/<item>/{ :loop; N; s/<\\/item>/&/; T loop; s/$blogpost/&/; T keep; d }; :keep; p" $rssfile + if [ "$1" = "$blogdir" ]; then + sed -i "/$blogpost/d" $index $blogindex + sed -ni "/<item>/{ :loop; N; s/<\\/item>/&/; T loop; s/$blogpost/&/; T keep; d }; :keep; p" $rssfile + fi remove_contents $1 && echo "Removed $blogpost." } +remove_contents() { + ls $1 | grep -x "$blogpost\.\S*\|$blogpost-final\S*" | sed "s/^/$1\//" | xargs rm +} + view() { cat $draftdir/$blogpost-final.html > $draftdir/$blogpost-final-view.html - title=$(grep "<title>" $draftdir/$blogpost-final-view.html | gsed "s/<title>//;s/<\/title>//;s/ *//;") - gsed -i "/<\!--BLOG-->/r $draftdir/$blogpost.html" $draftdir/$blogpost-final-view.html + title=$( + grep "<title>" $draftdir/$blogpost-final-view.html | \ + sed "s/<title>//;s/<\/title>//;s/ *//;" + ) + sed -i "/<\!--BLOG-->/r $draftdir/$blogpost.html" $draftdir/$blogpost-final-view.html $BROWSER $draftdir/$blogpost-final-view.html } title_change() { - read -erp "Give post a new title: " newtitle && confirm_action "Are you sure (y/N)? " "y" - oldtitle=$(grep "<title>" $blogdir/$blogpost.html | gsed "s/<title>//;s/<\/title>//;s/ *//;") - newtitle_fmt=$(echo $newtitle | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-') - gsed -i "s/$blogpost/$newtitle_fmt/g;s/$oldtitle/$newtitle/g" $blogdir/$blogpost.html $index $blogindex $rssfile && - mv $blogdir/$blogpost.html $blogdir/$newtitle_fmt.html && - echo "Title changed successfully: $oldtitle -> $newtitle" -} - -remove_contents() { - gnuls $1 | grep -w $blogpost | gsed "s/^/$1\//" | xargs rm -} - -remove_last_index_entry() { - indexentries=$(gsed "1,/<\!--BLOG-->/d" $index | grep "<li>") && - [ $(($(echo "$indexentries" | wc -l))) -gt 7 ] && - lastentry=$(echo "$indexentries" | tail -2 | head -1) && - gsed -i "s|$lastentry||;" $index + read -erp "Give post a new title: " newtitle && \ + confirm_action "Are you sure (y/N)? " "y" + oldtitle=$( + # same with view() + grep "<title>" $blogdir/$blogpost.html | + sed "s/<title>//;s/<\/title>//;s/ *//;" + ) + newtitle_fmt=$( + echo "$newtitle" | iconv -cf UTF-8 -t ASCII//TRANSLIT | \ + tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' + ) + sed -i "s/$blogpost/$newtitle_fmt/g;s/$oldtitle/$newtitle/g" \ + $blogdir/$blogpost.html $index $blogindex $rssfile && + mv $blogdir/$blogpost.html $blogdir/$newtitle_fmt.html && + echo "Title changed successfully: $oldtitle -> $newtitle" } err() { @@ -143,22 +189,4 @@ usage() { printf " -l\t\tList all published posts\n" } -# Script begins here -main() { - missing_check - case $1 in - -n*) newpost ;; - -p*) listposts $draftdir && publish ;; - -e*) listposts $draftdir && $EDITOR $draftdir/$blogpost.html ;; - -v*) listposts $draftdir && view ;; - -t*) listposts $draftdir && delete $draftdir ;; - -r*) listposts $blogdir && $EDITOR $blogdir/$blogpost.html ;; - -c*) listposts $blogdir && title_change ;; - -o*) listposts $blogdir && $BROWSER $blogdir/$blogpost.html ;; - -d*) listposts $blogdir && delete $blogdir ;; - -l*) listposts $blogdir ;; - *) usage ;; - esac -} - main "$1"