2 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
3 ;;;; *************************************************************************
4 ;;;; FILE IDENTIFICATION
6 ;;;; Name: web-utils-aserve.lisp
7 ;;;; Purpose: Web utilities based on aserve functions
8 ;;;; Programmer: Kevin M. Rosenberg
9 ;;;; Date Started: Apr 2000
11 ;;;; $Id: web-utils-aserve.lisp,v 1.6 2002/10/16 21:58:49 kevin Exp $
13 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
15 ;;;; KMRCL users are granted the rights to distribute and use this software
16 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
17 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
18 ;;;; *************************************************************************
22 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3)))
25 ;;; AllegroServe interaction functions
27 (defun cgi-var (var req)
28 "Look CGI variable in AllegroServe association list"
29 (cdr (assoc var (net.aserve:request-query req) :test #'equal)))
32 (princ s *html-stream*))
35 (format *html-stream* "~a~%" s))
40 (defmacro with-tag (tag &rest body)
41 "Outputs to http tag and executes body"
44 (princ-http (format nil "<~a>" ,tag))
46 (princ-http (format nil "</~a>" ,tag))))
48 (defmacro with-tag-attribute (tag attribute &rest body)
49 "Outputs to http tag + attribute and executes body"
52 (princ-http (format nil "<~a ~a>" ,tag ,attribute))
54 (princ-http (format nil "</~a>" ,tag))))
56 (defun princ-http-with-color (text color)
57 (with-tag-attribute "font" (format nil "color=\"~a\"" color)
60 (defun princ-http-with-size (text size)
61 (with-tag-attribute "font" (format nil "size=\"~a\"" size)
64 (defmacro with-link ((href xml linktype) &rest body)
65 (declare (ignore linktype))
66 ; (format *html-stream* "Return to <cui2 xml:href=\"qstr\">Home</cui2>")
67 ; (format *html-stream* "Return to <go xml:link=\"simple\" show=\"replace\" href=\"qstr/\">Home</go>")
70 (princ-http "<elem xlink:type=\"simple\" xlink:href=\"")
74 (princ-http "</elem>"))
76 (princ-http "<a href=\"")
80 (princ-http "</a>"))))
82 (defun home-link (&key (xml nil) (vars nil))
83 (princ-http "<font size=\"-1\">Return to ")
84 (with-link ((make-url "index.html" :vars vars) xml "homelink")
85 (princ-http "Browser Home"))
86 (princ-http "</font><p></p>"))
88 (defun head (title-str &key css)
90 (setq css "http://b9.com/main.css"))
91 (net.html.generator:html
93 (princ-http (format nil "<link rel=\"stylesheet\" href=\"~A\" type=\"text/css\"></link>" css))
94 (:title (:princ-safe title-str)))))
100 (defmacro with-page ((title &key css (format :html)) &rest body)
105 (net.html.generator:html
106 (print-http "<?xml version=\"1.0\" standalone=\"yes\"?>")
107 (print-http "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"")
108 (print-http " \"http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">")
110 (print-http "<html xmlns=\"http://www.w3.org/1999/xhtml\">")
111 (head ,title :css ,css)
112 (print-http "<body>")
115 (print-http "</body></html>"))))))
119 (net.html.generator:html
120 (princ-http (std-xml-header))
121 (princ-http "<pagedata xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"))
122 (with-tag "pagetitle" (princ-http ,title))
124 (princ-http "</pagedata>")))))
129 (defun encode-query (query)
130 "Escape [] from net.aserve's query-to-form-urlencoded"
131 (substitute-string-for-char
132 (substitute-string-for-char
133 (substitute-string-for-char
134 (substitute #\+ #\space query)