Repository navigation
Expand file tree
/
Copy pathnew-post
More file actions
executable file
·59 lines (47 loc) · 1.46 KB
/
Copy pathnew-post
File metadata and controls
executable file
·59 lines (47 loc) · 1.46 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash
#
# file: new-post
# author: Brian Buccola <brian.buccola@gmail.com>
#
# description: Create a file (for a new Jekyll/Hakyll/etc. post) from an input title.
# Where are your posts?
post_dir="$HOME/repos/website/posts"
# Convert uppercase to lowercase, whitespace to '-', and '/' to '-', and delete
# any character that is not alphanumeric or '-'.
convert_string() {
echo "$1" \
| tr [:upper:] [:lower:] \
| sed 's/\s\+/-/g' \
| sed 's,/,-,g' \
| tr -dc '[:alnum:]-'
}
# Print message and exit 1.
die() {
echo "$@"
exit 1
}
# Check that we're in $post_dir.
[[ "$(pwd)" == "$post_dir" ]] || die "Go to $post_dir."
# Get title and turn it into a post filename.
read -p "title: " title
read -p "tags [as, a, list]: " tags
post="$(date +"%Y-%m-%d")-$(convert_string "$title").md"
# Check that the post doesn't already exist.
[[ -f "$post" ]] && die "The file \"$post\" already exists."
# Create a temp post, for reading YAML frontmatter from.
tmp_post=$(mktemp --tmpdir --suffix .md blog-post-XXXXXXXXXX)
# Escape any occurrence of '"' in $title.
title=$(echo "$title" | sed 's/"/\\"/g')
# Create some basic YAML frontmatter.
cat <<EOT > "$tmp_post"
---
title: "$title"
date: $(date +"%Y-%m-%d %H:%M")
tags: $tags
---
EOT
# Edit the post, reading $tmp_post into it and putting the cursor on the last
# line in insert mode.
vim -c "silent read $tmp_post" + +startinsert "$post"
# Clean up.
rm "$tmp_post"