commit 4017a05

pita  ·  2026-06-22 12:37:47 +0000 UTC
parent 4017a05
init
1 files changed,  +69, -0
+69, -0
 1@@ -0,0 +1,69 @@
 2+fn webring_init {
 3+    switch ($req_path) {
 4+    case /prev/*
 5+        target=`{echo $req_path | sed 's!^/prev/!!'}
 6+        webring_navigate -1 $target
 7+        exit
 8+    case /next/*
 9+        target=`{echo $req_path | sed 's!^/next/!!'}
10+        webring_navigate 1 $target
11+        exit
12+    case /random
13+        webring_random
14+        exit
15+    }
16+}
17+
18+fn webring_navigate {
19+    step = $1
20+    pat = $2
21+    
22+    list_file = $sitedir/list.txt
23+
24+    num=`{awk -v pat=$pat '$0 ~ pat {print NR; exit}' $list_file}
25+
26+    if (~ $#num 0) {
27+        echo 'Pattern not found in the webring list.'
28+        exit
29+    }
30+
31+    lines=`{wc -l < $list_file}
32+
33+    thing=`{awk -v n=$num -v total=$lines -v step=$step '
34+    BEGIN {
35+        n = n + step
36+        if (n < 1)
37+            n = total
38+        else if (n > total)
39+            n = 1
40+    }
41+    NR == n {
42+        print
43+        exit
44+    }' $list_file}
45+
46+    http_redirect $thing '302 Found'
47+}
48+
49+fn webring_random {
50+    list_file = $sitedir/list.txt
51+
52+    lines=`{wc -l < $list_file}
53+
54+    thing=`{awk -v total=$lines '
55+    BEGIN {
56+        srand()
57+        target = int(rand() * total) + 1
58+    }
59+    NR == target {
60+        print
61+        exit
62+    }' $list_file}
63+
64+    if (~ $#thing 0) {
65+        echo 'Failed to pick a random site.'
66+        exit
67+    }
68+
69+    http_redirect $thing '302 Found'
70+}