cgi and html

Started by eddier, December 02, 2004, 01:31:27 PM

Previous topic - Next topic

eddier

I thought I had posted this, but I guess I didn't. And if I did, this is an update. Anyway a convient cgi.lsp for cgi.



(context 'CGI)

;; by Eddie Rucker
;; allows easyly interchangable HTML and newLISP code
;; version 0.6 (Dec 2, 2004) -- made code a bit better, turned around the "="
;; version 0.5 (Nov 29, 2004) -- fixed symbol evaluation
;; NO WARRANTY OF ANY KIND
;; credit for the idea goes to Tim Bradshaw although mine is a bit different

;; example
;;(load "cgi.lsp")
;; (:CGI
;;  (:html
;;   (:head
;;    (:title "fred")
;;   (:body
;;    (:form action="test.lsp" method="get"
;;     (:h1 style="text-align:center" "fred")
;;     (:table width="100%"
;;      (:tr
;;       (:td "First Name:")
;;       (:td (:input type="text" size="15"))
;;      (:tr
;;       (:td "Last Name:")
;;       (:td (:input type="text" size="15")))))))))
;;(exit)

;; parse url
(map (fn ($x$)
(let ($L$ (parse $x$ "="))
  (set (symbol (first $L$)) (last $L$))))
     (parse (replace "%([+0-9A-F]{2})"
    (or (read-line) (env "QUERY_STRING") "")
    (if (= $1 "+") " " (char (integer (append "0x" $1)))) 0)
   "[&;]" 0))

(context 'MAIN)

;; <tag>...</tag> constructs -- add to the list as needed
(dolist ($x$ '("html" "head" "body" "form" "style" "title" "div" "table" "tr"
      "th" "td" "a" "h1" "h2" "h3" "h4" "h5" "h6" "sub" "sup" "p" "b"
      "i" "u" "center" "option" "select" "ul" "ol" "dl" "li" "dt"
      "dd" "caption" "textarea"))
  (eval-string
   (format "(define-macro (:%s) (format "<%s%%s</%s>" (<tag> (args))))"
  $x$ $x$ $x$)))

;; <tag /> constructs -- add to the list as needed
(dolist ($x$ '("br" "hr" "img" "input"))
  (eval-string
   (format "(define-macro (:%s) (format "<%s%%s" (<tag /> (args))))"
  $x$ $x$)))


(define (<tag/> $L$)
  (let ($w$ (first $L$) $rl$ (rest (rest $L$)))
    (if
(empty? $L$)  
 " />n"
        (and (symbol? $w$) (= "=" (last (string $w$))))
 (format " %s="%s"%s"
 (chop (string $w$))
 (eval (nth 1 $L$))
 (<tag/> $rl$))) ))

(define (<tag> $L$)
  (let ($w$ (first $L$) $rl$ (rest (rest $L$)))
    (if
(empty? $L$)
 ">n"
(and (symbol? $w$) (= "=" (last (string $w$))))
 (format " %s="%s"%s"
 (chop (string $w$))
 (eval (nth 1 $L$))
 (<tag> $rl$))
(append ">n" (to</tag> $L$))) ))

(define (to</tag> $L$)
  (let ($w$ (first $L$))
    (if (empty? $L$)
""
      (format "%sn%s"  (eval $w$)  (to</tag> (rest $L$))))))

(define (:CGI $w$)
  (print (format "Content-type: text/htmlnn%s" $w$)))

For any CGIers out there.



Eddie

eddier

#1
on the line with

(format "(define-macro (:%s) (format "<%s%%s" (<tag /> (args))))"

Kill the space between "(<tab" and "/>"



I don't know how that got in there.



Eddie