]> git.armaanb.net Git - bettersearch.git/commitdiff
Implement pagination
authorArmaan Bhojwani <me@armaanb.net>
Sun, 28 Nov 2021 04:59:36 +0000 (23:59 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Sun, 28 Nov 2021 04:59:36 +0000 (23:59 -0500)
TODO
server.rkt
templates/search.html

diff --git a/TODO b/TODO
index 141a0f24001d4ceff322d45aa8bff2e964265503..ad2c40a3259d0e8608e0b386d7fe6432b12a49b0 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,2 +1 @@
-* Pagination
 * Customizeable searx instance
index 42da28635db7c462026ea9d7e36b0516f858f056..01177fec61401cd5311197ddc8dfa7848f1ad3fd 100644 (file)
                   (include-template "templates/index.html")
                   (do-footer))))
 
-(define (search query)
+(define (search query pageno)
   (define engine (string->url
                   (string-append
-                    "https://search.trom.tf/search?format=json&q=" query)))
+                      "https://search.trom.tf/search?format=json&q="
+                      query
+                      "&pageno="
+                      pageno
+                      )))
   (define response (get-pure-port engine))
   (define json-raw (port->string response))
   (close-input-port response)
   (with-input-from-string json-raw (lambda () (read-json))))
 
-(define (http-response content)  ; The 'content' parameter should be a string.
+(define (http-response content)
   (response/full
-    200                  ; HTTP response code.
-    #"OK"                ; HTTP response message.
-    (current-seconds)    ; Timestamp.
-    TEXT/HTML-MIME-TYPE  ; MIME type for content.
-    '()                  ; Additional HTTP heads.
-    (list                ; Content (in bytes) to send to the browser.
+    200
+    #"OK"
+    (current-seconds)
+    TEXT/HTML-MIME-TYPE
+    '()
+    (list
       (string->bytes/utf-8 content))))
 
 (define (do-search req)
@@ -57,6 +61,9 @@
 
   (if (non-empty-string? query)
     (let ()
+      (define pageno (if (exists-binding? 'pageno binds)
+                      (extract-binding/single 'pageno binds)
+                      "1"))
       (define results
        (foldr cons '()
               (filter hash?
@@ -76,7 +83,7 @@
                            )
                          ht
                      ))
-                   (hash-ref (search query) 'results)))))
+                   (hash-ref (search query pageno) 'results)))))
       (http-response (string-append
                       (do-head (string-append query " | Web Search"))
                       (include-template "templates/search.html")
index 58bf1467f1df1b30b91d1ada60b868b527ad406f..b3cf72d9045ae2e11de10e1994b9e650de1845fc 100644 (file)
        </div>
 }
 </dl>
+
+<p style="text-align: left;">
+       <a href="search?q=query&pageno=@(- (string->number pageno) 1)"
+               style="@(when (equal? pageno "1") "visibility: hidden")">
+               Previous
+       </a>
+       <span style="float: right;">
+<a href="/search?q=query&pageno=@(+ (string->number pageno) 1)">Next</a>
+       </span>
+</p>