2003-01-19 11:57:27 -05:00
|
|
|
;; utilities for surflet
|
2003-03-03 05:27:49 -05:00
|
|
|
;; Copyright 2002,2003, Andreas Bernauer
|
|
|
|
;; Copyright 2003, Martin Gasbichler
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
(define (send-html/suspend html-tree-maker)
|
|
|
|
(send/suspend
|
|
|
|
(lambda (new-url)
|
2003-01-14 06:27:42 -05:00
|
|
|
(make-usual-html-response
|
2003-01-24 10:23:51 -05:00
|
|
|
(surflet-XML->HTML #f (html-tree-maker new-url))))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
(define (send-html/finish html-tree)
|
|
|
|
(do-sending send/finish html-tree))
|
|
|
|
|
|
|
|
(define (send-html html-tree)
|
|
|
|
(do-sending send html-tree))
|
|
|
|
|
|
|
|
(define (do-sending send html-tree)
|
2003-01-24 10:23:51 -05:00
|
|
|
(send (make-usual-html-response
|
|
|
|
(surflet-XML->HTML #f html-tree))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2003-01-24 10:23:51 -05:00
|
|
|
(define (make-usual-html-response html-string)
|
|
|
|
(make-surflet-response
|
2003-01-09 10:05:30 -05:00
|
|
|
(status-code ok)
|
2002-09-25 09:02:31 -04:00
|
|
|
"text/html"
|
2003-01-17 12:39:07 -05:00
|
|
|
'(("Cache-Control" . "no-cache"))
|
2003-01-24 10:23:51 -05:00
|
|
|
html-string))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; from cgi-script:
|
|
|
|
;;; Return the form data as an alist of decoded strings.
|
|
|
|
;;; So a query string like "button=on&reply=Oh,%20yes" becomes alist
|
|
|
|
;;; (("button" . "on") ("reply" . "Oh, yes"))
|
2002-10-26 11:20:56 -04:00
|
|
|
;;; This works only for GET and POST methods.
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2003-02-26 10:55:22 -05:00
|
|
|
(define (form-query q)
|
|
|
|
(if q
|
|
|
|
(parse-html-form-query q)
|
|
|
|
'()))
|
2002-10-26 11:20:56 -04:00
|
|
|
|
|
|
|
;; Bindings of POST requests can be read only once, since they are
|
|
|
|
;; read from an input port. So we have to cache them, for the case of
|
2003-01-25 11:33:50 -05:00
|
|
|
;; a later GET-BINDINGS call on the same POST request. The requests
|
|
|
|
;; are referenced by a weak pointer. Thread-safe as all threads use
|
|
|
|
;; the same lock.
|
2002-10-26 11:20:56 -04:00
|
|
|
(define *POST-bindings-cache* '())
|
|
|
|
(define *cache-lock* (make-lock))
|
|
|
|
|
2003-02-17 05:09:24 -05:00
|
|
|
(define (get-bindings surflet-request)
|
|
|
|
(let ((request-method (surflet-request-method surflet-request)))
|
2002-10-02 20:18:10 -04:00
|
|
|
(cond
|
|
|
|
((string=? request-method "GET")
|
2003-02-17 05:09:24 -05:00
|
|
|
(form-query (http-url-search (surflet-request-url surflet-request))))
|
2002-10-02 20:45:41 -04:00
|
|
|
((string=? request-method "POST")
|
2003-02-17 05:09:24 -05:00
|
|
|
(or (cached-bindings surflet-request)
|
|
|
|
(let* ((content-length (get-content-length (surflet-request-headers surflet-request)))
|
|
|
|
(input-port (surflet-request-input-port surflet-request))
|
2002-10-26 11:20:56 -04:00
|
|
|
(form-data (read-string content-length input-port)))
|
|
|
|
(let ((form-bindings (form-query form-data)))
|
|
|
|
(obtain-lock *cache-lock*)
|
2003-02-17 05:09:24 -05:00
|
|
|
(set! *POST-bindings-cache* (cons (cons (make-weak-pointer surflet-request)
|
2002-10-26 11:20:56 -04:00
|
|
|
form-bindings)
|
|
|
|
*POST-bindings-cache*))
|
|
|
|
(release-lock *cache-lock*)
|
|
|
|
form-bindings))))
|
2002-10-02 20:18:10 -04:00
|
|
|
(else
|
|
|
|
(error "unsupported request type")))))
|
|
|
|
|
2002-10-26 11:20:56 -04:00
|
|
|
;; Looking up, if we have cached this request. While going through the
|
|
|
|
;; list, we remove entries to request objects, that are no longer
|
|
|
|
;; valid. Expecting a call for an uncached request every now and then,
|
2003-02-17 05:09:24 -05:00
|
|
|
;; it is guaranteed, that the list is cleaned up every now and
|
|
|
|
;; then. The cache is a list of pairs
|
|
|
|
;;; (surflet-request . computed-binding)
|
|
|
|
(define (cached-bindings surflet-request)
|
2002-10-26 11:20:56 -04:00
|
|
|
(obtain-lock *cache-lock*)
|
|
|
|
(let ((result
|
|
|
|
(let loop ((cache *POST-bindings-cache*))
|
|
|
|
(if (null? cache)
|
|
|
|
#f ; no such request cached
|
|
|
|
(let* ((head (car cache))
|
2003-02-17 05:09:24 -05:00
|
|
|
(s-req (weak-pointer-ref (car head))))
|
|
|
|
(if s-req
|
|
|
|
(if (eq? s-req surflet-request)
|
2002-10-26 11:20:56 -04:00
|
|
|
(cdar cache) ; request is cached
|
|
|
|
(loop (cdr cache))) ; request isn't cached
|
|
|
|
(begin
|
|
|
|
;; request object is gone ==> remove it from list
|
|
|
|
(set! cache (cdr cache))
|
|
|
|
(loop cache))))))))
|
|
|
|
(release-lock *cache-lock*)
|
|
|
|
result))
|
2003-01-22 07:53:46 -05:00
|
|
|
|
2002-10-26 11:20:56 -04:00
|
|
|
|
2002-10-02 20:18:10 -04:00
|
|
|
;; Will be needed when we handle POST requests.
|
2002-10-02 20:45:41 -04:00
|
|
|
(define (get-content-length headers)
|
|
|
|
(cond ((get-header headers 'content-length) =>
|
|
|
|
;; adopted from httpd/cgi-server.scm
|
|
|
|
(lambda (content-length) ; Skip initial whitespace (& other non-digits).
|
|
|
|
(let ((first-digit (string-index content-length char-set:digit))
|
|
|
|
(content-length-len (string-length content-length)))
|
|
|
|
(if first-digit
|
|
|
|
(string->number (substring content-length first-digit
|
|
|
|
content-length-len))
|
2003-01-09 10:05:30 -05:00
|
|
|
;; (status-code bad-request) req
|
2003-01-25 07:19:54 -05:00
|
|
|
(error "Illegal `Content-length:' header.")))))
|
2002-10-02 20:45:41 -04:00
|
|
|
(else
|
|
|
|
(error "No Content-length specified for POST data."))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2002-10-26 11:40:26 -04:00
|
|
|
(define (extract-bindings key bindings)
|
2002-09-25 09:02:31 -04:00
|
|
|
(let ((key (if (symbol? key) (symbol->string key) key)))
|
2002-10-03 08:02:47 -04:00
|
|
|
(map cdr
|
2002-10-03 07:58:27 -04:00
|
|
|
(filter (lambda (binding)
|
|
|
|
(equal? (car binding) key))
|
|
|
|
bindings))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2002-10-26 11:40:26 -04:00
|
|
|
(define (extract-single-binding key bindings)
|
|
|
|
(let ((key-bindings (extract-bindings key bindings)))
|
2002-09-25 09:02:31 -04:00
|
|
|
(if (= 1 (length key-bindings))
|
2002-10-03 07:58:27 -04:00
|
|
|
(car key-bindings)
|
2002-09-25 09:02:31 -04:00
|
|
|
(error "extract-one-binding: more than one or zero bindings found"
|
|
|
|
(length key-bindings)
|
|
|
|
key bindings))))
|
|
|
|
|
|
|
|
|
|
|
|
;; adapted from Oleg's SXML-tree-trans.scm
|
|
|
|
;; extended by port argument
|
|
|
|
;; #t: current-output-port
|
|
|
|
;; #f: string
|
|
|
|
;; port: port
|
|
|
|
;; else: error
|
|
|
|
(define (formated-reply port . fragments)
|
|
|
|
(cond
|
|
|
|
((not port)
|
|
|
|
(call-with-string-output-port
|
|
|
|
(lambda (port)
|
|
|
|
(real-formated-reply port fragments))))
|
|
|
|
((eq? port #t)
|
|
|
|
(real-formated-reply (current-output-port) fragments))
|
|
|
|
((output-port? port)
|
|
|
|
(real-formated-reply port fragments))
|
|
|
|
(else
|
|
|
|
(error "invalid port argument to FORMATED-REPLY" port))))
|
|
|
|
|
|
|
|
(define (real-formated-reply port fragments)
|
|
|
|
(let loop ((fragments fragments) (result #f))
|
|
|
|
(cond
|
|
|
|
((null? fragments) result)
|
|
|
|
((not (car fragments)) (loop (cdr fragments) result))
|
|
|
|
((null? (car fragments)) (loop (cdr fragments) result))
|
|
|
|
((pair? (car fragments))
|
|
|
|
(loop (cdr fragments) (loop (car fragments) result)))
|
|
|
|
((procedure? (car fragments))
|
|
|
|
((car fragments))
|
|
|
|
(loop (cdr fragments) #t))
|
|
|
|
(else
|
|
|
|
(display (car fragments) port)
|
|
|
|
(loop (cdr fragments) #t)))))
|
|
|
|
|
|
|
|
;; adapted from Oleg's SXML-to-HTML.scm
|
|
|
|
;; extended by additional port argument
|
2003-01-19 11:57:27 -05:00
|
|
|
(define (surflet-XML->HTML out html-tree)
|
2002-09-27 11:24:44 -04:00
|
|
|
(formated-reply out
|
|
|
|
(reformat html-tree)))
|
|
|
|
|
|
|
|
(define (reformat html-tree)
|
|
|
|
(pre-post-order
|
|
|
|
html-tree
|
|
|
|
`(
|
|
|
|
;; Universal transformation rules. Works for every HTML,
|
|
|
|
;; present and future
|
|
|
|
,@default-rules
|
|
|
|
(input-field
|
|
|
|
*preorder*
|
|
|
|
. ,(lambda (trigger input-field)
|
|
|
|
(reformat (input-field-HTML-tree input-field))))
|
|
|
|
|
2003-01-19 11:57:27 -05:00
|
|
|
(surflet-form
|
2002-09-27 11:24:44 -04:00
|
|
|
;; Must do something to prevent the callback-function string to
|
|
|
|
;; be HTML escaped.
|
|
|
|
*preorder*
|
2002-10-04 10:31:28 -04:00
|
|
|
. ,(lambda (trigger call-back-function . args)
|
|
|
|
(receive (parameters elems)
|
|
|
|
(typed-optionals (list symbol? XML-attribute?) args)
|
2003-01-19 11:57:27 -05:00
|
|
|
(make-surflet-form call-back-function
|
2002-10-04 10:31:28 -04:00
|
|
|
(car parameters)
|
|
|
|
(cadr parameters)
|
|
|
|
elems)))))
|
2002-09-27 11:24:44 -04:00
|
|
|
))
|
|
|
|
|
2003-01-19 11:57:27 -05:00
|
|
|
(define (make-surflet-form call-back-function method attributes elems)
|
2002-10-04 10:31:28 -04:00
|
|
|
(let ((real-method (case method
|
|
|
|
((get GET) "GET")
|
|
|
|
((post POST) "POST")
|
|
|
|
((#f) "GET")
|
|
|
|
(else
|
|
|
|
(error "invalid method type" method)))))
|
|
|
|
`("<form" ,@(map (lambda (attribute-value)
|
|
|
|
((enattr (car attribute-value)) (cadr attribute-value)))
|
|
|
|
`((method ,real-method)
|
|
|
|
(action ,call-back-function)
|
|
|
|
;; We have to divide attributes explicitly.
|
|
|
|
,@(if attributes (cdr attributes) '())))
|
|
|
|
#\> #\newline
|
2002-09-27 11:24:44 -04:00
|
|
|
,(reformat elems)
|
2002-10-04 10:31:28 -04:00
|
|
|
"</form>")))
|
2002-09-27 11:24:44 -04:00
|
|
|
|
|
|
|
(define (XML-attribute? thing)
|
|
|
|
(and (pair? thing)
|
|
|
|
(eq? '@ (car thing))))
|
|
|
|
|
|
|
|
(define attribute-rule
|
|
|
|
`(@ ; local override for attributes
|
|
|
|
((*default*
|
|
|
|
. ,(lambda (attr-key . value) ((enattr attr-key) value))))
|
|
|
|
. ,(lambda (trigger . value) (list '@ value))))
|
|
|
|
|
|
|
|
(define text-rule
|
|
|
|
`(*text*
|
|
|
|
. ,(lambda (trigger str)
|
|
|
|
(if (string? str) (string->goodHTML str) str))))
|
|
|
|
|
|
|
|
(define URL-rule
|
|
|
|
(cons 'URL
|
2002-10-01 08:10:15 -04:00
|
|
|
(lambda (tag URI . maybe-text) (list "<a href=\"" URI "\">"
|
|
|
|
(if (pair? maybe-text)
|
|
|
|
maybe-text
|
|
|
|
URI)"</a>"))))
|
2002-09-27 11:24:44 -04:00
|
|
|
|
2002-10-02 16:28:39 -04:00
|
|
|
(define plain-html-rule
|
|
|
|
`(plain-html
|
|
|
|
*preorder*
|
|
|
|
. ,(lambda (tag . text) text)))
|
|
|
|
|
2002-09-25 09:02:31 -04:00
|
|
|
(define default-rules
|
2002-09-27 11:24:44 -04:00
|
|
|
`(,attribute-rule
|
|
|
|
(*default*
|
|
|
|
. ,(lambda (tag . elems) (apply (entag tag) elems)))
|
|
|
|
,text-rule
|
2002-10-02 16:28:39 -04:00
|
|
|
,URL-rule
|
|
|
|
,plain-html-rule))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
(define (make-callback function)
|
|
|
|
(call-with-current-continuation
|
|
|
|
(lambda (exit)
|
|
|
|
(let* ((req (send/suspend (lambda (new-url)
|
2002-09-29 12:42:53 -04:00
|
|
|
(exit new-url)))))
|
|
|
|
(function req)))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2002-09-27 11:24:44 -04:00
|
|
|
|
2002-10-01 08:10:15 -04:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; outdater
|
|
|
|
|
|
|
|
(define-record-type outdater :outdater
|
|
|
|
(real-make-outdater outdated?)
|
|
|
|
outdater?
|
|
|
|
(outdated? outdater-outdated? set-outdater-outdated?!))
|
|
|
|
|
|
|
|
(define (make-outdater)
|
|
|
|
(real-make-outdater #f))
|
|
|
|
|
|
|
|
(define-syntax if-outdated
|
|
|
|
(syntax-rules ()
|
|
|
|
((if-outdated outdater consequence alternative)
|
|
|
|
(if (outdater-outdated? outdater)
|
|
|
|
consequence
|
|
|
|
(begin
|
|
|
|
(set-outdater-outdated?! outdater #t)
|
|
|
|
alternative)))))
|
|
|
|
|
|
|
|
(define (show-outdated url)
|
|
|
|
(send-html
|
|
|
|
`(html (title "Outdated Data")
|
|
|
|
(body (h1 "Outdated Data")
|
2003-02-17 05:09:24 -05:00
|
|
|
(p "The page or action you requested relies on outdated data.")
|
2002-10-03 09:28:31 -04:00
|
|
|
,(if url
|
|
|
|
`(p "Try to "
|
|
|
|
(URL ,url "reload")
|
|
|
|
" the page to get current data.")
|
|
|
|
'())))))
|
2002-09-27 11:24:44 -04:00
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;;; input-fields
|
2003-01-19 11:57:27 -05:00
|
|
|
;;; defines input-fields for surflets
|
2002-09-27 11:24:44 -04:00
|
|
|
|
2003-02-19 13:42:45 -05:00
|
|
|
;; get-bindings: Transformer will get all bindings of request, not
|
|
|
|
;; only the one concerning the input-field.
|
2002-09-25 09:02:31 -04:00
|
|
|
(define-record-type input-field :input-field
|
2002-09-27 13:29:31 -04:00
|
|
|
(real-make-input-field name transformer HTML-tree get-bindings?)
|
2003-02-19 13:42:45 -05:00
|
|
|
real-input-field?
|
2002-09-25 09:02:31 -04:00
|
|
|
(name input-field-name)
|
|
|
|
(transformer input-field-transformer)
|
|
|
|
(attributes input-field-attributes)
|
2002-09-27 13:29:31 -04:00
|
|
|
(HTML-tree input-field-HTML-tree)
|
|
|
|
(get-bindings? input-field-get-bindings?))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
(define-record-discloser :input-field
|
|
|
|
(lambda (input-field)
|
2002-09-27 13:29:31 -04:00
|
|
|
(list 'input-field
|
|
|
|
(input-field-name input-field))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2003-02-19 13:42:45 -05:00
|
|
|
;; Have to do a trick to get around with SSAX: input-field is a list
|
|
|
|
;; whose first element is 'input-field and the last (next) one is a
|
|
|
|
;; real input-field.
|
|
|
|
(define (input-field? input-field)
|
|
|
|
(and (pair? input-field)
|
|
|
|
(eq? 'input-field (car input-field))
|
|
|
|
(real-input-field? (cadr input-field))))
|
|
|
|
|
2002-09-25 09:02:31 -04:00
|
|
|
;; FIXME: consider creating small names
|
2002-10-02 16:28:39 -04:00
|
|
|
(define generate-unique-name
|
2002-09-25 09:02:31 -04:00
|
|
|
(let ((id 0))
|
|
|
|
(lambda (type-string)
|
|
|
|
(set! id (+ 1 id))
|
|
|
|
(string-append type-string (number->string id)))))
|
2002-10-02 16:28:39 -04:00
|
|
|
(define generate-input-field-name generate-unique-name)
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
(define identity (lambda (a) a))
|
|
|
|
|
2003-02-19 13:42:45 -05:00
|
|
|
;; See note at input-field? for reasons for the list.
|
2002-09-27 11:24:44 -04:00
|
|
|
(define (make-input-field name transformer HTML-tree)
|
2002-09-27 13:29:31 -04:00
|
|
|
(list 'input-field (real-make-input-field name transformer HTML-tree #f)))
|
|
|
|
|
2002-10-09 11:25:14 -04:00
|
|
|
(define (make-higher-input-field transformer HTML-tree)
|
2002-09-27 13:29:31 -04:00
|
|
|
(list 'input-field (real-make-input-field #f transformer HTML-tree #t)))
|
2002-09-27 11:24:44 -04:00
|
|
|
|
2002-09-30 10:43:15 -04:00
|
|
|
;; PRED-LIST contains list of predicates that recognizes optional
|
|
|
|
;; leading parameters. FURTHER-ATTRIBUTES is the optional parameter
|
2002-10-02 14:22:01 -04:00
|
|
|
;; list as got by procedure call. TYPED-OPTIONALS returns two values:
|
2002-09-30 10:43:15 -04:00
|
|
|
;; a list of the same length as PRED-LIST and a list containing the
|
|
|
|
;; left arguments that did not fit the predicates.
|
|
|
|
;;
|
2002-10-02 14:22:01 -04:00
|
|
|
;; With the help of TYPED-OPTIONALS you can define a function
|
2002-09-30 10:43:15 -04:00
|
|
|
;; like `make-submit-button [string] [further-attributes]' this way:
|
|
|
|
;; (define (make-submit-button . args)
|
|
|
|
;; (receive (params rest-args)
|
|
|
|
;; (prefix-optionals (list string? XML-attribute?) args)
|
|
|
|
;; (if (pair? rest-args)
|
|
|
|
;; (error "too many arguments to make-submit-button))
|
|
|
|
;; (let ((value (first params))
|
|
|
|
;; (attributes (second params)))
|
|
|
|
;; ...))))
|
|
|
|
;;
|
|
|
|
(define (typed-optionals pred-list args)
|
|
|
|
(let loop ((results '())
|
|
|
|
(pred-list pred-list)
|
|
|
|
(args args))
|
|
|
|
(cond
|
|
|
|
((null? pred-list)
|
|
|
|
(values (reverse results) args))
|
|
|
|
((null? args)
|
|
|
|
(values (rev-append results (make-list (length pred-list) #f)) '()))
|
|
|
|
(((car pred-list) (car args))
|
|
|
|
(loop (cons (car args) results)
|
|
|
|
(cdr pred-list)
|
|
|
|
(cdr args)))
|
|
|
|
(else
|
|
|
|
(loop (cons #f results)
|
|
|
|
(cdr pred-list)
|
|
|
|
args)))))
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax optionals
|
|
|
|
(lambda (exp rename compare)
|
|
|
|
(let ((%receive (rename 'receive))
|
|
|
|
(%typed-optionals (rename 'typed-optionals))
|
|
|
|
(%list (rename 'list))
|
|
|
|
(%if (rename 'if))
|
|
|
|
(%pair? (rename 'pair?))
|
|
|
|
(%error (rename 'error))
|
|
|
|
(%let (rename 'let))
|
|
|
|
(%list-ref (rename 'list-ref))
|
|
|
|
|
|
|
|
(args (cadr exp))
|
|
|
|
(var-list (caddr exp))
|
|
|
|
(body (cadddr exp)))
|
|
|
|
`(,%receive (params rest-args)
|
|
|
|
(,%typed-optionals (,%list ,@(map cadr var-list)) ,args)
|
|
|
|
(,%if (pair? rest-args)
|
2003-02-19 13:42:45 -05:00
|
|
|
(,%error "optionals: too many arguments and/or argument type mismatch"
|
|
|
|
rest-args)
|
2002-09-30 10:43:15 -04:00
|
|
|
(,%let (,@(let loop ((counter 0)
|
|
|
|
(var-list var-list))
|
|
|
|
(if (null? var-list)
|
|
|
|
'()
|
|
|
|
(cons (cons (caar var-list) `((,%list-ref params ,counter)))
|
|
|
|
(loop (+ 1 counter)
|
|
|
|
(cdr var-list))))))
|
|
|
|
,body))))))
|
|
|
|
|
|
|
|
;; from uri.scm
|
|
|
|
(define (rev-append a b) ; (append (reverse a) b)
|
|
|
|
(let rev-app ((a a) (b b)) ; Should be defined in a list-proc
|
|
|
|
(if (pair? a) ; package, not here.
|
|
|
|
(rev-app (cdr a) (cons (car a) b))
|
|
|
|
b)))
|
|
|
|
|
2002-09-25 09:02:31 -04:00
|
|
|
(define (make-text-input-field . maybe-further-attributes)
|
|
|
|
(let ((name (generate-input-field-name "text")))
|
2002-09-30 10:43:15 -04:00
|
|
|
(optionals maybe-further-attributes
|
2002-10-01 13:40:08 -04:00
|
|
|
((default-text string?)
|
|
|
|
(attributes XML-attribute?))
|
2002-09-30 10:43:15 -04:00
|
|
|
(make-input-field name
|
|
|
|
identity
|
|
|
|
`(input (@ (type "text")
|
|
|
|
(name ,name)
|
2002-10-01 13:40:08 -04:00
|
|
|
,(and default-text `(value ,default-text))
|
2002-09-30 10:43:15 -04:00
|
|
|
;; this will insert a list, but
|
|
|
|
;; XML->HTML doesn't care about it
|
|
|
|
,(and attributes (cdr attributes))
|
|
|
|
))))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2002-10-02 08:02:56 -04:00
|
|
|
(define make-number-input-field
|
|
|
|
(let ((number-input-field-transformer
|
|
|
|
(lambda (string)
|
|
|
|
(or (string->number string)
|
|
|
|
(error "wrong type")))
|
|
|
|
))
|
2002-10-02 14:22:01 -04:00
|
|
|
(lambda maybe-further-attributes
|
|
|
|
(let ((name (generate-input-field-name "number")))
|
|
|
|
(optionals maybe-further-attributes
|
2002-10-03 08:28:36 -04:00
|
|
|
((default (lambda (a) (or (number? a)
|
|
|
|
(string-or-symbol? a))))
|
|
|
|
(attributes XML-attribute?))
|
2002-10-05 07:58:52 -04:00
|
|
|
(make-input-field
|
2002-10-02 14:22:01 -04:00
|
|
|
name
|
|
|
|
number-input-field-transformer
|
|
|
|
`(input (@ (type "text")
|
|
|
|
(name ,name)
|
2002-10-05 07:58:52 -04:00
|
|
|
,(and default `(value ,default))
|
2002-10-02 14:22:01 -04:00
|
|
|
,(and attributes (cdr attributes))))))))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
(define (make-password-input-field . maybe-further-attributes)
|
|
|
|
(let ((name (generate-input-field-name "password")))
|
2002-09-30 10:43:15 -04:00
|
|
|
(optionals maybe-further-attributes
|
|
|
|
((attributes XML-attribute?))
|
|
|
|
(make-input-field
|
|
|
|
name
|
|
|
|
identity
|
|
|
|
`(input (@ (type "password")
|
|
|
|
(name ,name)
|
2002-11-03 12:42:27 -05:00
|
|
|
,(and attributes (cdr attributes))))))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
|
|
|
(define (make-textarea-input-field . maybe-further-attributes)
|
2002-09-30 10:43:15 -04:00
|
|
|
(let ((name (generate-input-field-name "textarea")))
|
|
|
|
(optionals maybe-further-attributes
|
|
|
|
((default-text string?)
|
|
|
|
(attributes XML-attribute?))
|
|
|
|
(make-input-field
|
|
|
|
name
|
|
|
|
identity
|
|
|
|
`(textarea (@ (type "textarea")
|
|
|
|
(name ,name)
|
|
|
|
,(and attributes (cdr attributes)))
|
|
|
|
,(and default-text))))))
|
|
|
|
|
|
|
|
;(make-select-input-fields '("this" "that" "those") '(@ ((id "sushi"))))
|
|
|
|
;(make-select-input-fields '("this" ("that" '(@ (selected))) "those"))
|
|
|
|
;; dropdown: (size 1)
|
|
|
|
;; multiple choice: (multiple)
|
|
|
|
;; preselected option: (selected)
|
|
|
|
;; changed return value: (value new-value)
|
|
|
|
;; returns a select input field with several options
|
2002-10-04 09:56:46 -04:00
|
|
|
(define make-select-input-field
|
|
|
|
(let ((make-multiple-transformer
|
|
|
|
(lambda (name)
|
|
|
|
(lambda (bindings)
|
|
|
|
(map cdr
|
|
|
|
(filter (lambda (binding)
|
|
|
|
(equal? (car binding) name))
|
|
|
|
bindings))))))
|
|
|
|
|
|
|
|
(lambda (options . maybe-further-attributes)
|
|
|
|
(optionals maybe-further-attributes
|
|
|
|
((multiple? boolean?)
|
|
|
|
(attributes XML-attribute?))
|
|
|
|
(let* ((name (generate-input-field-name "select"))
|
|
|
|
(SXML-options
|
|
|
|
(map (lambda (option)
|
|
|
|
(cond
|
|
|
|
((string-or-symbol? option)
|
|
|
|
(list 'option option))
|
|
|
|
((list? option)
|
2002-09-30 10:43:15 -04:00
|
|
|
(cond
|
2002-10-04 09:56:46 -04:00
|
|
|
((null? (cdr option))
|
|
|
|
`(option ,option))
|
2002-11-09 11:46:26 -05:00
|
|
|
((XML-attribute? (cdr option)) ; w/attribs?
|
|
|
|
`(option ,(cdr option) ,(car option)))
|
2002-09-30 10:43:15 -04:00
|
|
|
(else
|
2002-10-04 09:56:46 -04:00
|
|
|
(error "not an attribute" (cdr option)))))
|
|
|
|
(else
|
|
|
|
(error "not an option" option))))
|
|
|
|
options))
|
|
|
|
(SXML `(select (@ ((name ,name)
|
|
|
|
,(if multiple? '(multiple) '())
|
|
|
|
,(and attributes (cdr attributes))))
|
|
|
|
#\newline
|
|
|
|
,SXML-options)))
|
|
|
|
(if multiple?
|
2002-10-09 11:25:14 -04:00
|
|
|
(make-higher-input-field (make-multiple-transformer name) SXML)
|
2002-10-04 09:56:46 -04:00
|
|
|
(make-input-field name identity SXML)))))))
|
2002-09-30 10:43:15 -04:00
|
|
|
|
|
|
|
;; returns a *list* of radio buttons
|
|
|
|
(define (make-radio-input-fields values . maybe-further-attributes)
|
2002-09-25 09:02:31 -04:00
|
|
|
(let ((name (generate-input-field-name "radio")))
|
2002-09-30 10:43:15 -04:00
|
|
|
(optionals maybe-further-attributes
|
|
|
|
((attributes XML-attribute?))
|
|
|
|
(map (lambda (value)
|
|
|
|
(let ((value-value (if (pair? value) (car value) value))
|
|
|
|
(value-attributes (if (pair? value)
|
2002-11-09 11:46:26 -05:00
|
|
|
(if (XML-attribute? (cdr value))
|
|
|
|
(cddr value)
|
|
|
|
(error "not an attribute" cdr value))
|
2002-09-30 10:43:15 -04:00
|
|
|
#f)))
|
|
|
|
(make-input-field
|
|
|
|
name
|
|
|
|
(lambda (select)
|
2002-11-09 11:46:26 -05:00
|
|
|
select)
|
2002-09-30 10:43:15 -04:00
|
|
|
`(input (@ ((type "radio")
|
|
|
|
(name ,name)
|
|
|
|
(value ,value-value)
|
|
|
|
,(and value-attributes)
|
|
|
|
,(and attributes (cdr attributes))))))))
|
|
|
|
values))))
|
|
|
|
|
|
|
|
;; returns a checkbox input field
|
|
|
|
(define (make-checkbox-input-field . maybe-further-attributes)
|
|
|
|
(let* ((name (generate-input-field-name "checkbox")))
|
|
|
|
(optionals maybe-further-attributes
|
2002-10-04 11:29:50 -04:00
|
|
|
((checked? boolean?)
|
|
|
|
(value (lambda (a) (or (string? a)
|
2002-09-30 10:43:15 -04:00
|
|
|
(number? a)
|
|
|
|
(symbol? a))))
|
|
|
|
(attributes XML-attribute?))
|
|
|
|
(make-input-field
|
|
|
|
name
|
2003-02-19 13:42:45 -05:00
|
|
|
(lambda (value)
|
|
|
|
(or (string=? value "on")
|
|
|
|
value))
|
2002-09-30 10:43:15 -04:00
|
|
|
`(input (@ ((type "checkbox")
|
|
|
|
(name ,name)
|
|
|
|
,(if value `(value ,value) '())
|
2002-10-04 11:29:50 -04:00
|
|
|
,(if checked? '(checked) '())
|
2002-09-30 10:43:15 -04:00
|
|
|
,(and attributes (cdr attributes)))))))))
|
|
|
|
|
|
|
|
|
|
|
|
(define (make-hidden-input-field value . maybe-further-attributes)
|
|
|
|
(let ((name (generate-input-field-name "hidden")))
|
|
|
|
(optionals maybe-further-attributes
|
|
|
|
((attributes XML-attribute?))
|
|
|
|
(make-input-field name
|
|
|
|
identity
|
|
|
|
`(input (@ (type "hidden")
|
|
|
|
(name ,name)
|
|
|
|
(value ,value)
|
|
|
|
,(and attributes (cdr attributes))))))))
|
|
|
|
|
2002-10-02 14:22:01 -04:00
|
|
|
(define (make-button type name button-caption attributes)
|
|
|
|
(make-input-field name
|
|
|
|
identity
|
|
|
|
`(input (@ (type ,type)
|
|
|
|
(name ,name)
|
|
|
|
,(and button-caption `(value ,button-caption))
|
|
|
|
,(and attributes (cdr attributes))))))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2002-10-02 08:02:56 -04:00
|
|
|
(define (string-or-symbol? a)
|
|
|
|
(or (string? a)
|
|
|
|
(symbol? a)))
|
|
|
|
|
2002-09-25 09:02:31 -04:00
|
|
|
(define (make-submit-button . maybe-further-attributes)
|
2002-09-30 10:43:15 -04:00
|
|
|
(optionals maybe-further-attributes
|
2002-10-02 08:02:56 -04:00
|
|
|
((button-caption string-or-symbol?)
|
2002-09-30 10:43:15 -04:00
|
|
|
(attributes XML-attribute?))
|
2002-10-02 14:22:01 -04:00
|
|
|
(make-button "submit" (generate-input-field-name "submit")
|
|
|
|
button-caption attributes)))
|
2002-09-30 10:43:15 -04:00
|
|
|
|
|
|
|
(define (make-reset-button . maybe-further-attributes)
|
|
|
|
(optionals maybe-further-attributes
|
2002-10-02 08:02:56 -04:00
|
|
|
((button-caption string-or-symbol?)
|
2002-09-30 10:43:15 -04:00
|
|
|
(attributes XML-attribute?))
|
2002-10-02 14:22:01 -04:00
|
|
|
(make-button "reset" (generate-input-field-name "reset")
|
|
|
|
button-caption attributes)))
|
2002-09-30 10:43:15 -04:00
|
|
|
|
|
|
|
(define (make-image-button image-source . maybe-further-attributes)
|
|
|
|
(optionals maybe-further-attributes
|
|
|
|
((attributes XML-attribute?))
|
2002-10-02 14:22:01 -04:00
|
|
|
(make-button "image" (generate-input-field-name "imgbtn")
|
|
|
|
#f `(@ (src ,image-source)
|
|
|
|
,@(if attributes (cdr attributes) '())))))
|
2002-09-27 13:29:31 -04:00
|
|
|
|
2002-10-04 09:56:46 -04:00
|
|
|
;; <input-field>: '(input-field . <real-input-field>)
|
|
|
|
;; <real-input-field>: #{Input-field "name"}
|
2002-11-07 15:41:35 -05:00
|
|
|
(define (raw-input-field-value input-field bindings)
|
2002-09-27 11:24:44 -04:00
|
|
|
(let ((input-field (cadr input-field)))
|
|
|
|
(cond
|
2002-09-27 13:29:31 -04:00
|
|
|
((input-field-get-bindings? input-field)
|
|
|
|
((input-field-transformer input-field) bindings))
|
2002-10-02 14:22:01 -04:00
|
|
|
((real-input-field-binding input-field bindings) =>
|
2002-09-27 11:24:44 -04:00
|
|
|
(lambda (binding)
|
|
|
|
((input-field-transformer input-field) (cdr binding))))
|
|
|
|
(else
|
|
|
|
(error "no such input-field" input-field bindings)))))
|
|
|
|
|
2002-11-07 09:11:05 -05:00
|
|
|
;; Trys to get a value for INPUT-FIELD in BINDINGS. If this fails
|
2002-11-07 15:41:35 -05:00
|
|
|
;; (i.e. RAW-INPUT-FIELD-VALUE returns an error), the default-value is
|
2002-11-07 09:11:05 -05:00
|
|
|
;; returned. The default-value defaults to #f. NOTE: If you do this
|
|
|
|
;; with input-fields whose valid values may be the same as the default
|
|
|
|
;; value, you cannot determine by the result if there was such a value
|
2002-11-07 15:41:35 -05:00
|
|
|
;; or not. Keep in mind, that RAW-INPUT-FIELD-VALUE returns also an
|
|
|
|
;; error, if there was not such an input field. This makes
|
|
|
|
;; INPUT-FIELD-VALUE working with checkbox input fields because they
|
|
|
|
;; miss if they are not checked.
|
|
|
|
(define (input-field-value input-field bindings . maybe-default)
|
2002-11-07 09:11:05 -05:00
|
|
|
(let ((default (:optional maybe-default #f)))
|
|
|
|
(with-fatal-error-handler
|
|
|
|
(lambda (condition more)
|
|
|
|
default)
|
2002-11-07 15:41:35 -05:00
|
|
|
(raw-input-field-value input-field bindings))))
|
2002-11-07 09:11:05 -05:00
|
|
|
|
2002-10-02 14:22:01 -04:00
|
|
|
(define (real-input-field-binding input-field bindings)
|
|
|
|
(assoc (input-field-name input-field) bindings))
|
2002-09-25 09:02:31 -04:00
|
|
|
|
2002-10-02 14:22:01 -04:00
|
|
|
(define (input-field-binding input-field bindings)
|
2002-10-02 16:28:39 -04:00
|
|
|
(real-input-field-binding (cadr input-field) bindings))
|
|
|
|
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; return address
|
|
|
|
|
|
|
|
;; generates an unique return-addres
|
|
|
|
;; may be used like
|
|
|
|
;; (let ((address (make-address)))
|
|
|
|
;; (send-html/suspend
|
|
|
|
;; (lambda (new-url)
|
|
|
|
;; ...
|
|
|
|
;; (URL (address new-url) "Click here to get more")...)
|
2003-02-28 03:22:29 -05:00
|
|
|
|
|
|
|
(define-record-type address :address
|
|
|
|
(really-make-address name annotated?)
|
|
|
|
(name address-name)
|
|
|
|
(annotated? address-annotated?))
|
|
|
|
|
2002-10-02 16:28:39 -04:00
|
|
|
(define (make-address)
|
2003-02-28 03:22:29 -05:00
|
|
|
(let ((address (really-make-address
|
|
|
|
(generate-unique-name "return") #f)))
|
2002-10-02 16:28:39 -04:00
|
|
|
(lambda (message)
|
|
|
|
(cond
|
|
|
|
((string? message)
|
2003-02-28 03:22:29 -05:00
|
|
|
(string-append message "?" (address-name address) "="))
|
|
|
|
((eq? message 'address)
|
|
|
|
address)
|
|
|
|
(else
|
|
|
|
(error "address: unknown message/bad argument"
|
|
|
|
message (address-name address)))))))
|
|
|
|
|
|
|
|
(define (make-annotated-address)
|
|
|
|
(let ((address (really-make-address
|
|
|
|
(generate-unique-name "return")
|
|
|
|
#t)))
|
|
|
|
(lambda (message . annotation)
|
|
|
|
(cond
|
|
|
|
((and (string? message)
|
|
|
|
(<= (length annotation) 1))
|
|
|
|
(let ((escaped-annotation
|
|
|
|
(if (null? annotation)
|
|
|
|
""
|
|
|
|
(escape-uri (car annotation)))))
|
|
|
|
(string-append message "?" (address-name address)
|
|
|
|
"=" escaped-annotation)))
|
|
|
|
((eq? message 'address)
|
|
|
|
address)
|
|
|
|
(else
|
|
|
|
(error "annotated-address: unknown message/bad argument(s)"
|
|
|
|
message (address-name address)))))))
|
2002-10-02 16:28:39 -04:00
|
|
|
|
2003-03-03 05:27:49 -05:00
|
|
|
(define (returned-via return-object bindings)
|
2003-02-19 13:42:45 -05:00
|
|
|
(if (input-field? return-object)
|
|
|
|
(input-field-binding return-object bindings)
|
|
|
|
;; We assume we have a return-address-object instead.
|
2003-02-28 03:22:29 -05:00
|
|
|
(let ((address (return-object 'address)))
|
|
|
|
(cond
|
|
|
|
((assoc (address-name address) bindings) =>
|
|
|
|
(lambda (pair)
|
|
|
|
(if (address-annotated? address)
|
|
|
|
(unescape-uri (cdr pair))
|
|
|
|
#t)))
|
|
|
|
(else #f)))))
|
2002-10-02 16:28:39 -04:00
|
|
|
|
2003-03-03 05:27:49 -05:00
|
|
|
;; It depends on the object, if returned-via returns only boolean
|
|
|
|
;; values or string values as well. So let us have both names.
|
|
|
|
(define returned-via? returned-via)
|
|
|
|
|
|
|
|
;; This is from Martin Gasbichler
|
|
|
|
(define-syntax case-returned-via
|
|
|
|
(syntax-rules (else)
|
|
|
|
((case-returned-via (%bindings ...) clauses ...)
|
|
|
|
(let ((bindings (%bindings ...)))
|
|
|
|
(case-returned-via bindings clauses ...)))
|
|
|
|
((case-returned-via bindings (else body ...))
|
|
|
|
(begin body ...))
|
|
|
|
((case-returned-via bindings
|
|
|
|
((%return-object ...) %body ...))
|
|
|
|
(if (or (returned-via? %return-object bindings) ...)
|
|
|
|
(begin %body ...)))
|
|
|
|
((case-returned-via bindings
|
|
|
|
((%return-object ...) %body ...)
|
|
|
|
%clause %clauses ...)
|
|
|
|
(if (or (returned-via? %return-object bindings) ...)
|
|
|
|
(begin %body ...)
|
|
|
|
(case-returned-via bindings %clause %clauses ...)))))
|