This repository was archived by the owner on Feb 8, 2021. It is now read-only.
forked from LukeSmithxyz/lb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzm
More file actions
executable file
·158 lines (147 loc) · 6.67 KB
/
zm
File metadata and controls
executable file
·158 lines (147 loc) · 6.67 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
website_domain="zerm.eu"
shortener_domain="zerm.link"
twittername="ZERMZeitung"
css="../style.css"
blogfile="2021.html"
indexfile="index.html"
rssfile="rss.xml"
sitemapfile="sitemap.xml"
website="https://$website_domain/"
shortener="https://$shortener_domain/"
[ -z "$EDITOR" ] && EDITOR="vim"
mkdir -p "zerm/.drafts"
newpost() {
read -erp "Give a title for your article:
" title
echo "$title" | grep "\"" >/dev/null && echo "Use unicode quotes in titles." && exit
url="$(echo "$title" | iconv -f UTF-8 -t ASCII//TRANSLIT | tr -d '[:punct:]' | tr '[:upper:]' '[:lower:]' | tr ' ' '-' | sed "s/-\+/-/g;s/\(^-\|-\$\)//g")"
[[ $1 == n ]] && EXT=".html" || EXT=".md"
echo "AddDescription \"$title\" \"$url$EXT\"" >> "zerm/.htaccess" || { echo "Error: Is htaccess writeable?"; exit; }
[ -f "zerm/.drafts/$url$EXT" ] && echo "There is already an existing draft entry of that same name/URL." && exit
[ -f "zerm/$url$EXT" ] && echo "There is already an existing published entry of that same name/URL." && exit
touch "zerm/.drafts/$url$EXT"
$EDITOR "zerm/.drafts/$url$EXT"
}
listandReturn() {
filecount=$(ls "$1" | wc -l)
[ $filecount -eq 0 ] && echo "There's nothing to $2." && exit 1
number=1
[ $filecount -eq 1 ] || {
ls -rc "$1" | awk -F '/' '{print $NF}' | nl
read -erp "Pick an entry by number to $2, or press Ctrl-C to cancel. " number
}
chosen="$(ls -rc "$1" | nl | grep -w "$number" | awk '{print $2}')"
basefile="$(basename "$chosen")" && base="${basefile%.*}"
}
getExtension() {
: "${1%/}"
printf '%s\n' "${_##*.}"
}
publish() {
if [[ $(getExtension $chosen) == md ]]; then
markdown "zerm/.drafts/$chosen" > "zerm/.drafts/${chosen/.md/.html}"
rm -f "zerm/.drafts/$chosen"
chosen="${chosen/.md/.html}"
basefile="$(basename "$chosen")" && base="${basefile%.*}"
fi
delete
htaccessentry=$(grep "$base" "zerm/.htaccess")
realname="$(echo "$htaccessentry" | cut -d'"' -f2)"
rssdate="$(echo "$htaccessentry" | sed "s/.*\.html\"* *#*//g" | tr -d '\n')"
[ -z "$rssdate" ] && rssdate="$(LC_TIME=en_US date '+%a, %d %b %Y %H:%M:%S %z')" # RSS date formats must comply with standards to validate.
webdate="$(date '+%d.%m.%Y %H:%M:%S %Z')" # But this visible date you can set to any format.
tmpdir=$(mktemp -d)
filecount=$(ls ".authors" | wc -l)
[ $filecount -eq 0 ] && { echo "There are no authors." ; exit 1 ; }
number=1
[ $filecount -eq 1 ] || {
ls -rc ".authors" | awk -F '/' '{print $NF}' | nl
read -erp "Pick an author by number, or press Ctrl-C to cancel. " number
}
author_name="$(basename "$(ls -rc ".authors" | nl | grep -w "$number" | awk '{print $2}')")"
[ ! -f ".authors/$author_name" ] && echo "Author \"$author_name\" doesn't exist." && exit 1
author=$(cat ".authors/$author_name")
SHORT_URL_ID=0
while grep ^$SHORT_URL_ID zm/short.csv >/dev/null ; do
SHORT_URL_ID=$(($SHORT_URL_ID + 1))
done
SHORT_URL="$shortener$SHORT_URL_ID"
LONG_URL="$website"zerm/"$basefile"
echo "$SHORT_URL_ID,$LONG_URL" >> zm/short.csv
echo "<html><head><meta http-equiv='refresh' content='0; URL=$LONG_URL'><script>window.location.href = '$LONG_URL'</script></head><body>Sie sollten <a href='$LONG_URL'>hierhin</a> weitergeleitet werden.</body></html>" > zm/$SHORT_URL_ID.html
echo "<html>
<head>
<title>$realname</title>
<link rel='stylesheet' type='text/css' href='$css'>
</head>
<body>
<a href='../index.html'>zurück</a>
<h1>$realname</h1>
$(cat "zerm/.drafts/$basefile")
<a href='https://twitter.com/intent/tweet?text=$(echo "$realname" | sed 's/ /%20/g' | sed 's/?/%3F/g')%20%23ZERM&url=https%3A%2F%2F$shortener_domain%2F$SHORT_URL_ID&via=ZERMZeitung' target="_blank" class="twitterbutton"><i></i></a>
<br/><br/>
<footer>von <strong>$author</strong></footer>
</body>
</html>" > "zerm/$basefile"
echo "
<item>
<title>$realname</title>
<guid>$website$blogfile#$base</guid>
<pubDate>$rssdate</pubDate>
<description><![CDATA[
$(cat "zerm/.drafts/$basefile")
]]></description>
</item>
" > "$tmpdir/rss"
echo "<div class='entry'>
<h2 id='$base'>$realname</h2>
<small>[<a href='#$base'>link</a>—<a href='zerm/$basefile'>standalone</a>]</small>
<br/>
$(cat "zerm/.drafts/$basefile")
<small>$webdate</small>
</div>" > "$tmpdir/html"
echo "<li>$(date '+%d.%m.%Y') – <a href=\"zerm/$basefile\">$realname</a></li>" > "$tmpdir/index"
echo "<url><loc>$LONG_URL</loc><changefreq>monthly</changefreq></url>" > "$tmpdir/sitemap"
sed -i "/<!-- ZERM -->/r $tmpdir/html" "$blogfile"
sed -i "/<!-- ZERM -->/r $tmpdir/rss" "$rssfile"
sed -i "/<!-- ZERM -->/r $tmpdir/index" "$indexfile"
sed -i "/<!-- ZERM -->/r $tmpdir/sitemap" "$sitemapfile"
sed -i "/ \"$base.html\"/d" "zerm/.htaccess"
echo "AddDescription \"$realname\" \"$basefile\" #$rssdate" >> "zerm/.htaccess"
rm -f "zerm/.drafts/$chosen"
echo "@everyone Neuer ZERM-Artikel! $SHORT_URL" | xclip -in -selection clipboard
echo "Article published and Discord message copied to your clipboard!"
}
confirm() {
read -erp "Really $1 \"$base\"? (y/N) " choice
[ "$choice" = "y*" ] || [ "$choice" = "Y*" ] || exit 1
}
delete() {
sed -i "/<item/{:a;N;/<\\/item>/!ba};/#$base<\\/guid/d" "$rssfile"
sed -i "/<div class='entry'>/{:a;N;/<\\/div>/!ba};/id='$base'/d" "$blogfile"
sed -i "/<li>.*<a href=\"zerm\\/$base.html\">/d" "$indexfile"
rm "zerm/$basefile" >/dev/null && echo "Old article removed."
}
case "$1" in
n*) newpost n ;;
m*) newpost m ;;
e*) listandReturn zerm/.drafts/ edit && "$EDITOR" "zerm/.drafts/$chosen" ;;
p*) listandReturn zerm/.drafts/ publish && publish ;;
t*) listandReturn zerm/.drafts/ trash && confirm trash && rm -f "zerm/.drafts/$chosen" && sed -i "/ \"$base.html\"/d" "zerm/.htaccess" ; echo "Draft deleted." ;;
d*) listandReturn zerm/ delete && confirm delete && delete && sed -i "/ \"$base.html\"/d" "zerm/.htaccess" ;;
s*) listandReturn zerm/.drafts/ schedule && sleep $2 && echo $3 | publish && git add . && git commit -m "published scheduled article (automated)" && git push ;;
*) echo "zm by Chris Häußler <chrissx@chrissx.de>, based on lb by Luke Smith <luke@lukesmith.xyz>
Usage:
zm n: new draft in html
zm m: new draft in md
zm e: edit draft
zm p: publish/finalize draft
zm t: trash draft
zm d: delete published entry
zm s: schedule entry (only use if you know, what you're doing, beta stuff)
Be sure to have the following pattern added to your RSS feed, blog file and blog index:
<!-- ZERM -->
New content will be added directly below that sequence. This is required.
See https://github.com/chrissxYT/zm for more." ;;
esac