~bpv/sbs

ref: 1538443bed33e6bb7327abdcfc809c02f532d762 sbs/sbs -rwxr-xr-x 3.0 KiB
1538443b — Bryce Vandegrift Almost finished with publish feature 6 months ago
                                                                                
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
#!/bin/sh

# Static Blogging System: A blog generation script
# 2021 Bryce Vandegrift
# This program is licensed under the GPL V3
# Report bugs to: bpv@disroot.org

name="Bryce Vandegrift"
website="https://brycevandegrift.xyz"
website_dir="/home/bryce/Documents/website" # Absolute path to website directory
blog_dir="blog"
blogindex="blog/index.html"
rssindex="rss.xml"
draft_dir=".drafts"
html_css_path="//brycevandegrift.xyz/index.css" # Path to css stylesheet
html_icon_path="//brycevandegrift.xyz/p/icon.ico" # Path to web icon

default() {
	[ -z "$1" ] && help || echo "ERROR: $1 is not a valid command!" && exit 1
}

listDrafts() {
	echo "Printing drafts"
	case "$(ls "$website_dir/$draft_dir" | wc -l)" in
		0) echo "There are no drafts." && exit 1 ;;
		*) ls -rc "$website_dir/$draft_dir" | nl
		read -rp "Pick a draft to $1: " number ;;
	esac
	choice="$(ls -rc "$website_dir/$draft_dir" | nl | grep -w $number | awk '{print $2}')"
}

new() {
	read -rp "Enter file name: " name
	echo $name | grep -q "\"" && echo "Double quotes are not allowed in file names." && exit 1
	name="$(echo $name | tr -d '[:punct:]' | tr ' ' '-' | tr '[:upper:]' '[:lower:]')"
	( [ -f "$website_dir/$draft_dir/$name.md" ] || [ -f "$website_dir/$blog_dir/$name.html" ] ) && echo "A draft or post already exists under that name!" && exit 1
	$EDITOR "$website_dir/.drafts/$name.md"
}

edit() {
	listDrafts edit "$@"
	$EDITOR "$website_dir/$draft_dir/$choice"
}

publish() {
	listDrafts publish "$@"
	dir_name=$(echo $choice | sed 's/.md$//g')
	date=$(date '+%a, %d %b %Y %H:%M:%S %z')
	url="$website/blog/$dir_name"
	contents=$(pandoc -f markdown -t html "$website_dir/$draft_dir/$choice")
	title=$(echo "$contents" | grep "<h1.*</h1>$" | sed 's/<\/h1>$//g' | sed 's/^<h1\s.*>//g')
	tmp_dir=$(mktemp -d)
	printf "<!DOCTYPE html>\n<html lang=\"en\" dir=\"ltr\">\n<head>\n<meta charset=\"utf-8\">\n<link rel=\"icon\" href='%s' type=\"image/x-icon\">\n<link rel=\"stylesheet\" href='%s'>\n<title>%s</title>\n</head>\n<body>\n%s\n</body>\n</html>" "$html_icon_path" "$html_css_path" "$title" "$contents" > $tmp_dir/blog.html
	printf "\n<item>\n<title>%s</title>\n<guid>%s</guid>\n<link>%s</link>\n<pubDate>%s</pubDate>\n<description>\n%s\n</description>\n</item>" "$title" "$url.html" "$url.html" "$date" "$contents" > $tmp_dir/rss.xml
	printf "\n<li>%s - <a href='%s'>%s</a></li>" "$(date '+%b %d, %Y')" "$url.html" "$title" > $tmp_dir/preview.html
}

delete() {
	listDrafts delete "$@"
	rm "$website_dir/$draft_dir/$choice"
}

help() {
	cat <<-EOF
Usage:
        sbs [OPERATION] ...
Operations:
        sbs new
        sbs edit
	sbs publish
	sbs delete
        sbs help
                Show this text
        sbs version
                Show program version
EOF
}

version() {
	echo "sbs: Static Blogging System V1.0.0"
}

[ -z "$EDITOR" ] && EDITOR="vi"

case "$1" in
	new) shift;	new "$@" ;;
	edit) shift;	edit "$@" ;;
	publish) shift;		publish "$@" ;;
	delete|del) shift;	delete "$@" ;;
	help|-h)	help ;;
	version|-v)	version ;;
	*)	default "$@" ;;
esac
exit 0