Subject: Re: Keywords and CL-WHO
From: rpw3@rpw3.org (Rob Warnock)
Date: Fri, 22 Sep 2006 00:11:24 -0500
Newsgroups: comp.lang.lisp
Message-ID: <kqqdnTkoOdzh7Y7YnZ2dnUVZ_q6dnZ2d@speakeasy.net>
Dustin Withers <fadeddata@gmail.com> wrote:
+---------------
| a wrote:
| > (cl-who:with-html-output (*standard-output* nil :prologue t)
| >    `(,(intern "FOO" :keyword) (:body "test")))
| >
| > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
| > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
| > (:FOO (:BODY "test"))
| 
| Ok so that works...
+---------------

No it doesn't, sorry. "A" confused himself [and you]:

1. The *only* output which went to *STANDARD-OUTPUT* was the prologue.
   None of the FOO stuff went anywhere.

2. WITH-HTML-OUTPUT returns a *value* [which the REPL printed], which
   in the above case happened to be the value of the last form in the
   &BODY arg. The (:FOO (:BODY "test")) value shown above is the result
   of the *evaluation* of the backquoted form, which had no side-effects,
   and in particular was *NOT* output to *STANDARD-OUTPUT*.

The following may make it more clear: First, note that we can replace
the backquoted form by its exact [except for potential sharing of
structure] equivalent expansion:

    > `(,(intern "FOO" :keyword) (:body "test"))

    (:FOO (:BODY "test"))
    > (list (intern "FOO" :keyword) '(:body "test"))

    (:FOO (:BODY "test"))
    > (equal * **)		; See? They're equal.

    T
    > 

Now let's put that version into a sexp-based HTML template
and look at the code that WITH-HTML-OUTPUT generates:

    > (macroexpand
       '(cl-who:with-html-output (*standard-output* nil :prologue t)
	  (:html
	    (:body
	      "Some text"
	      (list (intern "FOO" :keyword) '(:body "test"))
	      "Some more text"))))

    (LET ((*STANDARD-OUTPUT* *STANDARD-OUTPUT*))
      (PROGN
       (WRITE-STRING
	"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html><body>Some text"
	*STANDARD-OUTPUT*)
       (LIST (INTERN "FOO" :KEYWORD) '(:BODY "test"))
       (WRITE-STRING "Some more text</body></html>" *STANDARD-OUTPUT*)))
    T
    > 

See? The LIST form (or the equivalent backquoted form) results in
*only* a value -- the list (:FOO (:BODY "test")) -- which is then
immediately thrown away [since it's not the last such value].
It has no effect on the output:

    > (with-output-to-string (string)
	(cl-who:with-html-output (*standard-output* string :prologue t)
	  (:html
	    (:body
	      "Some text"
	      (list (intern "FOO" :keyword) '(:body "test"))
	      "Some more text"))))

    "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html><body>Some textSome more text</body></html>"
    > 

I again strongly suggest that you consider simply walking your
internal XML document tree yourself, emitting XML (or HTML) text
as needed during the walk. Something like this, but you'd need
to add all the specific details appropriate to your application:

    > (defun walk-and-emit (tree &key (stream *standard-output*))
	(cond
	  ((null tree)
	   nil)
	  ((and (consp tree) (consp (car tree)))
	   (error "This version can't handle attributes: ~s" tree))
	  ((consp tree)
	   (format stream "<~a>" (car tree))
	   (dolist (i (cdr tree))
	     (walk-and-emit i :stream stream))
	   (format stream "</~a>" (car tree)))
	  (t (format stream "~a" tree))))

    WALK-AND-EMIT
    > (with-output-to-string (string)
	(walk-and-emit '(document (type "Purchase Order")
				  (item
				    (description "Desk")
				    (quantity 1)
				    (unit_cost "$153.24"))
				  (item
				    (description "Pencil")
				    (quantity 125)
				    (unit_cost "$0.153")))
		       :stream string))

    "<DOCUMENT><TYPE>Purchase Order</TYPE><ITEM><DESCRIPTION>Desk</DESCRIPTION><QUANTITY>1</QUANTITY><UNIT_COST>$153.24</UNIT_COST></ITEM><ITEM><DESCRIPTION>Pencil</DESCRIPTION><QUANTITY>125</QUANTITY><UNIT_COST>$0.153</UNIT_COST></ITEM></DOCUMENT>"
    > 


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607