1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: web-utils-aserve.lisp
6 ;;;; Purpose: Web utilities based on aserve functions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: web-utils-aserve.lisp,v 1.10 2002/10/18 07:28:57 kevin Exp $
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
21 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
24 ;;; AllegroServe interaction functions
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)))
31 (princ s *html-stream*))
34 (format *html-stream* "~a~%" s))
39 (defmacro with-tag (tag &rest body)
40 "Outputs to http tag and executes body"
43 (princ-http (format nil "<~a>" ,tag))
45 (princ-http (format nil "</~a>" ,tag))))
47 (defmacro with-tag-attribute (tag attribute &rest body)
48 "Outputs to http tag + attribute and executes body"
51 (princ-http (format nil "<~a ~a>" ,tag ,attribute))
53 (princ-http (format nil "</~a>" ,tag))))
55 (defun princ-http-with-color (text color)
56 (with-tag-attribute "font" (format nil "color=\"~a\"" color)
59 (defun princ-http-with-size (text size)
60 (with-tag-attribute "font" (format nil "size=\"~a\"" size)
63 (defmacro with-link ((href &key (format :html)) &rest body)
64 ; (format *html-stream* "Return to <cui2 xml:href=\"qstr\">Home</cui2>")
65 ; (format *html-stream* "Return to <go xml:link=\"simple\" show=\"replace\" href=\"qstr/\">Home</go>")
68 (princ-http "<xmllink xlink:type=\"simple\" xlink:href=\"")
72 (princ-http "</xmllink>"))
74 (princ-http "<html:a href=\"")
78 (princ-http "</html:a>"))
80 (princ-http "<a href=\"")
84 (princ-http "</a>"))))
86 (defun home-link (&key (format :html) (vars nil))
89 (princ-http "<div class=\"homelink\">Return to ")
90 (with-link ((make-url "index.html" :vars vars))
92 (princ-http "</div>"))
94 (princ-http "<homelink>Return to ")
95 (with-link ((make-url "index.html" :vars vars :format format) :format format)
97 (princ-http "</homelink>"))))
99 (defun head (title-str &key css)
101 (setq css "http://b9.com/main.css"))
102 (net.html.generator:html
104 (princ-http (format nil "<link rel=\"stylesheet\" href=\"~A\" type=\"text/css\"></link>" css))
105 (:title (:princ-safe title-str)))))
111 (defmacro with-page ((title &key css (format :xhtml)) &rest body)
116 (net.html.generator:html
117 (print-http *standard-xhtml-header*)
118 (print-http "<html xmlns=\"http://www.w3.org/1999/xhtml\">")
119 (head ,title :css ,css)
120 (print-http "<body>")
123 (print-http "</body></html>"))))))
127 (net.html.generator:html
128 (print-http *standard-html-header*)
129 (head ,title :css ,css)
130 (print-http "<body>")
133 (print-http "</body></html>"))))))
137 (net.html.generator:html
138 (princ-http *standard-xml-header*)
139 (princ-http "<pagedata xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"))
140 (with-tag "pagetitle" (princ-http ,title))
142 (princ-http "</pagedata>")))))
147 (defun encode-query (query)
148 "Escape [] from net.aserve's query-to-form-urlencoded"
149 (substitute-string-for-char
150 (substitute-string-for-char
151 (substitute-string-for-char
152 (substitute #\+ #\space query)