-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathb.sh
executable file
·135 lines (116 loc) · 2.92 KB
/
b.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
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
130
131
132
133
#!/bin/bash
CMD=$1
SRC=$2
GEN_HTML=/tmp/draft.html
GEN_PREVIEW=/tmp/preview.html
RC=$(pwd)/b.rc
gen() {
# Only generate HTML file when needed
if [[ -f "$GEN_HTML" ]] && [[ $(stat -c %y "$SRC") == $(stat -c %y "$GEN_HTML") ]]; then
return
fi
case "$markup" in
mkd)
MARKUP='markdown2 --extras=code-friendly,footnotes=^'
;;
rst)
MARKUP='my-rst2html.py'
;;
*)
echo 'Unknown markup language: $markup' >&2
exit 1
esac
if [[ $HAS_HEADER == yes ]]; then
$MARKUP <(sed '1,/^$/d' "$SRC") > "$GEN_HTML"
else
$MARKUP "$SRC" > "$GEN_HTML"
fi
sed "s/%%Title%%/$title/" tmpl/tmpl1.html > "$GEN_PREVIEW"
cat "$GEN_HTML" >> "$GEN_PREVIEW"
cat tmpl/tmpl2.html >> "$GEN_PREVIEW"
# match the timestamp
touch -r "$SRC" "$GEN_HTML" "$GEN_PREVIEW"
}
get_headers() {
sed -n '1d;/^$/q;p' "$SRC"
}
insert_url() {
if [[ $HAS_HEADER == yes ]] && get_headers | grep 'url=' >/dev/null; then
return
fi
# extract the URL and insert into source of markup file
URL=$(echo "$RESULT" | sed -n '/Post \(created\|updated\)/ s/Post \(created\|updated\): //p')
if [[ $URL == http* ]]; then
sed -i "1 a\\url=$URL" "$SRC"
if [[ $HAS_HEADER == no ]]; then
sed -i "1 a\\!b" "$SRC"
fi
fi
}
post() {
if [[ $HAS_HEADER == yes ]] && get_headers | grep 'url=' >/dev/null; then
echo "Found a url in header, about posting!" >&2
exit 1
fi
RESULT=$(google blogger post --blog "$blog" --title "$title" -t "$tags" --src "$GEN_HTML" 2>&1)
echo "$RESULT"
insert_url
}
update() {
if [[ ! -z $new_title ]]; then
# need to update title
RESULT=$(google blogger update --blog "$blog" --title "$title" \
--new-title "$new_title" -t "$tags" --src "$GEN_HTML" 2>&1)
if [[ $RESULT == 'Post updated*' ]]; then
# new_title=... -> title=...
sed -i '1,/^$/ {/^title/d}' "$SRC"
sed -i '1,/^$/ s/^new_title/title/' "$SRC"
fi
else
RESULT=$(google blogger update --blog "$blog" --title "$title" -t "$tags" --src "$GEN_HTML" 2>&1)
fi
echo "$RESULT"
insert_url
}
check_header() {
if [[ $(head -1 "$SRC") == *!b* ]]; then
HAS_HEADER=yes
_blog=$(get_headers | sed -n '/^blog=/ {s/^blog=//;p}')
blog=${blog:-$_blog}
title=$(get_headers | sed -n '/^title=/ {s/^title=//;p}')
new_title=$(get_headers | sed -n '/^new_title=/ {s/^new_title=//;p}')
tags=$(get_headers | sed -n '/^tags=/ {s/^tags=//;p}')
url=$(get_headers | sed -n '/^url=/ {s/^url=//;p}')
else
HAS_HEADER=no
fi
markup=${SRC##*.}
_title=$(basename "$SRC")
_title="${_title%.*}"
title=${title:-$_title}
unset _blog _title
}
# Source configuration file, currently only for $blog
if [[ -f "$RC" ]]; then
source "$RC"
fi
case "$CMD" in
gen)
check_header
gen
;;
post)
check_header
gen
post
;;
update)
check_header
gen
update
;;
*)
echo "Unknown command: $CMD" >&2
exit 1
;;
esac