forked from LukeSmithxyz/lb
/
lb
executable file
·95 lines (86 loc) · 5.74 KB
/
lb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
name="Niklas"
webdir="/srv/http"
website="https://192.168.0.31/" # Keep a trailing slash.
css="style.css"
blogfile="index.html"
indexfile="blogindex.html"
rssfile="rss.xml"
[ -z "$EDITOR" ] && EDITOR="vim"
update() { \
cp "$blogfile" "$webdir/$blogfile"
cp "$rssfile" "$webdir/$rssfile"
cp "$indexfile" "$webdir/$indexfile"
cp "$css" "$webdir/$css"
}
[ ! -d "$webdir/blog/.drafts" ] &&
read -erp "Initialize blog in $webdir?" ask &&
if [ "$ask" = "y" ]; then
printf "Initializing blog system...\\n"
mkdir -pv "$webdir/blog/.drafts" || printf "Error. Do you have write permissions in this directory?\\n"
mkdir -pv "$webdir/blog/.markdown"
echo "Options +Indexes" > "$webdir/blog/.htaccess"
update
else
exit
fi
newpost() { read -erp "Give a title for your post:
" title
echo "$title" | grep "\"" >/dev/null && printf "Double quotation marks (\") are not allowed in entry titles.\\n" && exit
url="$(echo "$title" | iconv -cf UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")"
echo "AddDescription \"$title\" \"$url.html\"" >> "$webdir/blog/.htaccess" || { echo "Error: Is htaccess writeable?"; exit; }
[ -f "$webdir/blog/.drafts/$url.md" ] && echo "There is already an existing draft entry of that same name/URL." && exit
[ -f "$webdir/blog/$url.html" ] && echo "There is already an existing published entry of that same name/URL." && exit
$EDITOR "$webdir/blog/.drafts/$url.md" ;}
listandReturn() { printf "Listing contents of %s.\\n" "$1"
ls -rc "$1" | awk -F '/' '{print $NF}' | nl
read -erp "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}')"
basefile="$(basename "$chosen")" && base="${basefile%.*}" ;}
publish() { \
delete
htmlfile="$base.html"
htaccessentry=$(grep "$htmlfile" "$webdir/blog/.htaccess")
realname="$(echo "$htaccessentry" | cut -d'"' -f2)"
rssdate="$(grep "$htmlfile" $webdir/blog/.htaccess | sed "s/.*\.html\"* *#*//g" | tr -d '\n')"
[ -z "$rssdate" ] && rssdate="$(date '+%a, %d %b %Y %H:%M:%S %z')" # RSS date formats must comply with standards to validate.
webdate="$(date '+%a, %d %b %Y %H:%M:%S %z')" # But this visible date you can set to any format.
tmpdir=$(mktemp -d)
printf "<html>\\n<head>\\n<title>%s</title>\\n<link rel='stylesheet' type='text/css' href='%s'>\\n<meta charset='utf-8'/>\\n</head>\\n<body>\\n<h1>%s</h1>\\n<small>[<a href='%s#%s'>link</a>—<a href='%s'>standalone</a>]</small>\\n%s\\n<footer>by <strong><a href='%s'>%s</a></strong></footer>\\n</body>\\n\\n</html>" "$realname" "../$css" "$realname" "../$blogfile" "$base" "$htmlfile" "$(markdown "$webdir/blog/.drafts/$basefile")" "$website" "$name" > "$webdir/blog/$htmlfile"
printf "\\n<item>\\n<title>%s</title>\\n<guid>%s%s#%s</guid>\\n<pubDate>%s</pubDate>\\n<description><![CDATA[\\n%s\\n]]></description>\\n</item>\\n\\n" "$realname" "$website" "$blogfile" "$base" "$rssdate" "$(markdown "$webdir/blog/.drafts/$basefile")" > "$tmpdir/rss"
printf "<div class='entry'>\\n<h2 id='%s'>%s</h2>\\n<small>[<a href='#%s'>link</a>—<a href='%s'>standalone</a>]</small>\\n%s\\n<small>%s</small>\\n</div>\\n" "$base" "$realname" "$base" "blog/$htmlfile" "$(markdown "$webdir/blog/.drafts/$basefile")" "$webdate" > "$tmpdir/html"
printf "<li>%s – <a href=\"blog/%s\">%s</a></li>\\n" "$(date '+%Y %b %d')" "$htmlfile" "$realname" > "$tmpdir/index"
sed -i "/<!-- LB -->/r $tmpdir/html" "$blogfile"
sed -i "/<!-- LB -->/r $tmpdir/rss" "$rssfile"
sed -i "/<!-- LB -->/r $tmpdir/index" "$indexfile"
sed -i "/ \"$base.html\"/d" "$webdir/blog/.htaccess"
echo "AddDescription \"$realname\" \"$htmlfile\" #$rssdate" >> "$webdir/blog/.htaccess"
mv "$webdir/blog/.drafts/$chosen" "$webdir/blog/.markdown/$chosen"
update
}
confirm() { read -erp "Really $1 \"$base\"? (y/N) " choice && echo "$choice" | grep -i "^y$" >/dev/null || exit 1 ;}
delete() { \
sed -i "/<item/{:a;N;/<\\/item>/!ba};/#$base.html<\\/guid/d" $rssfile
sed -i "/<div/{:a;N;/<\\/div>/!ba};/id='$base'/d" $blogfile
sed -i "/<li>.*<a href=\"blog\\/$base.html\">/d" $indexfile
rm "$webdir/blog/.markdown/$base.md" 2>/dev/null
rm "$webdir/blog/$base.html" 2>/dev/null && printf "Old blog entry removed.\\n"
update ;}
revise() { \
#awk '/^<small>\[/{flag=1;next}/<footer>/{flag=0}flag' "$webdir/blog/$chosen" > "$webdir/blog/.drafts/$basefile"
mdfile="$base.md"
rm "$webdir/blog/$chosen"
mv "$webdir/blog/.markdown/$mdfile" "$webdir/blog/.drafts/$mdfile"
"$EDITOR" "$webdir/blog/.drafts/$mdfile"
printf "Revision stored in blog/.drafts. Publish as normal entry when desired.\\n" ;}
case "$1" in
n*) newpost ;;
e*) listandReturn "$webdir"/blog/.drafts/ edit && "$EDITOR" "$webdir/blog/.drafts/$chosen" ;;
p*) listandReturn "$webdir"/blog/.drafts/ publish && publish ;;
# TODO also remove file in blog/.markdown
t*) listandReturn "$webdir"/blog/.drafts/ trash && confirm trash && rm -f "$webdir/blog/.drafts/$chosen" && sed -i "/ \"$base.html\"/d" "$webdir/blog/.htaccess" ; printf "Draft deleted.\\n" ;;
d*) listandReturn "$webdir"/blog/ delete && confirm delete && delete && sed -i "/ \"$base.html\"/d" "$webdir/blog/.htaccess" ;;
r*) listandReturn "$webdir"/blog/ revise && revise ;;
u*) update && echo "Web directory updated." ;;
*) printf "lb blog system by Luke Smith <luke@lukesmith.xyz>\\nUsage:\\n lb n:\\tnew draft\\n lb e:\\tedit draft\\n lb p:\\tpublish/finalize draft\\n lb r:\\trevise published entry\\n lb u:\\tupdate files in web directory\\n lb t:\\tdiscard draft\\n lb d:\\tdelete published entry\\n\\nBe sure to have the following pattern added to your RSS feed, blog file and blog index:\\n\\n<!-- LB -->\\n\\nNew content will be added directly below that sequence. This is required.\\nSee https://github.com/LukeSmithxyz/lb for more.\\n" ;;
esac