r2656: initial import
[lml.git] / lml.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          lml.cl
6 ;;;; Purpose:       Lisp Markup Language functions
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Aug 2002
9 ;;;;
10 ;;;; $Id: lml.cl,v 1.1 2002/09/16 01:13:49 kevin Exp $
11 ;;;;
12 ;;;; This file, part of LML, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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 ;;;; *************************************************************************
18
19 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
20 (in-package :lml)
21
22 (defconstant +html4-prologue-string+
23     (format nil
24             "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">~%"))
25
26 (defconstant +xml-prologue-string+ 
27     (format nil
28             "<?xml version=\"1.0\" encoding=\"iso-8859-1\" standalone=\"yes\"?>~%"))
29
30 (defconstant +xhtml-prologue-string+
31     (format nil
32             "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">~%"))
33
34 (defvar *print-spaces* nil)
35 (defvar *indent* 0)
36 (defun reset-indent ()
37   (setq *indent* 0))
38
39 (defun lml-print (str &rest args)
40   (when (streamp *html-output*)
41     (when *print-spaces* (indent-spaces *indent* *html-output*))
42     (if args
43         (apply #'format *html-output* str args)
44       (princ str *html-output*))
45     (when *print-spaces* (format *html-output* "~%"))
46     (values)))
47
48 (defmacro lml-line (str &rest args)
49   `(lml-print ,str ,@args))
50
51 (defun lml-print-date (date)
52   (lml-print (date-string date)))
53
54 (defmacro lml-exec-body (&body forms)
55   `(progn
56     ,@(mapcar
57        #'(lambda (form)
58            (etypecase form
59              (string
60               `(lml-print ,form))
61              (number
62               `(lml-print "~D" ,form))
63              (symbol
64               `(lml-print (string-downcase (symbol-name ,form))))
65              (nil
66               nil)
67              (cons
68               form)))
69        forms)))
70
71 (defmacro with-attr-string (tag attr-string &body body)
72   (let ((attr (gensym)))
73   `(let ((,attr ,attr-string))
74      (lml-print "<~(~A~)~A>" ',tag
75               (if (and (stringp ,attr) (plusp (length ,attr)))
76                   (format nil "~A" ,attr)
77                 ""))
78      (incf *indent*)
79      (lml-exec-body ,@body)
80      (decf *indent*)
81      (lml-print "</~(~A~)>" ',tag))))
82
83 (defun one-keyarg-string (key value)
84   "Return attribute string for keys"
85   (format nil "~(~A~)=\"~A\"" key
86           (typecase value
87             (symbol
88              (string-downcase (symbol-name value)))
89             (string
90              value)
91             (t
92              (eval value)))))
93
94 (defmacro with-keyargs (tag keyargs &body body)
95   (let ((attr (gensym))
96         (kv (gensym)))
97   `(progn
98      (let ((,attr '()))
99        (dolist (,kv ',keyargs)
100          (awhen (cadr ,kv)
101            (push (one-keyarg-string (car ,kv) it) ,attr)))
102        (with-attr-string ,tag (list-to-spaced-string (nreverse ,attr)) ,@body)))))
103
104 (defmacro with (tag &rest args)
105   (let ((body '())
106         (keyargs '())
107         (n (length args)))
108     (do ((i 0 (1+ i)))
109         ((> i (1- n)))
110       (let ((arg (nth i args))
111             (value (when (< (1+ i) n)
112                      (nth (1+ i) args))))
113         (if (keyword-symbol? arg)
114             (progn
115               (push (list arg value) keyargs)
116               (incf i))
117           (push arg body))))
118     `(with-keyargs ,tag ,keyargs ,@(nreverse body))))
119
120
121 (defmacro keyargs-string (&rest args)
122   "Returns a string of attributes and values. Result contains a leading space."
123   (let ((keyarg-list '()))
124     (loop for ( name val ) on args by #'cddr
125           do
126           (when val
127             (push (one-keyarg-string name val) keyarg-list)))
128     (list-to-spaced-string (nreverse keyarg-list))))
129   
130
131 (defmacro xhtml-prologue ()
132   `(progn
133      (lml-print +xml-prologue-string+)
134      (lml-print +xhtml-prologue-string+)))
135
136 (defmacro link (dest &body body)
137   `(with a :href ,dest ,@body))
138
139 (defmacro link-c (class dest &body body)
140   `(with a :href ,dest :class ,class ,@body))
141
142 (defmacro img (dest &key class id alt style width height align)
143   (let ((attr
144          (eval `(keyargs-string :class ,class :id ,id :alt ,alt :style ,style
145                              :width ,width :height ,height :align ,align))))
146     `(lml-print ,(format nil "<img src=\"~A\"~A />" dest attr))))
147
148 (defmacro input (&key name class id type style size maxlength value)
149   (let ((attr
150          (eval `(keyargs-string :name ,name :class ,class :id ,id :style ,style
151                              :size ,size :maxlength ,maxlength :value ,value
152                              :type ,type))))
153     `(lml-print ,(format nil "<input~A />" attr))))
154
155 (defmacro meta (name content)
156   `(with meta :name ,name :content ,content))
157
158 (defmacro meta-key (&key name content http-equiv)
159   `(with meta :name ,name :content ,content :http-equiv ,http-equiv))
160
161 (defmacro br ()
162   `(lml-print "<br />"))
163
164 (defmacro lml-tag-macro (tag)
165   `(progn
166      (defmacro ,tag (&body body)
167        `(with ,',tag ,@body))
168      (export ',tag)))
169
170 (defmacro lml-tag-class-macro (tag)
171   (let ((name (intern (format nil "~A-~A" tag :c))))
172     `(progn
173        (defmacro ,name (&body body)
174          `(with ,',tag :class ,(car body) ,@(cdr body)))
175        (export ',name))))
176
177 (eval-when (:compile-toplevel :load-toplevel :execute)
178   (defparameter *macro-list*
179     '(a div span h1 h2 h3 h4 h5 h6 i b p li ul ol table tbody td tr body head
180           html title pre tt u dl dt dd kbd code form))
181   (export '(link link-c br img input meta meta-key))
182   (export *macro-list*))
183
184 (loop for i in *macro-list*
185       do
186       (eval `(lml-tag-macro ,i))
187       (eval `(lml-tag-class-macro ,i)))
188
189 (defmacro print-page (title &body body)
190   `(html
191     (head
192      (title ,title))
193     (body ,@body)))
194
195 (defmacro page (out-file &body body)
196   `(with-open-file (*html-output*
197                     (lml-file-name ,out-file :output)
198                     :direction :output
199                     :if-exists :supersede)
200      (xhtml-prologue)
201      (html :xmlns "http://www.w3.org/1999/xhtml"
202        ,@body)))