1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Lisp Markup Language functions
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Aug 2002
10 ;;;; $Id: lml.cl,v 1.3 2002/09/16 08:10:11 kevin Exp $
12 ;;;; This file, part of LML, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; LML users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the GNU General Public License v2
16 ;;;; (http://www.gnu.org/licenses/gpl.html)
17 ;;;; *************************************************************************
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
22 (defconstant +html4-prologue-string+ "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">"))
24 (defconstant +xml-prologue-string+ "<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"yes\"?>"))
26 (defconstant +xhtml-prologue-string+
27 "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"))
29 (defvar *print-spaces* nil)
31 (defun reset-indent ()
34 (defun lml-print (str &rest args)
35 (when (streamp *html-output*)
36 (when *print-spaces* (indent-spaces *indent* *html-output*))
38 (apply #'format *html-output* str args)
39 (princ str *html-output*))
40 (when *print-spaces* (format *html-output* "~%"))
43 (defmacro lml-line (str &rest args)
44 `(lml-print ,str ,@args))
46 (defun lml-print-date (date)
47 (lml-print (date-string date)))
49 (defmacro lml-exec-body (&body forms)
57 `(lml-print "~D" ,form))
59 `(lml-print (string-downcase (symbol-name ,form))))
66 (defmacro with-attr-string (tag attr-string &body body)
67 (let ((attr (gensym)))
68 `(let ((,attr ,attr-string))
69 (lml-print "<~(~A~)~A>" ',tag
70 (if (and (stringp ,attr) (plusp (length ,attr)))
71 (format nil "~A" ,attr)
74 (lml-exec-body ,@body)
76 (lml-print "</~(~A~)>" ',tag))))
78 (defun one-keyarg-string (key value)
79 "Return attribute string for keys"
80 (format nil "~(~A~)=\"~A\"" key
83 (string-downcase (symbol-name value)))
89 (defmacro with-keyargs (tag keyargs &body body)
94 (dolist (,kv ',keyargs)
96 (push (one-keyarg-string (car ,kv) it) ,attr)))
97 (with-attr-string ,tag (list-to-spaced-string (nreverse ,attr)) ,@body)))))
99 (defmacro with (tag &rest args)
105 (let ((arg (nth i args))
106 (value (when (< (1+ i) n)
108 (if (keyword-symbol? arg)
110 (push (list arg value) keyargs)
113 `(with-keyargs ,tag ,keyargs ,@(nreverse body))))
116 (defmacro keyargs-string (&rest args)
117 "Returns a string of attributes and values. Result contains a leading space."
118 (let ((keyarg-list '()))
119 (loop for ( name val ) on args by #'cddr
122 (push (one-keyarg-string name val) keyarg-list)))
123 (list-to-spaced-string (nreverse keyarg-list))))
126 (defmacro xhtml-prologue ()
128 (lml-print "~A~%" +xml-prologue-string+)
129 (lml-print "~A~%" +xhtml-prologue-string+)))
131 (defmacro link (dest &body body)
132 `(with a :href ,dest ,@body))
134 (defmacro link-c (class dest &body body)
135 `(with a :href ,dest :class ,class ,@body))
137 (defmacro img (dest &key class id alt style width height align)
139 (eval `(keyargs-string :class ,class :id ,id :alt ,alt :style ,style
140 :width ,width :height ,height :align ,align))))
141 `(lml-print ,(format nil "<img src=\"~A\"~A />" dest attr))))
143 (defmacro input (&key name class id type style size maxlength value)
145 (eval `(keyargs-string :name ,name :class ,class :id ,id :style ,style
146 :size ,size :maxlength ,maxlength :value ,value
148 `(lml-print ,(format nil "<input~A />" attr))))
150 (defmacro meta (name content)
151 `(with meta :name ,name :content ,content))
153 (defmacro meta-key (&key name content http-equiv)
154 `(with meta :name ,name :content ,content :http-equiv ,http-equiv))
157 `(lml-print "<br />"))
160 `(lml-print "<hr />"))
162 (defmacro lml-tag-macro (tag)
164 (defmacro ,tag (&body body)
165 `(with ,',tag ,@body))
168 (defmacro lml-tag-class-macro (tag)
169 (let ((name (intern (format nil "~A-~A" tag :c))))
171 (defmacro ,name (&body body)
172 `(with ,',tag :class ,(car body) ,@(cdr body)))
175 (eval-when (:compile-toplevel :load-toplevel :execute)
176 (defparameter *macro-list*
177 '(a div span h1 h2 h3 h4 h5 h6 i b p li ul ol table tbody td tr body head
178 html title pre tt u dl dt dd kbd code form))
179 (export '(link link-c br hr img input meta meta-key))
180 (export *macro-list*))
182 (loop for i in *macro-list*
184 (eval `(lml-tag-macro ,i))
185 (eval `(lml-tag-class-macro ,i)))
187 (defmacro print-page (title &body body)
193 (defmacro page (out-file &body body)
194 `(with-open-file (*html-output*
195 (lml-file-name ,out-file :output)
197 :if-exists :supersede)
199 (html :xmlns "http://www.w3.org/1999/xhtml"