-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-article.sh
executable file
·56 lines (39 loc) · 1014 Bytes
/
new-article.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
#!/bin/bash
original=$(realpath "$(dirname "$0")")
if [[ -z $1 ]]; then
echo "Missing argument: storage directory." >&2
exit 1
fi
directory="$1"
if [[ -d $directory ]]; then
echo "Directory already exists: '$directory'." >&2
exit 2
fi
mkdir -p "$directory"
cd "$directory" || exit 4
IFS='' read -r -p "Title: " title
if [[ -z $title ]]; then
exit 8
fi
## Generate an ID and drop the metadata in the file.
id=$(date -Ins | sha256sum | head -c6)
cat >"__article__.py" <<INFO_END
{
"id": "$id",
"title": "$title",
"date": "$(date -I)",
"tags": [],
}
INFO_END
touch "article.md"
## Update store file with the path to the new article.
cd "$original" || exit 16
store_file="articles.py"
if [[ ! -f "$store_file" ]]; then
echo -e "{\n}" >"$store_file"
fi
cp "$store_file" "${store_file}_backup"
head -n-1 "$store_file" >"${store_file}_new"
echo " '$id': '$directory'," >>"${store_file}_new"
echo "}" >>"${store_file}_new"
mv "${store_file}_new" "${store_file}"