lb/lb

91 lines
4.5 KiB
Plaintext
Raw Normal View History

2018-10-17 14:56:46 +00:00
#!/usr/bin/env sh
2018-05-16 21:46:29 +00:00
# Set your personal data here:
rssfile="rss.xml"
blogfile="2018.html"
website="https://lukesmith.xyz/"
2018-08-11 23:31:17 +00:00
indexfile="blogindex.html"
stylesheet="..\/style.css" # Characters should be escaped for this variable.
author="<a href=\"$website\">Luke Smith<\/a>"
2018-05-16 21:46:29 +00:00
dir=$(pwd)
2018-09-03 03:46:37 +00:00
draftdir="$dir"/blog/.drafts
blogdir="$dir"/blog
# Set vi if $EDITOR not set
2018-10-17 14:56:46 +00:00
[ -z $EDITOR ] && EDITOR="vi";
# See other variables defined later, such as `header` and `webdate` for more customizability.
2018-05-16 21:46:29 +00:00
getHelp() { \
echo -e "This system will take the blog entries you write and add them into the RSS file and HTML file of your choosing. Set which files to use by editing the 'lb' file directly and changing the variables at the top.\n"
echo -e "To fully prepare these files, add the following line to both files where you want the content to be added:\n"
echo -e "<!-- LB -->\n"
echo -e "Usage: 'new': create new draft; 'finalize': finish draft and add it to the RSS and HTML page; 'delete': delete a finished page.\n"
echo -e "As you finalize posts, they will be appended in the appropriate format below that line.\n"
echo -e "Blog posts will be stored in 'blog/' and drafts will be in 'blog/.drafts/'. To delete drafts, you only need delete their .html files directly, but use the 'lb delete' command to remove finalized posts since they appear in three places."
}
listandReturn() { \
echo "Listing contents of $1."
ls -rc $1 | awk -F '/' '{print $NF}' | nl
2018-08-11 23:31:17 +00:00
read -rp "Pick an entry by number to $2, or press ctrl-c to cancel. " number
chosen=$(ls -rc $1 | nl | grep -w "$number" | awk '{print $2}')
2018-05-16 21:46:29 +00:00
}
getTitle() { \
2018-09-03 03:46:37 +00:00
echo "Post will be stored as draft in $draftdir until finalized."
2018-08-11 23:31:17 +00:00
read -rp "Give a title for your post: " title
2018-09-03 23:39:22 +00:00
url=$(echo "$title" | tr -d '[:punct:]' | tr " " "-" | tr '[:upper:]' '[:lower:]')
2018-09-03 16:57:04 +00:00
grep "$url" "$blogfile" &>/dev/null && lbdupnum=1 && while [ grep "$url" "$blogfile" ]; do lbdupnum=$((lbdupnum+1)); done
[ ! -z ${lbdupnum+x} ] && url="$url"-"$lbdupnum"
2018-09-03 23:39:22 +00:00
return 0
2018-09-03 16:57:04 +00:00
}
2018-05-16 21:46:29 +00:00
postNew() { \
2018-09-03 03:46:37 +00:00
mkdir -p "$draftdir"
2018-09-03 23:39:22 +00:00
echo -e "<h2 id='$url'>$title</h2>\n<small>[<a href=\"$blogfile#$url\">link</a>&mdash;<a href=\"blog/$url.html\">standalone</a>]</small>\n\n<++>" >> "$draftdir"/$url.html && $EDITOR "$draftdir"/$url.html
}
2018-05-16 21:46:29 +00:00
finalize() { \
2018-09-03 03:46:37 +00:00
url=$(grep -o "<h2 id='\(.\)*'>" "$draftdir"/"$chosen" | cut -d "'" -f2)
title=$(grep -o "<h2 id='\(.\)*h2>" "$draftdir"/"$chosen" | sed -e 's/<[^>]*>//g')
echo "AddDescription \"$title\" $chosen" >> "$blogdir"/.htaccess
2018-05-16 21:46:29 +00:00
rssdate=$(date '+%a, %d %b %Y %H:%M:%S %z')
webdate=$(date '+%a, %d %b %Y %H:%M:%S %z')
2018-08-11 23:31:17 +00:00
listformat='<li>'$(date '+%Y %b %d')' &ndash; <a href="blog/'$url'.html">'$title'</a></li>'
2018-05-16 21:46:29 +00:00
tmpdir=$(mktemp -d)
2018-09-03 03:46:37 +00:00
echo -e "\n<item>\n<title>$title</title>\n<guid>$website$blogfile#$url</guid>\n<pubDate>$rssdate</pubDate>\n<description><![CDATA[\n$(awk 'a==1;/^$/{a=1}' "$draftdir"/"$chosen")\n]]></description>\n</item>\n"> $tmpdir/rss.xml
echo -e "\n<div class=\"entry\">\n$(cat "$draftdir"/"$chosen")\n<small>$webdate</small>\n</div>\n" > $tmpdir/html.html
2018-05-16 21:46:29 +00:00
sed -i "/<!-- LB -->/r $tmpdir/html.html" $blogfile
sed -i "/<!-- LB -->/r $tmpdir/rss.xml" $rssfile
2018-08-11 23:31:17 +00:00
sed -i "/<!-- LB -->/a $listformat" $indexfile
2018-05-20 01:56:44 +00:00
header="<html>\n<head>\n<title>$title<\/title>\n<link rel='stylesheet' type='text\/css' href='$stylesheet'>\n<meta charset='utf-8'\/>\n<\/head>\n<body>"
2018-05-19 20:58:23 +00:00
footer="<\/body>\n<footer>by <strong>$author<\/strong><\/footer>\n<\/html>"
2018-09-03 03:46:37 +00:00
sed "s/href=\"/href=\"..\//g;s/\.\.\/http/http/g;0,/^/s//$header/;0,/<h2 id=/s//<h1 id=/;0,/h2>/s//h1>/;\$a$footer" "$draftdir"/"$chosen" > "$blogdir"/"$chosen"
rm "$draftdir"/"$chosen"
2018-05-16 21:46:29 +00:00
}
delete() { \
2018-09-03 14:18:49 +00:00
base=$(basename $(echo "$chosen" | cut -f1 -d'.'))
2018-08-11 23:31:17 +00:00
read -rp "Really delete \"$base\"? (y/N) " choice
2018-05-16 21:46:29 +00:00
[[ $choice =~ [Yy] ]] || exit
2018-10-17 10:53:10 +00:00
sed -i "/<item/{:a;N;/<\/item>/!ba};/#$base<\/guid/d" $rssfile && echo "Entry removed from RSS feed file."
sed -i "/<div/{:a;N;/<\/div>/!ba};/id='$base'/d" $blogfile && echo "HTML code removed from blogfile."
2018-08-11 23:31:17 +00:00
sed -i "/<li>.*<a href=\"blog\/$base.html\">/d" $indexfile && echo "Index file entry removed."
2018-09-03 14:18:49 +00:00
rm "$chosen" && echo "Blog post deleted from directories."
2018-05-16 21:46:29 +00:00
}
2018-07-30 01:56:18 +00:00
discard() { \
2018-08-11 23:31:17 +00:00
base=$(echo "$chosen" | cut -f1 -d'.')
2018-09-03 23:39:11 +00:00
read -rp "Really discard \"$(basename $chosen)\"? (y/N) " choice
2018-07-30 01:56:18 +00:00
[[ $choice =~ [Yy] ]] || exit
rm "$chosen" && echo "Blog draft discarded."
2018-07-30 01:56:18 +00:00
}
2018-05-16 21:46:29 +00:00
case "$1" in
new) getTitle && postNew ;;
2018-10-17 14:56:19 +00:00
discard) listandReturn "$draftdir/*.html" discard && discard ;;
finalize) listandReturn "$draftdir" finalize && finalize ;;
delete) listandReturn "$blogdir/*.html" delete && delete ;;
2018-09-03 03:46:37 +00:00
edit) listandReturn "$draftdir/*.html" edit && vim "$chosen" ;;
2018-05-16 21:46:29 +00:00
*) getHelp ;;
esac