sbrs

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

commit f37f577089669a2247994ad70caefb8a51e2a2d9
parent 1d1b53389777d2d392c9000bf3963d0912c14958
Author: Christos Margiolis <christos@margiolis.net>
Date:   Fri, 11 Dec 2020 23:55:41 +0200

enclosed variables in braces

Diffstat:
Msbrs | 189++++++++++++++++++++++++++++++++++++++++---------------------------------------
1 file changed, 95 insertions(+), 94 deletions(-)

diff --git a/sbrs b/sbrs @@ -9,31 +9,31 @@ BLOGINDEX="blogindex.html" INDEX="index.html" RSSFILE="rss.xml" TEMPLATE="template.html" -test -z "$EDITOR" && EDITOR="vim" +test -z "${EDITOR}" && EDITOR="vim" main() { - test -f "$TEMPLATE" || err "$TEMPLATE: missing file" - test -f "$BLOGINDEX" || err "$BLOGINDEX: missing file" - test -f "$INDEX" || err "$INDEX: missing file" - test -f "$RSSFILE" || err "$RSSFILE: missing file" + test -f "${TEMPLATE}" || err "${TEMPLATE}: missing file" + test -f "${BLOGINDEX}" || err "${BLOGINDEX}: missing file" + test -f "${INDEX}" || err "${INDEX}: missing file" + test -f "${RSSFILE}" || err "${RSSFILE}: missing file" - if ! test -d "$BLOGDIR"; then + if ! test -d "${BLOGDIR}"; then confirmact "Blog directory doesn't exist. Intialize it here (y/n)? " "y" - mkdir -pv "$BLOGDIR" + mkdir -pv "${BLOGDIR}" fi - case $1 in + 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" ;; + -p*) listposts "${DRAFTDIR}" && publish ;; + -e*) listposts "${DRAFTDIR}" && ${EDITOR} "${DRAFTDIR}/${blogpost}.html" ;; + -v*) listposts "${DRAFTDIR}" && view ;; + -t*) listposts "${DRAFTDIR}" && delete "${DRAFTDIR}" ;; # edit rss too - -r*) listposts "$BLOGDIR" && $EDITOR "$BLOGDIR/$blogpost.html" ;; - -c*) listposts "$BLOGDIR" && titlechange ;; - -o*) listposts "$BLOGDIR" && $BROWSER "$BLOGDIR/$blogpost.html" ;; - -d*) listposts "$BLOGDIR" && delete "$BLOGDIR" ;; - -l*) listposts "$BLOGDIR" ;; + -r*) listposts "${BLOGDIR}" && ${EDITOR} "${BLOGDIR}/${blogpost}.html" ;; + -c*) listposts "${BLOGDIR}" && titlechange ;; + -o*) listposts "${BLOGDIR}" && ${BROWSER} "${BLOGDIR}/${blogpost}.html" ;; + -d*) listposts "${BLOGDIR}" && delete "${BLOGDIR}" ;; + -l*) listposts "${BLOGDIR}" ;; *) usage ;; esac } @@ -41,28 +41,29 @@ main() { err() { echo "${0##*/}: $@" && exit } + psed() { - sed -i.orig "$@" && rm *.orig "$BLOGDIR/*.orig" 2> /dev/null + sed -i.orig "$@" && rm *.orig "${BLOGDIR}/*.orig" 2> /dev/null } confirmact() { - read -erp "$1" act && test "$act" = "$2" || exit + read -erp "${1}" act && test "${act}" = "${2}" || exit } list() { - find "$1" -type f -name '*\.html' ! -name '*\.final*' 2> /dev/null | + find "${1}" -type f -name '*\.html' ! -name '*\.final*' 2> /dev/null | awk -F/ '{print $NF}' } listposts() { - printf "Listing posts in: %s (total: %d)\n" "$1" "$(list "$1" | wc -l)" - list "$1" | nl - nposts=$(expr $(list "$1" | wc -l)) - test $nposts -eq 0 && err "no posts available in: $1" + printf "Listing posts in: %s (total: %d)\n" "${1}" "$(list "${1}" | wc -l)" + list "${1}" | nl + nposts=$(expr $(list "${1}" | wc -l)) + test ${nposts} -eq 0 && err "no posts available in: ${1}" read -erp "Choose a post to by number: " num - if ! test -z "$(echo $num | grep -E "^[0-9]+$" | grep -v "^0")"; then - test $(expr $num) -gt $nposts && err "no post selected" - blogpost=$(list "$1" | nl | grep -w "$num" | awk '{print $2}') + if ! test -z "$(echo ${num} | grep -E "^[0-9]+$" | grep -v "^0")"; then + test $(expr ${num}) -gt ${nposts} && err "no post selected" + blogpost=$(list "${1}" | nl | grep -w "${num}" | awk '{print $2}') blogpost=${blogpost%.*} else err "no post selected" @@ -70,130 +71,130 @@ listposts() { } newpost() { - mkdir -p "$DRAFTDIR" + mkdir -p "${DRAFTDIR}" read -erp "Title: " title - test -z "$title" && err "please specify a title" - blogpost=$(titlefmt "$title") - test -f "$BLOGDIR/$blogpost.html" && err "file exists already" - $EDITOR "$DRAFTDIR/$blogpost.html" - sed "s/TITLE/$title/g;s/HEADER/$title/g;s/AUTHOR/$AUTHOR/g;" "$TEMPLATE" \ - > "$DRAFTDIR/$blogpost.final.html" + test -z "${title}" && err "please specify a title" + blogpost=$(titlefmt "${title}") + test -f "${BLOGDIR}/${blogpost}.html" && err "file exists already" + $EDITOR "${DRAFTDIR}/${blogpost}.html" + sed "s/TITLE/${title}/g;s/HEADER/${title}/g;s/AUTHOR/${AUTHOR}/g;" "${TEMPLATE}" \ + > "${DRAFTDIR}/${blogpost}.final.html" } titlefmt() { - echo "$1" | iconv -cf UTF-8 -t ASCII//TRANSLIT | + echo "${1}" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' } titleget() { - grep "<title>" "$1" | sed "s/<title>//;s/<\/title>//;s/ *//;" + grep "<title>" "${1}" | sed "s/<title>//;s/<\/title>//;s/ *//;" } titlechange() { read -erp "Give post a new title: " newtitle confirmact "Are you sure (y/N)? " "y" - oldtitle=$(titleget "$BLOGDIR/$blogpost.html") - newtitlefmt=$(titlefmt "$newtitle") - test -f "$BLOGDIR/$newtitlefmt.html" && err "file exists already" + oldtitle=$(titleget "${BLOGDIR}/${blogpost}.html") + newtitlefmt=$(titlefmt "${newtitle}") + test -f "${BLOGDIR}/${newtitle}fmt.html" && err "file exists already" - psed "s/$blogpost/$newtitlefmt/g;s/$oldtitle/$newtitle/g" \ - "$BLOGDIR/$blogpost.html" "$INDEX" "$BLOGINDEX" "$RSSFILE" && - mv "$BLOGDIR/$blogpost.html" "$BLOGDIR/$newtitlefmt.html" && - echo "Title changed successfully: $oldtitle -> $newtitle" + psed "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}" } rmcontents() { # replace with find(1) - ls "$1" | grep -x "$blogpost\...*" | sed "s/^/$1\//" | xargs rm + ls "${1}" | grep -x "${blogpost}\...*" | sed "s/^/${1}\//" | xargs rm } rmlastentry() { - indexentries=$(sed "1,/<\!--BLOG-->/d" "$INDEX" | grep "<li>") - if test $(expr $(echo "$indexentries" | wc -l)) -gt 7; then - lastentry=$(echo "$indexentries" | tail -2 | head -1) && - psed "s|$lastentry||;" "$INDEX" + indexentries=$(sed "1,/<\!--BLOG-->/d" "${INDEX}" | grep "<li>") + if test $(expr $(echo "${indexentries}" | wc -l)) -gt 7; then + lastentry=$(echo "${indexentries}" | tail -2 | head -1) && + psed "s|${lastentry}||;" "${INDEX}" fi } blogindexupdate() { dateid=$(date '+%b %Y' | sed 's/\ //' | tr '[:upper:]' '[:lower:]') - if test -z "$(grep "$dateid" "$BLOGINDEX")"; then + if test -z "$(grep "${dateid}" "${BLOGINDEX}")"; then datename=$(date '+%B %Y') psed "/<\!--BLOG-->/a\\ -\ <h2 id=\\"$dateid\\">$datename<\\/h2> \\ +\ <h2 id=\\"${dateid}\\">${datename}<\\/h2> \\ \ <ul> \\ -\ <\\!--BLOG $datename--> \\ +\ <\\!--BLOG ${datename}--> \\ \ <\\/ul> \\ -" "$BLOGINDEX" +" "${BLOGINDEX}" fi } view() { - cat "$DRAFTDIR/$blogpost.final.html" > "$DRAFTDIR/$blogpost.final-view.html" - psed "/<\!--BLOG-->/r $DRAFTDIR/$blogpost.html" "$DRAFTDIR/$blogpost.final-view.html" - $BROWSER "$DRAFTDIR/$blogpost.final-view.html" + cat "${DRAFTDIR}/${blogpost}.final.html" > "${DRAFTDIR}/${blogpost}.final-view.html" + psed "/<\!--BLOG-->/r ${DRAFTDIR}/${blogpost}.html" "${DRAFTDIR}/${blogpost}.final-view.html" + ${BROWSER} "${DRAFTDIR}/${blogpost}.final-view.html" } delete() { - confirmact "Are you sure you want to delete \"$blogpost\" (y/N)? " "y" - if test "$1" = "$BLOGDIR"; then - psed "/$blogpost/d" "$INDEX" "$BLOGINDEX" - psed "/<\!--BEGIN $blogpost-->/,/<\!--END $blogpost-->/d" "$RSSFILE" + confirmact "Are you sure you want to delete \"${blogpost}\" (y/N)? " "y" + if test "${1}" = "${BLOGDIR}"; then + psed "/${blogpost}/d" "${INDEX}" "${BLOGINDEX}" + psed "/<\!--BEGIN ${blogpost}-->/,/<\!--END ${blogpost}-->/d" "${RSSFILE}" fi - rmcontents "$1" && echo "Removed $blogpost." + rmcontents "${1}" && echo "Removed ${blogpost}." } publish() { confirmact "Publish post (y/N)? " "y" - title=$(titleget "$DRAFTDIR/$blogpost.final.html") + title=$(titleget "${DRAFTDIR}/${blogpost}.final.html") # bad? - psed "s/^/\ \ \ \ \ \ \ \ /" "$DRAFTDIR/$blogpost.html" - psed "/<\!--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/$blogpost.html" + psed "s/^/\ \ \ \ \ \ \ \ /" "${DRAFTDIR}/${blogpost}.html" + psed "/<\!--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}/${blogpost}.html" printf "\t\t<li>%s &ndash; <a href=\"%s\">%s</a></li>\n" \ - "$(date '+%Y %b %d')" "$BLOGDIR/$blogpost.html" "$title" | - expand -t8 > "$DRAFTDIR/$blogpost.final-htmlentry" - rsscreate | expand -t8 > "$DRAFTDIR/$blogpost.final-rssentry" + "$(date '+%Y %b %d')" "${BLOGDIR}/${blogpost}.html" "${title}" | + expand -t8 > "${DRAFTDIR}/${blogpost}.final-htmlentry" + rsscreate | expand -t8 > "${DRAFTDIR}/${blogpost}.final-rssentry" # using || because of psed blogindexupdate - psed "/<\!--BLOG $(date '+%B %Y')-->/r $DRAFTDIR/$blogpost.final-htmlentry" \ - "$BLOGINDEX" || echo "Blogindex... done." - psed "/<\!--BLOG-->/r $DRAFTDIR/$blogpost.final-htmlentry" "$INDEX" || echo "Index... done" - psed "/<\!--BLOG-->/r $DRAFTDIR/$blogpost.final-rssentry" "$RSSFILE" || echo "RSS... done" + psed "/<\!--BLOG $(date '+%B %Y')-->/r ${DRAFTDIR}/${blogpost}.final-htmlentry" \ + "${BLOGINDEX}" || echo "Blogindex... done." + psed "/<\!--BLOG-->/r ${DRAFTDIR}/${blogpost}.final-htmlentry" "${INDEX}" || echo "Index... done" + psed "/<\!--BLOG-->/r ${DRAFTDIR}/${blogpost}.final-rssentry" "${RSSFILE}" || echo "RSS... done" rmlastentry || echo "Removing last entry from index file... done" - rmcontents "$DRAFTDIR" && echo "Cleaning up $DRAFTDIR... done" - echo "Published $blogpost." + rmcontents "${DRAFTDIR}" && echo "Cleaning up ${DRAFTDIR}... done" + echo "Published ${blogpost}." } rsscreate() { - printf "<!--BEGIN %s-->\n" "$blogpost" + printf "<!--BEGIN %s-->\n" "${blogpost}" printf "<item>" - printf "\t<title>%s</title>\n" "$title" - printf "\t<guid>%s</guid>\n" "$WEBSITE/$BLOGDIR/$blogpost.html" + printf "\t<title>%s</title>\n" "${title}" + printf "\t<guid>%s</guid>\n" "${WEBSITE}/${BLOGDIR}/${blogpost}.html" printf "\t<pubDate>%s</pubDate>\n" "$(date '+%a, %d %b %Y')" printf "\t<description>\n" - printf "%s\n" "$(cat "$DRAFTDIR/$blogpost.xml")" + printf "%s\n" "$(cat "${DRAFTDIR}/${blogpost}.xml")" printf "\t</description>\n" printf "</item>\n" - printf "<!--END %s-->\n" "$blogpost" + printf "<!--END %s-->\n" "${blogpost}" } usage() { - printf "Usage: ${0##*/} [OPTION]\n\n" - printf "Options:\n" - printf " -n\t\tNew post\n" - printf " -p\t\tPublish draft post\n" - printf " -e\t\tEdit draft post\n" - printf " -v\t\tView draft post in browser\n" - printf " -t\t\tDelete draft post\n" - printf " -r\t\tRevise published post\n" - printf " -c\t\tChange title\n" - printf " -o\t\tView published post in browser\n" - printf " -d\t\tDelete published post\n" - printf " -l\t\tList all published posts\n" -} - -main "$1" + printf "Usage: ${0##*/} OPTION\n\n" + printf "options:\n" + printf " -n\tnew post\n" + printf " -p\tpublish draft post\n" + printf " -e\tedit draft post\n" + printf " -v\tview draft post in browser\n" + printf " -t\tdelete draft post\n" + printf " -r\trevise published post\n" + printf " -c\tchange title\n" + printf " -o\tview published post in browser\n" + printf " -d\tdelete published post\n" + printf " -l\tlist all published posts\n" +} + +main "${1}"