summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArmaan Bhojwani <me@armaanb.net>2021-11-27 23:59:36 -0500
committerArmaan Bhojwani <me@armaanb.net>2021-11-27 23:59:36 -0500
commit43a001ece8e3b2ab9f103f822afd14eeb2e3f42e (patch)
treec76f3931f139fc8549a6ee4f4d5cffc480d964d3
parentdfce1b5805e601af45e9bac9bdd9f5095ca92ba9 (diff)
downloadbettersearch-43a001ece8e3b2ab9f103f822afd14eeb2e3f42e.tar.gz
Implement pagination
-rw-r--r--TODO1
-rw-r--r--server.rkt27
-rw-r--r--templates/search.html10
3 files changed, 27 insertions, 11 deletions
diff --git a/TODO b/TODO
index 141a0f2..ad2c40a 100644
--- a/TODO
+++ b/TODO
@@ -1,2 +1 @@
-* Pagination
* Customizeable searx instance
diff --git a/server.rkt b/server.rkt
index 42da286..01177fe 100644
--- a/server.rkt
+++ b/server.rkt
@@ -30,23 +30,27 @@
(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")
diff --git a/templates/search.html b/templates/search.html
index 58bf146..b3cf72d 100644
--- a/templates/search.html
+++ b/templates/search.html
@@ -16,3 +16,13 @@
</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>