pandoc/pandoc/server.chicken.scm

30 lines
1.2 KiB
Scheme

(define (pandoc-server base-url)
(lambda (input-format bytevectors)
(with-input-from-request
(make-request
method: 'POST
uri: (uri-reference (string-append base-url "convert-batch"))
headers: (headers '((content-type "application/json")
(accept "application/json"))))
(lambda ()
(let ((input-format (symbol->string input-format)))
(write-json
(list->vector
(map (lambda (bytevector)
(list (cons 'from input-format)
(cons 'to "json")
(cons 'text (utf8->string bytevector))))
bytevectors)))))
(lambda ()
(let ((array (string->cjson (read-string))))
(unless (eq? cjson/array (cjson-type array))
(error "Got unexpected JSON from pandoc-server"))
(let loop ((i (- (cjson-array-size array) 1)) (results '()))
(if (< i 0) results
(loop (- i 1)
(cons (cjson-schemify
(string->cjson
(cjson-schemify
(cjson-array-ref array i))))
results)))))))))