r2948: *** empty log message ***
[kmrcl.git] / web-utils-aserve.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          web-utils-aserve.lisp
6 ;;;; Purpose:       Web utilities based on aserve functions
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: web-utils-aserve.lisp,v 1.1 2002/10/06 13:21:47 kevin Exp $
11 ;;;;
12 ;;;; This file, part of Webutils, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; Webutils users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the GNU General Public License.
16 ;;;; *************************************************************************
17
18
19
20 (in-package :webutils)
21 (declaim (optimize (speed 3) (safety 1)))
22
23
24 ;;; AllegroServe interaction functions
25
26 (defun cgi-var (var req)
27   "Look CGI variable in AllegroServe association list"
28   (cdr (assoc var (net.aserve:request-query req) :test #'equal)))
29
30 (defun princ-http (s)
31   (princ s *html-stream*))
32
33 (defun print-http (s)
34   (format *html-stream* "~a~%" s))
35
36
37 ;;; Tag functions
38
39 (defmacro with-tag (tag &rest body)
40   "Outputs to http tag and executes body"
41   `(prog1
42        (progn
43          (princ-http (format nil "<~a>" ,tag))
44          ,@body)
45      (princ-http (format nil "</~a>" ,tag))))
46   
47 (defmacro with-tag-attribute (tag attribute &rest body)
48   "Outputs to http tag + attribute and executes body"
49   `(prog1
50        (progn
51          (princ-http (format nil "<~a ~a>" ,tag ,attribute))
52          ,@body)
53      (princ-http (format nil "</~a>" ,tag))))
54   
55 (defun princ-http-with-color (text color)
56   (with-tag-attribute "font" (format nil "color=\"~a\"" color)
57                       (princ-http text)))
58
59 (defun princ-http-with-size (text size)
60   (with-tag-attribute "font" (format nil "size=\"~a\"" size)
61                       (princ-http text)))
62
63 (defmacro with-link ((href xml linktype) &rest body)
64   (declare (ignore linktype))
65 ;   (format *html-stream* "Return to <cui2 xml:href=\"qstr\">Home</cui2>")
66 ;   (format *html-stream* "Return to <go xml:link=\"simple\" show=\"replace\" href=\"qstr/\">Home</go>")
67   `(if ,xml
68        (progn
69          (princ-http "<elem xlink:type=\"simple\" xlink:href=\"")
70          (princ-http ,href)
71          (princ-http "\">")
72          ,@body
73          (princ-http "</elem>"))
74      (progn 
75        (princ-http "<a href=\"")
76        (princ-http ,href)
77        (princ-http "\">")
78        ,@body
79        (princ-http "</a>"))))
80
81 (defun home-link (&key (xml nil) (vars nil))
82   (princ-http "<font size=\"-1\">Return to ")
83   (with-link ((make-url "index.html" :vars vars) xml "homelink")
84     (princ-http "Browser Home"))
85   (princ-http "</font><p></p>"))
86
87 (defun head (title-str)
88   (net.html.generator:html 
89    (:head 
90     "<LINK rel=\"stylesheet\" href=\"http://www.med-info.com/main.css\" type=\"text/css\">"
91     (:title (:princ-safe title-str)))))
92
93
94
95 ;;; Page wrappers
96
97 (defmacro with-xml-page (title &rest body)
98   `(prog1
99        (progn
100          (net.html.generator:html
101           (princ-http (std-xml-header))
102           (princ-http "<pagedata xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"))
103          (with-tag "pagetitle" (princ-http ,title))
104          ,@body)
105      (princ-http "</pagedata>")))
106
107 (defmacro with-trans-page (title &rest body)
108   `(prog1
109        (progn
110           (print-http "<?xml version=\"1.0\" standalone=\"yes\"?>")
111           (print-http "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"")
112           (print-http " \"http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">")
113           (print-http "")
114           (print-http "<html xmlns=\"http://www.w3.org/1999/xhtml\">")
115           (head ,title)
116           (print-http "<body bgcolor=\"#FFFFFF\">")
117           (prog1 
118               ,@body
119             (print-http "</body>")))
120      (print-http "</html>")))
121
122
123 ;;; URL Encoding
124
125 (defun encode-query (query)
126   "Escape [] from net.aserve's query-to-form-urlencoded"
127   (substitute-string-for-char
128    (substitute-string-for-char
129     (substitute-string-for-char 
130      (substitute #\+ #\space query)
131      #\[ "%5B")
132     #\] "%5D")
133    #\" "%22"))
134
135