summaryrefslogtreecommitdiffstats
path: root/prog/skovikanje/komentar.sh
blob: bc7f75bd966d49c6b2583629eedbcf1979b8c1ce (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
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
#!/bin/bash
set -xeuo pipefail
function hsc () {
	sed -e 's/</\&lt;/g' -e "s/'/\&apos;/g" -e 's/"/\&quot;/g'
}
function besedilo () { # <tip -- html> <datoteka> <zaupanje - 0 za html escaping, 1 brez html escapinga> <nesting level>
	vrstica=1
	seznam=1
	naredilodstavek=0
	echo "<div class=besedilo>"
	tail -n+2 $2 | while read -r l
	do
		if grep "^$" <<<"$l" > /dev/null
		then
			echo "<p>"
			naredilodstavek=1
			continue
		fi
		navadno=1
		if grep ^! <<<"$l" > /dev/null && grep -v \' <<<"$l" > /dev/null
		then
			if [ ! $vrstica -eq 1 ]
			then
				[ $naredilodstavek -eq 0 ] && echo "<br>"
			fi
			naredilodstavek=0
			echo "<img style=max-width:100% src='`sed -e s/!// <<<"$l"`' />"
			navadno=0
		fi
		seznam=$(($seznam-1))
		if grep '^*' <<<"$l" > /dev/null
		then
			[ $seznam -eq 0 ] && echo "<ul>"
			seznam=2
			navadno=0
		fi
		dodajnakonec=""
		dodajnazayetek=""
		if grep '^>[^>]' <<<"$l" > /dev/null
		then
			dodajnazayetek="<span style=color:green>"
			dodajnakonec="</span>"
		fi
		[ $seznam -eq 1 ] && echo "</ul>"
		if [ $navadno -eq 1 ]
		then
			if [ $3 -eq 0 ]
			then
				tekst=`hsc <<<"$l"`
			else
				tekst=$l
			fi
			if [ $seznam -eq 2 ]
			then
				tekst=`sed 's/^*/<li>/' <<<"$tekst"`
			fi
			if [ $4 -gt 0 ]
			then
				for you in `grep -Eo ">>[A-Za-z0-9+-]{4}" <<<"$l"`
				do
					stil="border:1px solid green"
					if [ $(($4%2)) -eq 0 ]
					then
						stil="border:1px solid red"
					fi
					koment=`komentar $1 $(cut -d\> -f3 <<<$you) $(($4-1))`
					echo "<details class=komentar style='$stil'><summary>`head -n1 <<<$koment`</summary>`tail -n+2 <<<$koment`</details>"
					naredilodstavek=1
				done
			fi
			if [ ! $vrstica -eq 1 ]
			then
				[ $naredilodstavek -eq 0 ] && echo "<br>"
			fi
			naredilodstavek=0
			echo $dodajnazayetek
			urlregex="[A-Za-z0-9._~%:/?#@!$&()*+,;=-]"
			sed -Ee "s,http([s]*)://([w.]*)($urlregex*),<a class=raw href='http\1://\2\3'>\3</a>,g" <<<"$tekst"
			echo "$dodajnakonec"
		fi
		vrstica=$(($vrstica+1))
	done
	echo "</div>"
}
function komentar () { # tip id nesting-level
	stat komentarji/$2 > /dev/null || { echo komentar $2 ne obstaja.; return; }
	echo -n "<i><a href=# onclick=citiraj('$2')><code>$2</code></a> <b class=avtor>$(head -n1 komentarji/$2 | cut -f2 | cut -d@ -f1 | hsc)</b> "
	echo "dne <span class=datum>`date --date @$(stat --format %W komentarji/$2)`</span> <a href=//z.4a.si/skovikanje/komentar.php?koga=$2 target=_blank>uredi</a> <a href=/skovikanje/skovik/$(head -n1 komentarji/$2 | cut -f1).html>na</a></i>"
	besedilo $1 komentarji/$2 0 $3
}
if [ $1 = komentar ]
then
	komentar $2 $3 5
fi
if [ $1 = objava ]
then
	metadesc=`tail -n+2 objave/$3 | head -n3 | hsc`
	if [ `wc -l < objave/$3` -gt 4 ]
	then
		metadesc="$metadesc ..."
	fi
	naslov=`head -n1 objave/$3 | cut -f1 | hsc`
	spisal=`head -n1 objave/$3 | cut -f2`
	if [ $2 = rss ]
	then
		echo "<guid isPermaLink='false'>`hsc <<<$3`</guid><title>$naslov</title><description>$metadesc</description><pubDate>`date --rfc-email --date $spisal`</pubDate><link>$RSS_BASEURL`hsc <<<$3`.html</link>"
		exit
	fi
	echo "<meta name=referrer content=unsafe-url />"
	if [ $4 -eq 1 ]
	then
		echo "<title>$naslov :: skovikanje</title>"
		echo "<meta property=og:title content='$naslov :: skovik' />"
		echo "<meta property=og:description content='$metadesc' />"
		if grep "^!" objave/$3 > /dev/null
		then
			echo "<meta property=og:image content='`grep "^!" objave/$3 | head -n1 | sed 's/!//'`' />"
		fi
		echo "<h2><a href=..><<< nazaj</a></h2>"

	fi
	echo "<script>function citiraj(slug) {odgovordetails.open=true;odgovor.hidden=false;besedilo.value+='>>'+slug+'\n';}</script>"
	echo "<h1 class=naslov>$naslov</h1>"
	echo "<p><b><i><u>spisal <span class=spisal>$spisal</span>"
	if [ ! `head -n1 objave/$3 | cut -f2` = `date --iso-8601 --date @$(stat --format %Y objave/$3)` ]
	then
		echo "| nazadnje spremenil <span class=spremenil>`date --iso-8601 --date @$(stat --format %Y objave/$3)`</span>"
	fi
	echo "</u></i></b></p>"
	if [ $4 -eq 1 ]
	then
		besedilo $2 objave/$3 1 5
		echo "<hr><h2>komentarji</h2>"
		komentiraj="<input id=komentiraj1 hidden type=button onclick='document.getElementById(&quot;odgovor&quot;).hidden=false' value=komentiraj />"
		echo $komentiraj
		for file in `ls --reverse --sort=time --time=birth komentarji`
		do
			
			grep "^$3	" komentarji/$file > /dev/null && { echo "<div class=komentar style='border:1px solid red'>"; komentar $1 $file 5; echo "</div>"; }
		done
		echo "<div id=odgovor><details open id=odgovordetails><summary>pokaži/skrij</summary>`curl --fail-with-body http://z.4a.si/skovikanje/komentar.php?action=//z.4a.si/skovikanje/komentar.php%3Fna=$3%26tip=objava%26ref=REPLACEME | sed s/REPLACEME/HTTP_REFERER/`</details></div>"
		sed s/komentiraj1/komentiraj2/ <<<$komentiraj
		echo "<script>odgovor.style='background:gray;position:fixed;right:0;bottom:0';odgovor.hidden=true;komentiraj1.hidden=false;komentiraj2.hidden=false</script>"
		echo "<br><br><br><br>"
	else
		besedilo $2 <(head -n4 objave/$3) 1 5
		komentarjev=0
		for file in `ls --reverse --sort=time --time=birth komentarji`
		do
			grep "^$3	" komentarji/$file > /dev/null && komentarjev=$(($komentarjev+1))
		done
		vey=""
		tripiyje=""
		if [ `wc -l < objave/$3` -gt 4 ]
		then
			vey=" ogled celotnega besedila,"
			tripiyje="..."
		fi
		echo "$tripiyje<h4><a href=skovik/$3.html>Klikni sem za$vey komentiranje in branje komentarjev ($komentarjev).</a></h4>"
	fi
fi