summaryrefslogtreecommitdiff
path: root/bash/blog.sh
blob: f627aea65da4705c444d963096c3a1932be5478a (plain)
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
#!/usr/bin/env sh

######################################################################
# @author      : garhve (dev@garhve.com)
# @file        : blog
# @created     : Friday Dec 09, 2022 16:09:42 CST
#
# @description : simplify blog publishing, it only can use in blog dir
######################################################################

# usage: prog new/edit/push [en/cn]

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) new/edit/push [en/cn]"

[ $1 == "new" ] && new "$2"
[ $1 == "edit" ] && edit "$2"
[ $1 == "push" ] && push