-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-post.sh
executable file
·112 lines (93 loc) · 2.44 KB
/
new-post.sh
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/bin/bash
# Initialize variables with default values
keywords=""
description=""
excerpt=""
POST_TITLE=""
# Function to display usage information
usage() {
echo "Usage: $0 <POST_TITLE> [-k <arg>] [-d <arg>] [-e <arg>]"
echo " <POST_TITLE>: The title of the post"
echo " -k: Optional keywords for the post"
echo " -d: Optional description for the post"
echo " -e: Optional excerpt to show for the post"
exit 1
}
# Check if there's a required argument
if [ $# -eq 0 ]; then
echo "Error: Required argument is missing."
usage
fi
# Assign the required argument
POST_TITLE="$1"
# Shift the parsed required argument out of the argument list
shift
# Parse command-line options
while getopts "k:d:e:" opt; do
case $opt in
k)
keywords="$OPTARG"
;;
d)
description="$OPTARG"
;;
e)
excerpt="$OPTARG"
;;
\?)
echo "Invalid option: -$OPTARG" >&2
usage
;;
esac
done
# # Your script logic goes here
# echo "Required Argument: $(date +'%Y-%m-%d-')$POST_TITLE.md"
# echo "Optional Flag 1: $keywords"
# echo "Optional Flag 2: $description"
# echo "Optional Flag 3: $excerpt"
NEW_POST_FILE="./_posts/""$(date +'%Y-%m-%d-')""${POST_TITLE}"".md"
# description
function create_post() {
local keywords="$1"
local description="$2"
local excerpt="$3"
touch "$NEW_POST_FILE"
cat << EOF >> "$NEW_POST_FILE"
---
layout: post
title: $POST_TITLE
author: dnck
date: $(date +'%Y-%m-%d')
comments: true
analytics: true
keywords: $keywords
description: $description
category: blog
show_excerpt: true
excerpt: $excerpt
---
EOF
}
function main() {
local keywords="$1"
local description="$2"
local excerpt="$3"
if [ -n "$keywords" ] && [ -n "$description" ] && [ -n "$excerpt" ]; then
create_post "$keywords" "$description" "$excerpt"
elif [ -n "$keywords" ] && [ -n "$description" ]; then
create_post "$keywords" "$description" ""
elif [ -n "$keywords" ] && [ -n "$excerpt" ]; then
create_post "$keywords" "" "$excerpt"
elif [ -n "$description" ] && [ -n "$excerpt" ]; then
create_post "" "$description" "$excerpt"
elif [ -n "$keywords" ]; then
create_post "$keywords" "" ""
elif [ -n "$description" ]; then
create_post "" "$description" ""
elif [ -n "$excerpt" ]; then
create_post "" "" "$excerpt"
else
create_post "" "" ""
fi
}
main "$keywords" "$description" "$excerpt"