cgi and html

Featuring the Dragonfly web framework
Locked
eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

cgi and html

Post by 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.

Code: Select all

(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 "%s\n%s"  (eval $w$)  (to</tag> (rest $L$))))))

(define (:CGI $w$)
  (print (format "Content-type: text/html\n\n%s" $w$)))
For any CGIers out there.

Eddie

eddier
Posts: 289
Joined: Mon Oct 07, 2002 2:48 pm
Location: Blue Mountain College, MS US

Post by eddier »

on the line with

Code: Select all

 (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

Locked