-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteige.ksh
397 lines (331 loc) · 9.23 KB
/
teige.ksh
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/bin/ksh
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY.
USERVAR="$HOME/.teige.conf" #path to default config file. If it doesn't exist
#running teige for the first time will create it.
DATE=$(date "+%Y-%m-%d") #date shows up underneath a picture on its individual site
#choose any format you'd like. Default is YYYY-MM-DD
DATE2="$(date -u "+%a, %d %b %Y %H:%M:%S +0000")" #date for RSS feeds in UTC
LANG="en-US" #Used in html header. Change to whatever language you want post in.
#FUNCTIONS
#ID generator
#Each image has a unique ID that is generated here.
#B default, teige uses only $(date +%s) for its generation of post IDs.
#This is a personal choice and a silly habit, where using Unix time is an easy
#method of sorting posts. Since teige does currently sort posts by IDs, see
#FORCE REBUILD section, if you choose to change the ID generation method.
#Every post gets a unique ID.
rando() {
print $(date +%s)
}
#my particularly vintage method
#rando() {
#REM=$(print -n $(date +%s))
#IDTR=$(print -n "$(head -n15 /dev/urandom | tr -dc "123456abcdef" | tail -c2)")
#ID=$(print -n $REM$IDTR)
#print $ID
#}
FIRSTRUN() {
#generate configuration file
touch $USERVAR || print "Can't create configuration file!"
cat <<.> $USERVAR
#Path to the root of web. YOU MOST PROBABLY WANT TO CHANGE THIS
#leave it to test teige offline
WROOT=/home/$USER/site
#Web directory of teige in \$WROOT. By default, teige places itself
#in \$WROOT/teige. user running teige must have write and read
#access to this directory.
WEB=teige
#Local directory where teige stores data for
#rebuilding the site.
#note that changing this after you've began using teige
#will cause issues unless you move its contents as well
LOCAL=/home/$USER/.teige
#After posting an image, teige will print its URL in the format:
#\$WEBSITE/\$ID.
WEBSITE="http://website.cz"
#path to a profile picture. either a full path from \$WROOT or "URL".
PROFILOVKA=
#css style sheet to use for html generation. either a full path from \$WROOT or URL.
CSS=/gal.css
NAME="My Name" #h1 in \$WEB/index.html
MOTTO="A heading" #h2 in \$WEB/index.html
BIO="This is a bio" #single paragraph on \$WEB/index.html
EDITOR="vi" #editor to use for editing posts
#PROFILOVKA=/img/profile.png #Profile picture is in website.cz/img/profile.png
#CSS=/gal.css #style sheet is in website.cz/gal.css
#WEB=teige #teige gallery is in website.cz/teige
.
print "You can find your configuration in $USERVAR"
print "That's where you can change everything."
print "Edit the file, then run teige without any arguments,"
print "it will generate the gallery in \$WROOT/\$WEB"
}
HELP() {
cat <<.
$USAGE
Running teige without any flags or arguments refreshes index.html.
-h Display this help text.
-v Displays version information.
-c file Load different configuration file.
-b Blur preview of a posted image.
-e ID Edit by ID.
-F Force rebuild of entire gallery.
-r ID Remove image by ID. This removes the post
from both the \$WROOT/\$WEB and \$LOCAL
-U Teige will expect a URL instead of a 'file'
.
}
USAGE="usage: teige [-h] [-v] [-bFU] [-c config] [-e ID] [-r ID] file|url"
VERSION() {
print "teige is a simple pure html image gallery manager."
print "teige 1.0 - published under GPL-v3"
print "Written by Tomáš Rodr in 2022."
print "https://triapul.cz/teige"
}
while getopts :hvDbc:e:Fp:r:U FLAG; do
case $FLAG in
h) HELP;exit;;
v) VERSION;exit;;
D) DEBUG=1;;
b) BLUR=1;;
c) FCONF=1;CONF=$OPTARG;;
e) FEDIT=1;ID=$OPTARG;;
F) FBUILD=1;;
p) FPRE=1;PREVFILE=$OPTARG;;
r) FREMOVE=1;ID=$OPTARG;;
U) FURL=1;;
\?) print "Invalid flag";print $USAGE;exit;;
:) print "Invalid flag.";
print "$USAGE";;
esac
done
if (( FCONF == 1 )); then
USERVAR="./$CONF"
fi
if [[ ! (-a $USERVAR) ]]; then
FIRSTRUN
exit
fi
#LOAD CONFIG
. $USERVAR
#Paranoid checks
if [[ ! (-d $WROOT/$WEB) ]]; then
mkdir -p $WROOT/$WEB || print "Path to website $WROOT/$WEB doesn't exist and I can't create it..";
touch $WROOT/$WEB/index.html || exit
fi
if [[ ! (-d $LOCAL) ]]; then
mkdir -p $LOCAL || print "Directory for local data doesn't exist and I can't create it."
touch $LOCAL/feed.txt || exit
fi
#teige needs a local offline directory..
if [[ ! (-d $LOCAL) ]]; then mkdir $LOCAL || print "teige needs an accessible local directory!";
touch $LOCAL/feed.txt || exit
fi
#BEGIN
build() {
if (( SWITCH == 0 )); then
MOTTO=$DESC
fi
# <link rel="alternate" type="application/rss+xml" href="$RSS"/>
# for future versions and integration with rss...
# keep in mind you can edit the build() function however you want to reflect your
# desired html code. Do not uncomment these!
# <link rel="icon" type="image/x-icon" href="$FAVICON"/>
# <meta name="keywords" content="$TAGS"/>
cat <<.
<!doctype html>
<html lang="$LANG">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta charset="utf-8"/>
<meta name="description" content="$MOTTO"/>
<link rel="stylesheet" type="text/css" href="$CSS?v=$RANDOM"/>
<title>$TITLE</title>
</head>
<body>
.
if [[ $SWITCH != 0 ]]; then
# uncomment if you want a link somewhere at the top of teige's index.html.
# for example
:
#cat <<.
#<a href="../">go up</a>
#.
else
#change 'back' to whatever you want your back link to say. This returns you from a post
#to teige's root.
cat <<.
<a href="../#$ID" class="zpet-link">back</a>
.
fi
#$NAME from user config gets put here on /index.html
cat <<.
<h1>$TITLE</h1>
.
if [[ $SWITCH != 0 ]]; then
cat <<.
<img class="profilovka" src="$PROFILOVKA" alt="profile picture">
<h2>$MOTTO</h2>
.
fi
if [[ $SWITCH = 0 ]]; then
print "<p>$DESC</p>"
else
print "<p>$BIO</p>"
fi
if [[ $SWITCH = "0" ]]; then
cat <<.
<img src="$IMG" alt="$IMGALT" class="full-img">
.
elif [[ $SWITCH = "1" ]]; then
print "<a class=\"gal-link\" href=\"$ID\"><img src=\"$ID/$PREVIEW\" alt=\"$IMGALT\" class=\"preview\" id=\"$ID\"></a>" >> $LOCAL/feed.txt
sort -r $LOCAL/feed.txt
elif [[ $SWITCH = "3" ]]; then
sort -r $LOCAL/feed.txt
fi
if [[ $SWITCH = "0" ]]; then
#The ID beneath $DATE serves as a permanent link to your post. It will probably not work if you're using teige locally offline.
cat <<.
<p class="datum">$DATE</p>
<a href="/$WEB/$ID" class="id">$ID</a>
.
fi
cat <<.
</body>
</html>
.
}
NEWPOST=0
#SWITCH=3 builds /index and changes nothing else
#SWITCH=0 builds a post
#SWITCH=1 updates /index and adds entry to $LOCAL/feed.txt
#REMOVE A POST
if (( FREMOVE == 1 )); then
if [[ ! ( -d $WROOT/$WEB/$ID) ]]; then print "bad ID";exit; else
SWITCH=3
read x?"Delete this? $WROOT/$WEB/$ID (y?)"
if [[ $x = [yY] ]]; then
rm $WROOT/$WEB/$ID/*
rm -r $WROOT/$WEB/$ID
sed -i /$ID/d $LOCAL/feed.txt
if [[ ! ( -a $LOCAL/$ID.var) ]]; then print "Local file $ID doesn't exist.";exit; else
rm $LOCAL/$ID.var
print "Post is removed."
. $USERVAR
build > $WROOT/$WEB/index.html
fi
fi
exit
fi
fi
#FORCE REBUILD OF ENTIRE TEIGE GALLERY
if (( FBUILD == 1 )); then
:> $LOCAL/feed.txt
#this is where you might run into unforeseen consequences if you use a custom ID generator.
#Here teige will by default use sort -br on your posts to regenerate its index.html
#change accordingly, if you use a custom ID gen!
for i in $(ls $LOCAL | sort -br); do
if [[ $i = "feed.txt" ]]; then :; else
SWITCH=0
. $LOCAL/$i
print "rebuilding $ID $TITLE"
build > $WROOT/$WEB/$ID/index.html
print "<a class=\"gal-link\" href=\"$ID\"><img src=\"$ID/$PREVIEW\" alt=\"$IMGALT\" class=\"preview\" id=\"$ID\"></a>" >> $LOCAL/feed.txt
fi
done
SWITCH=3
. $USERVAR
TITLE="$NAME"
build > $WROOT/$WEB/index.html
exit
fi
#EDIT A POST
if (( FEDIT == 1 )); then
$EDITOR $LOCAL/$ID.var
SWITCH=0
. $LOCAL/$ID.var
build > $WROOT/$WEB/$ID/index.html
exit
fi
SWITCH=0
(( START = OPTIND - 1 ))
shift $START
if (( DEBUG == 1 )); then
print "$START"
print "OPTIND: $OPTIND $*"
print "flag 1: $1"
read
fi
if [[ $* = "" ]]; then
SWITCH=3
. $USERVAR
TITLE="$NAME"
build > $WROOT/$WEB/index.html
exit
fi
PROCK="$*"
ID=$(rando)
if (( FURL == 1 )); then
ftp -o .temp$ID "$PROCK"
PROCK=.temp$ID
fi
if [[ ! (-a $PROCK ) ]]; then print "Image $PROCK does not exist."; exit; fi
identify $PROCK 1>/dev/null || print "Not an image."
NEWPOST=1
read TITLE?"Title of your post? "
read IMGALT?"Desctiption of image? (img alt=) "
read DESC?"Text on your post? "
mkdir $WROOT/$WEB/$ID
#IMG IS A GIF
if [[ $PROCK = *[Gg][iI][fF] ]]; then
convert -resize 150x150 $PROCK $WROOT/$WEB/$ID/$ID-preview.gif #GIFS are currently... not much cared for.
cp $PROCK $WROOT/$WEB/$ID/$ID.gif
IMG="$ID.gif"
PREVIEW="$ID-preview.gif"
else
#IMG IS NOT A GIF
if (( BLUR == 1 )); then
#BLUR
convert $PROCK -resize 150x150^ \
-blur 0x3 \
-gravity Center \
-crop 150x150+0+0 +repage \
$WROOT/$WEB/$ID/$ID-preview.png #OUTPUT
cp $PROCK $WROOT/$WEB/$ID/$ID.png
IMG="$ID.png"
PREVIEW="$ID-preview.png"
else
#NOBLUR
convert $PROCK -resize 150x150^ \
-gravity Center \
-crop 150x150+0+0 +repage \
$WROOT/$WEB/$ID/$ID-preview.png #OUTPUT
cp $PROCK $WROOT/$WEB/$ID/$ID.png
IMG="$ID.png"
PREVIEW="$ID-preview.png"
fi
fi
build > $WROOT/$WEB/$ID/index.html
#store vars locally for rebuilding the gallery
if (( NEWPOST == 1 ));then
cat <<.> $LOCAL/$ID.var
ID="$ID"
IMG="$IMG"
PREVIEW="$PREVIEW"
IMGALT="$IMGALT"
TITLE="$TITLE"
DESC="$DESC"
DATE="$DATE"
DATE2="$DATE2"
.
NEWPOST=0
fi
SWITCH=1
. $USERVAR
TITLE="$NAME"
build > $WROOT/$WEB/index.html
if (( FURL == 1 )); then
rm $PROCK
fi
print $WEBSITE/$WEB/$ID