aboutsummaryrefslogtreecommitdiffstats
path: root/gemlog-new
blob: 76da00c9ea2ec70aef0bd8d0ca59b8f33da60859 (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/bin/sh -e

# script to create new gemposts

GEMINI_HOME="$HOME/Documents/gemini-aaoth.xyz"
# gemlog page on your capsule, e.g. gemini://foo.bar/gemlog/
#                                                    ^----^
GEMLOG_DIR="gemlog"
GEMLOG_HOME="$GEMINI_HOME/$GEMLOG_DIR"
GEMLOG_INDEX="$GEMLOG_HOME/index.gmi"
GEMLOG_TEMPLATE="$GEMLOG_HOME/.template"
# gemlikes view binary location
GEMLIKES="/cgi-bin/gemlikes/view"

die() {
    echo "$1" && exit 1
}

usage() {
    echo "add new post to gemlog"
    echo 
    echo "this script is using gemini subscription specification and also \
utilizes gemlikes utility for likes and comments"
    echo
    echo "usage:"
    echo "  $0 [OPTIONS]"
    echo
    echo "options:"
    echo "  -T, --title <title>"
    echo "      specify new post title"
    echo "  -t, --tags  <tags>"
    echo "      specify new post tags (space separated, now they are just decorative)"
    echo "  -h, --help"
    echo "      print this help message"
}

read_title() {
    echo "new post title:"
    read -r _new_post_title
}

read_tags() {
    echo "new post tags:"
    read -r _new_post_tags
}

update_index() {
    sed "$_new_post_loc i => $_new_post_rel_url $_new_post_date $_new_post_title" < "$GEMLOG_INDEX" > "$GEMLOG_INDEX.tmp"
    mv "$GEMLOG_INDEX.tmp" "$GEMLOG_INDEX"
}

edit_post() {
    _new_post_gemlikes=$(echo "$_new_post_gemlikes" | sed 's/\//\\\//g')

    cp "$GEMLOG_TEMPLATE" "$_new_post_filename"

    [ -n "$_new_post_tags" ] && sed -e "s/{{ tags }}/\* tags: $_new_post_tags/" \
        < "$_new_post_filename" > "$_new_post_filename.tmp"
    mv "$_new_post_filename.tmp" "$_new_post_filename"

    sed -e '/{{ tags }}/d' < "$_new_post_filename" > "$_new_post_filename.tmp"
    mv "$_new_post_filename.tmp" "$_new_post_filename"

    sed -e "s/{{ title }}/$_new_post_title/" \
        -e "s/{{ gemlog_location }}/\/$GEMLOG_DIR\//" \
        -e "s/{{ date }}/$_new_post_date/" \
        -e "s/{{ gemlog_title }}/back to post list/" \
        -e "s/{{ gemlikes }}/${_new_post_gemlikes#/}/" < "$_new_post_filename" \
        > "$_new_post_filename.tmp"
    mv "$_new_post_filename.tmp" "$_new_post_filename"

    nvim -c ":Go" -c "normal 6j" -c "startinsert" "$_new_post_filename"
}

main() {
    if [ $# -gt 0 ]
    then
        while [ -n "$1" ]
        do
            case "$1" in
                --title|-T)
                    shift
                    _new_post_title="$1"
                    ;;
                --tags|-t)
                    shift
                    _new_post_tags="$1"
                    ;;
                --help|-h)
                    shift
                    usage
                    exit
                    ;;
                *)
                    usage
                    exit 1
                    ;;
            esac
            shift
        done
    fi

    [ -z "$_new_post_title" ] && read_title
    [ -z "$_new_post_title" ] && die "title could not be empty"

    [ -z "$_new_post_tags" ] && read_tags
    [ -z "$_new_post_tags" ] && echo "no tags specified"

    _new_post_date=$(date +%F)
    _new_post_title_for_file=$(echo "$_new_post_title" | tr '[:upper:]' '[:lower:]' \
        | sed 's/ /-/g')
    _new_post_basename="$_new_post_date-$_new_post_title_for_file.gmi"
    _new_post_filename="$GEMLOG_HOME/$_new_post_basename"
    _new_post_rel_url="/$GEMLOG_DIR/$_new_post_basename"
    _new_post_loc=$(awk '/^## posts/ { print NR+2 }' < "$GEMLOG_INDEX")
    _new_post_gemlikes="$GEMLIKES?$_new_post_basename"

    update_index

    edit_post

    {
        cd "$GEMINI_HOME"
        git add "$GEMLOG_INDEX" "$_new_post_filename"
        git commit && git push
    }
}

main "$@"