~dricottone/my-utils

my-utils/documents/hugo-post -rwxr-xr-x 1.4 KiB
ef6d4cd1Dominic Ricottone Incomplete work on hardware/media utils 4 days 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
#!/bin/sh

name="hugo-post"
version="1.0"
usage_message="Usage: hugo-post [OPTIONS] TITLE"
help_message=$(/usr/bin/cat <<-EOF
	Create a new Hugo post
	Usage: hugo-post [OPTIONS] TITLE
	Options:
	 -h, --help     print this message and exit
	 -q, --quiet    suppress error messages
	 -v, --version  print version number and exit
EOF
)

. /usr/local/lib/myminiparse.sh

# loop through arguments
title=
for arg; do
  case "$arg" in
  -q|--quiet)
    #ignore these
    ;;

  *)
    if [ -z "$title" ]; then
      title="$arg"
    fi
    ;;
  esac
done

# error if no title given
if [ -z "$title" ]; then
  (>&2 /usr/bin/printf "%s\n" "$usage_message")
  exit 1
fi

# check if title can be made into a new filename
filename="$(/usr/bin/printf "$title" | /usr/bin/tr --squeeze-repeat ' ' '_' | /usr/bin/tr --complement --delete '[:alnum:]_-').md"
path="content/posts/${filename}"
if [ ! -d "content" ] || [ ! -d "content/posts" ]; then
  if [ "$quiet" -eq 0 ]; then
    (>&2 /usr/bin/printf "%s: not in a Hugo directory\n" "$name")
  fi
  exit 1
elif [ -f "$path" ]; then
  if [ "$quiet" -eq 0 ]; then
    (>&2 /usr/bin/printf "%s: '%s' already exists\n" "$name" "$filename")
  fi
  exit 1
elif [ -d "$path" ]; then
  if [ "$quiet" -eq 0 ]; then
    (>&2 /usr/bin/printf "%s: '%s' is a directory\n" "$name" "$filename")
  fi
  exit 1
fi

# create new file
/usr/bin/printf -- '---\ntitle: %s\ndate: %s\ndraft: true\n---\n\n' "$title" "$(hugo-date)" > "$path"