summaryrefslogtreecommitdiff
path: root/scripts/sh/blog
blob: 59de4357588931d70dde2d071f8bb9ec45fde6a9 (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
#!/bin/sh
# blog: write a blog entry

begins_with() { # haystack prefix
    value=$1
    prefix=$2

    case "$value" in
       "$prefix"*) return 0
    esac

    return 1
}

usage() {
    echo "blog [help | ls | ls-html | NAME]"
}

fuzzy() {
    FILE="$(find "$WWW_DEFAULT_PATH/src/blog" -type f | $FUZZY -l 1000)"
    [ -z "$FILE" ] && exit 0
    if ! begins_with "$FILE" "$WWW_DEFAULT_PATH/src/blog" ; then
	FILE="$WWW_DEFAULT_PATH/src/blog/$FILE"
    fi

    mkdir "$(dirname "$FILE")" -p 
    $EDITOR "$FILE"
}

list() {
    header="Date\tTitle\n"
    for f in $(find $WWW_DEFAULT_PATH/src/blog -type f | sort -k 3) ; do
	out="$out$(sed -n 2p $f)	$(sed -n 1p $f)\n"
    done
    out="$header$(printf "$out" | sort -rk 1)\n"
    printf "$out"
}

list_html() {
    header="<table>\n<tr><th>Date</th>\t<th>Title</th></tr>\n"
    for f in $(find $WWW_DEFAULT_PATH/src/blog -type f | sort -k 3) ; do
	out="$out\t<tr><td>$(sed -n 2p $f)</td><td><a href=\"/blog/$(basename "$f")\">$(sed -n 1p $f)</a></td></tr>\n"
    done
    out="$header$(printf "$out" | sort -rk 1)\n</table>\n"
    printf "$out"
}

[ -z "$WWW_DEFAULT_PATH" ] && echo "WWW_DEFAULT_PATH not set" && exit 1
[ -z "$EDITOR" ] && echo "EDITOR not set" && exit
[ -z "$FUZZY" ] && echo "FUZZY not set" && exit
[ "$1" = "help" ] && usage && exit 0
[ "$1" = "ls" ] && list && exit 0
[ "$1" = "ls-html" ] && list_html && exit 0

if [ -z "$1" ] || [ "$1" = "fuzzy" ] ; 
then
    fuzzy
    exit 0
fi

mkdir -p "$WWW_DEFAULT_PATH/src/blog"
$EDITOR "$WWW_DEFAULT_PATH/src/blog/$1"