summaryrefslogtreecommitdiff
path: root/bash
diff options
context:
space:
mode:
authorgarhve <git@garhve.com>2022-12-12 11:52:12 +0800
committergarhve <git@garhve.com>2022-12-12 11:52:12 +0800
commitd7c8cb203eeabdc47ee42949f5acfaa64baef18e (patch)
treeab7ddf57a2cf659a3cca03ca36d8ca13b91a0ba0 /bash
parentbd90b7b7730abd351ad215c66765818c151016de (diff)
rewrite script to complete usage of new, edit, publishing blog
Diffstat (limited to 'bash')
-rwxr-xr-xbash/blog.sh110
1 files changed, 110 insertions, 0 deletions
diff --git a/bash/blog.sh b/bash/blog.sh
new file mode 100755
index 0000000..f6405ff
--- /dev/null
+++ b/bash/blog.sh
@@ -0,0 +1,110 @@
+#!/usr/bin/env sh
+
+######################################################################
+# @author : garhve (dev@garhve.com)
+# @file : blog
+# @created : Friday Dec 09, 2022 16:09:42 CST
+#
+# @description : simplify blog publishing.
+######################################################################
+
+# usage: prog [action] [option]
+
+#p="$(pwd)"
+#
+#cd "$HOME/Storage/data/blog"
+#
+#echo "Enter the blog name"
+#read -r name
+#
+#hugo new posts/"$name"
+#
+#echo "1. en"
+#echo "2. cn"
+#echo "all option would be default to 1 except 2[1 or 2]: "
+#read l
+#
+#if [ $l -eq 1 ]; then
+# lang="en"
+#elif [ $l -eq 2 ]; then
+# lang="cn"
+#else
+# lang="en"
+#fi
+#[ $lang == "cn" ] && mv "content/en/posts/$name" "content/cn/posts/$name"
+#vim "content/$lang/posts/$name"
+#
+#hugo
+#cd "public"
+#git add .
+#read -r -p "commit message: " com
+#git commit -m "$com"
+#
+#cd "$p"
+
+push() {
+ read -r -p "Do you want to push?[y/n] " ans
+ if [ "$ans" == 'y' ]; then
+ hugo
+ cd "public"
+ git add .
+ if [ ! -z "$(git status | grep 'Changes to be committed')" ]; then
+ read -r -p "commit message: " msg
+ git commit -m "$msg"
+ git push origin main
+ fi
+ echo -e "push done"
+ fi
+}
+
+edit() {
+ # --- define path
+ [ -z "$1" ] || [ "$1" == "en" ] && lang="en"
+ [ "$1" == "cn" ] && lang="cn"
+ path="content/$lang/posts"
+ # --- end
+
+ # --- get editing file
+ choice=1
+ declare -a arr
+ for file in $path/*; do
+ echo -ne "($choice)\x1b[0;32m${file##*/}\x1b[0m " # n get rid of trailing new line, e strip backslash
+ arr[$choice]="$file"
+ choice=$((choice+1))
+ done
+ echo ""
+ read -r -p "please choose file by number you want to edit from above [$lang]: " num
+ vim "${arr[$num]}"
+ # --- end
+
+ push
+ echo -e "\nall done"
+}
+
+new() {
+ read -r -p "blog name: " name
+ post="posts/$name"
+
+ [ -z "$1" ] || [ "$1" == "en" ] && lang="en"
+ [ "$1" == "cn" ] && lang="cn"
+ path="content/$lang/posts"
+ hugo new "${post}.md"
+ [ "$?" -eq 0 ] && echo "creation done"
+ [ "$?" -ne 0 ] && echo "creation fail, please check error" && exit 1
+
+ [ "$lang" == "cn" ] && mv "content/en/posts/$name.md" "$path/$name.md"
+
+ read -r -p "do you want to edit now?[y/n] " ans
+ [ "${ans,,}" == 'y' ] && vim "$path/$name.md" && echo "edit done"
+
+ push
+
+ echo -e "\nall done"
+}
+
+
+[ $# -lt 1 ] && echo "usage: $(basename $0) [action] [option]"
+
+[ $1 == "new" ] && new "$2"
+[ $1 == "edit" ] && edit "$2"
+[ $1 == "push" ] && push