commit 48aa2f5de05cab199e36ae5be3a0679117a20659
parent bd7e6c00af3c79a870d0510b23924c4865232ff5
Author: Christos Margiolis <christos@margiolis.net>
Date: Wed, 21 Oct 2020 03:55:13 +0300
changed man to BSD style, changed ls to gnuls and sed to gsed
Diffstat:
M | autoblog | | | 55 | ++++++++++++++++++++++++++++--------------------------- |
M | autoblog.1 | | | 123 | ++++++++++++++++++++++++++++++++++++++----------------------------------------- |
2 files changed, 87 insertions(+), 91 deletions(-)
diff --git a/autoblog b/autoblog
@@ -1,4 +1,5 @@
-#!/bin/sh
+#!/usr/local/bin/bash
+# make POSIX
# See LICENSE file for copyright and license details.
website="https://christosmarg.xyz"
@@ -9,23 +10,23 @@ blogindex="blogindex.html"
index="index.html"
rssfile="rss.xml"
template="template.html"
-[ -z "$EDITOR" ] && EDITOR="nano"
+[ -z "$EDITOR" ] && EDITOR="vi"
confirm_action() {
read -erp "$1" confirm && [ "$confirm" = "$2" ] || exit
}
listposts() {
- printf "Listing posts in %s (total: %d)\n" "$1" "$(ls $1 -I "*final*" | wc -l)"
- ls -rc $1 -I "*-final*" | awk -F '/' '{print $NF}' | nl
- [ $(($(ls $1 | wc -l))) -eq 0 ] && echo "No posts available in $1" && exit
+ 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 $(($(ls $1 | wc -l))) ] \
+ || [ $(($num)) -gt $(($(gnuls $1 | wc -l))) ] \
|| [ -z "$num" ] \
&& echo "No post selected." && exit
- blogpost=$(ls -rc $1 -I "*-final*" | nl | grep -w " $num" | awk '{print $2}' | sed "s/\..*//")
+ blogpost=$(gnuls -rc $1 -I "*-final*" | nl | grep -w " $num" | awk '{print $2}' | gsed "s/\..*//")
}
newpost() {
@@ -34,24 +35,24 @@ newpost() {
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"
- sed -i "s/^/\ \ \ \ \ \ \ \ /" $draftdir/$blogpost.html # bad?
- sed "s/TITLE/$title/g;s/HEADER/$title/g;s/AUTHOR/$author/g;" $template > $draftdir/$blogpost-final.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
}
publish() {
confirm_action "Publish post (y/N)? " "y"
- title=$(grep "<title>" $draftdir/$blogpost-final.html | sed "s/<title>//;s/<\/title>//;s/ *//;")
- sed -i "/<\!--BLOG-->/r $draftdir/$blogpost.html" $draftdir/$blogpost-final.html
- sed "s/</\</g;s/>/\>/g;" "$draftdir/$blogpost.html" > $draftdir/$blogpost.xml
+ 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/</\</g;s/>/\>/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 – <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
blogindex_update
- sed -i "/<\!--BLOG $(date '+%B %Y')-->/r $draftdir/$blogpost-htmlentry" $blogindex && echo "Blogindex... done."
- sed -i "/<\!--BLOG-->/r $draftdir/$blogpost-htmlentry" $index && echo "Index... done"
- sed -i "/<\!--BLOG-->/r $draftdir/$blogpost-rssentry" $rssfile && echo "RSS... done"
+ 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"
remove_last_index_entry && echo "Removing last entry from index file... done"
remove_contents $draftdir && echo "Cleaning up .drafts... done"
echo "Published $blogpost."
@@ -59,7 +60,7 @@ publish() {
# i dont like how this looks...
blogindex_update() {
- dateid=$(date '+%b %Y' | sed 's/\ //' | tr '[:upper:]' '[:lower:]')
+ dateid=$(date '+%b %Y' | gsed 's/\ //' | tr '[:upper:]' '[:lower:]')
if [ -z "$(grep "$dateid" $blogindex)" ]; then
datename=$(date '+%B %Y')
monthheader=$(printf "\\
@@ -67,9 +68,9 @@ blogindex_update() {
<ul> \\
\t<\!--BLOG %s--> \\
<\/ul>" \
- "$dateid" "$datename" "$datename" | sed 's/^/\t\t\t/' | expand -t4)
+ "$dateid" "$datename" "$datename" | gsed 's/^/\t\t\t/' | expand -t4)
- sed -i "/<\!--BLOG-->/a $monthheader" $blogindex
+ gsed -i "/<\!--BLOG-->/a $monthheader" $blogindex
fi
}
@@ -77,36 +78,36 @@ 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" ] &&
- sed -i "/$blogpost/d" $index $blogindex &&
- sed -ni "/<item>/{ :loop; N; s/<\\/item>/&/; T loop; s/$blogpost/&/; T keep; d }; :keep; p" $rssfile
+ gsed -i "/$blogpost/d" $index $blogindex &&
+ gsed -ni "/<item>/{ :loop; N; s/<\\/item>/&/; T loop; s/$blogpost/&/; T keep; d }; :keep; p" $rssfile
remove_contents $1 && echo "Removed $blogpost."
}
view() {
cat $draftdir/$blogpost-final.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
+ 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
$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 | sed "s/<title>//;s/<\/title>//;s/ *//;")
+ 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 ' ' '-')
- sed -i "s/$blogpost/$newtitle_fmt/g;s/$oldtitle/$newtitle/g" $blogdir/$blogpost.html $index $blogindex $rssfile &&
+ 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() {
- ls $1 | grep -w $blogpost | sed "s/^/$1\//" | xargs rm
+ gnuls $1 | grep -w $blogpost | gsed "s/^/$1\//" | xargs rm
}
remove_last_index_entry() {
- indexentries=$(sed "1,/<\!--BLOG-->/d" $index | grep "<li>") &&
+ indexentries=$(gsed "1,/<\!--BLOG-->/d" $index | grep "<li>") &&
[ $(($(echo "$indexentries" | wc -l))) -gt 7 ] &&
lastentry=$(echo "$indexentries" | tail -2 | head -1) &&
- sed -i "s|$lastentry||;" $index
+ gsed -i "s|$lastentry||;" $index
}
err() {
diff --git a/autoblog.1 b/autoblog.1
@@ -1,80 +1,75 @@
-.TH AUTOBLOG 1 autoblog\-VERSION
-.SH NAME
-autoblog \- automatic blog and RSS system
-.SH SYNOPSIS
-.B autoblog
-.RB [ \-n \ new\ post ]
-.RB [ \-p \ publish\ draft ]
-.RB [ \-e \ edit\ draft ]
-.RB [ \-v \ view\ draft ]
-.RB [ \-t \ delete\ draft ]
-.RB [ \-r \ revise\ published ]
-.RB [ \-c \ change\ title]
-.RB [ \-o \ view\ published ]
-.RB [ \-d \ delete\ published ]
-.RB [ \-l \ list\ all\ published ]
-.SH DESCRIPTION
-.B autoblog
+.Dd autoblog\-VERSION
+.Dt AUTOBLOG 1
+.Os
+.Sh NAME
+.Nm autoblog
+.Nd automatic blog and RSS system
+.Sh SYNOPSIS
+.Nm
+.Op Fl n Ar new post
+.Op Fl p Ar publish draft
+.Op Fl e Ar edit draft
+.Op Fl v Ar view draft
+.Op Fl t Ar delete draft
+.Op Fl r Ar revise published
+.Op Fl c Ar change title
+.Op Fl o Ar view published
+.Op Fl d Ar delete published
+.Op Fl l Ar list all published
+.Sh DESCRIPTION
+.Nm
generates a blog post page and RSS feed.
-.SH OPTIONS
+.Sh OPTIONS
The options are as follows:
-.TP
-.BR \-n
+.Bl -tag -width Ds
+.It Fl n Ar new post
Prompt the user to write a new blog post in HTML
-.TP
-.BR \-p
+.It Fl p Ar publish draft
Publish one of the posts stored in the .drafts directory.
-.TP
-.BR \-e
+.It Fl e Ar edit draft
Edit one of the posts stored in the .drafts directory.
-.TP
-.BR \-v
+.It Fl v Ar view draft
View one of the posts stored in the .drafts directory.
-.TP
-.BR \-t
+.It Fl t Ar delete draft
Delete one of the posts stored in the .drafts directory.
-.TP
-.BR \-r
+.It Fl r Ar revise published
Revise a published post stored in the blog directory.
-.TP
-.BR \-c
+.It Fl c Ar change title
Change a published post's title.
-.TP
-.BR \-o
+.It Fl o Ar view published
View a published post stored in the blog directory on your default browser.
-.TP
-.BR \-d
+.It Fl d Ar delete published
Delete a published post stored in the blog directory.
-.TP
-.BR \-l
+.It Fl l Ar list all published
List all posts stored in the blog directory.
-.P
+.El
+.Pp
Only one option at a time can be used.
-.P
+.Sh USAGE
+.Pp
Run the script inside your website's main directory and edit the website and author
to match your own information. Make sure that the rest of the variables are properly
-set to match your website's structure. By default, all blog posts are stored in
-.I blog/
-.P
-.B autoblog
-will search for <!--BLOG--> inside
-.I index.html
-and
-.I rss.xml
-in order to put the blog post listings and RSS feed respectively. Inside
-.I blogindex.html
+set to match your website's structure. By default, all blog posts are stored in blog/.
+.Sh REQUIREMENTS
+.Pp
+The following files have to exist in the same directory
+.Nm
+is called:
+.Bl -tag -width Ds
+.It index.html
+Main page.
+.It blogindex.html
+Blog page index.
+.It template.html
+A template file that will be used to format the blog post.
+The TITLE, HEADER and AUTHOR fields must exist and be left as is.
+.It rss.xml
+The RSS feed.
+.El
+.Pp
+.Nm
+will search for <!--BLOG--> inside index.html and rss.xml
+in order to put the blog post listings and RSS feed respectively. Inside blogindex.html
however, it will search for <!--BLOG [Month Year]--> (e.g <!--BLOG January 1800->).
-.P
-A
-.I template.html
-file needs to exist in your main directory in order for the
-script to run properly. That file is how you want your blog post's generated page
-to look like. The
-.I TITLE
-,
-.I HEADER
-and
-.I AUTHOR
-fields must exist and be left as is.
-.SH AUTHORS
-Christos Margiolis <christos@christosmarg.xyz>
+.Sh AUTHORS
+.An Christos Margiolis Aq Mt christos@christosmarg.xyz