-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-post.sh
More file actions
executable file
·38 lines (29 loc) · 1.01 KB
/
new-post.sh
File metadata and controls
executable file
·38 lines (29 loc) · 1.01 KB
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
#!/bin/bash
## Creates a new post from the template file inside _posts using the current timestamp and a supplied title.
title=$1
if [[ -z "$title" ]] ; then
echo "Usage: $0 post_title"
exit 1
fi
HERE=$(dirname $0)
TEMPLATE_FILE="$HERE/post-template.markdown"
today=$(date +%Y-%m-%d)
now=$(date +%H):00:00
# take the title, replace spaces with dashes, remove characters like double-quotes, and lowercase it
title_filename=$(echo "$title" \
| tr ' ' '-' \
| tr -d '"' \
| tr '[:upper:]' '[:lower:]')
filename="${today}-${title_filename}.markdown"
dest_path="$HERE/_posts/$filename"
if [ -e "$dest_path" ] ; then
echo "ERROR: Path $dest_path already exists. Will not clobber."
exit 1
fi
# Start with the template file, and replace the title and date with the correct values,
# outputting the result into dest_path, the new post file
sed -e 's/^title: ""/title: "'"$title"'"/' \
-e 's/^date:.*$/date: '"${today} ${now}"'/' \
< "$TEMPLATE_FILE" \
> "$dest_path"
echo "Created $dest_path"