-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzm
executable file
·73 lines (63 loc) · 2.74 KB
/
zm
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
#!/bin/bash
set -ue
[ -z "$EDITOR" ] && EDITOR="vi"
newpost() {
filecount=$(ls "authors" | wc -l)
[ $filecount -eq 0 ] && { echo "There are no authors." ; exit 1 ; }
read -erp "Title: " title
echo "$title" | grep \" >/dev/null && echo "Use unicode quotes in titles." && exit 1
url="$(echo "$title" | iconv -f UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed -E 's/-+/-/g' | sed 's/\(^-\|-$\)//g')"
[ -f "zerm/$url.md" ] && echo "There is already an article with the same name/URL." && exit 1
echo "$url,\"$title\"" >> drafts.csv
touch "zerm/$url.md"
$EDITOR "zerm/$url.md"
}
getDraftTo() {
files="$(cut -d, -f1 drafts.csv)"
filecount=$(wc -l drafts.csv | awk '{ print $1 }')
[ $filecount -eq 0 ] && echo "There's nothing to $1." && exit 1
number=1
[ $filecount -ne 1 ] && {
echo "$files" | nl
read -erp "Pick an entry by number to $1, or press Ctrl-C to cancel. " number
}
SELECTION="$(echo "$files" | nl | grep -w "$number" | awk '{ print $2 }')"
}
publish() {
id=$(tail -n 1 articles.csv | rev | cut -d, -f1 | rev)
id=$(($id + 1))
read -erp "Author ($(ls authors | tr '\n' ' ' | sed 's/\.html /, /g; s/, $//')): " author
read -erp "Published (\"dd.mm.yy hh:mm:ss CES?T\") [now]: " pub
[ "$pub" = "" -o "$pub" = "now" ] && pub="$(date '+%d.%m.%Y %H:%M:%S %Z')"
title="$(grep "^$1," drafts.csv | cut -d, -f2-)"
echo "$pub,$1,$title,$author,$id" >> articles.csv
# TODO: fix crash here because grep returns 1
grep -v "^$1," drafts.csv > tmp
mv -f tmp drafts.csv
read -erp "Commit? [y/N] " commit
[ "$commit" = "y" ] && git add -A && git commit -m "Published $title."
}
zerm2pdf() {
[ "$1" = "" ] && echo "Usage: zm PDF [year]" && exit 1
! grep "^$1," gas.csv >/dev/null && echo 'Year does not exist.' && exit 2
ids="$(grep "^$1," gas.csv | cut -d, -f2)"
for id in $ids ; do
/bin/echo -n "# "
grep ",$id$" articles.csv | cut -d, -f3- | rev | cut -d, -f3- | rev | sed 's/^"//;s/"$//'
perl -pe 's|\\{.*?\\}||g' "zerm/$(grep ",$id$" articles.csv | cut -d, -f2).md" \
| sed -E 's/ +,/,/g;s/ +/ /g'
done | cat -s | pandoc --pdf-engine=xelatex -o "$1.pdf"
}
case "$1" in
n*) newpost ;;
e*) getDraftTo edit && $EDITOR "zerm/$SELECTION.md" ;;
p*) getDraftTo publish && publish "$SELECTION" ;;
P*) zerm2pdf $2 ;;
*) echo "zm2 by ZERM <[email protected]>
Usage:
$0 n[ew]
$0 e[dit]
$0 p[ublish]
$0 P[DF]
See https://github.com/ZERMZeitung/zm2 for more." ;;
esac