;;;; 6.891 simple-xhtml.scm

(define ((wrap-content key) . content)
  (flatten-append "\n"
		  "<" key ">"
		  content
		  "</" key ">"))


(define (flatten-append . pieces)
  (apply string-append
	 (map (lambda (piece)
		(cond ((string? piece) piece)
		      ((number? piece) (number->string piece))
		      ((list? piece) (apply flatten-append piece))
		      (else
		       (error "Unknown piece type: flatten-append"
			      piece))))
	      pieces)))

(define x-body (wrap-content "body"))

(define x-paragraph (wrap-content "p"))


(define ((empty-element key))
  (string-append "\n" "<" key  "/>"))

(define x-break (empty-element "br"))

(define x-horizontal-rule (empty-element "hr"))

#|
;;; For example

(display
 (x-body "Now is the time"
	 (x-paragraph "for all good men")
	 (x-paragraph "to come" (x-break) " to the aid")
	 (x-paragraph "of their country.")))

<body>Now is the time
<p>for all good men</p>
<p>to come
<br/> to the aid</p>
<p>of their country.</p></body>


(display
 (x-body "Factorials"
	 (map (lambda (n)
		(x-paragraph n "! = " (factorial n)))
	      (iota 10))))

<body>Factorials
<p>0! = 1</p>
<p>1! = 1</p>
<p>2! = 2</p>
<p>3! = 6</p>
<p>4! = 24</p>
<p>5! = 120</p>
<p>6! = 720</p>
<p>7! = 5040</p>
<p>8! = 40320</p>
<p>9! = 362880</p></body>
|#

#|
;;; By the way, iota and factorial are defined as follows in the
;;; scmutils system.

(define (factorial n)
  (if (= n 0)
      1
      (* n (factorial (- n 1)))))

(define (iota to #!optional from increment)
  (define (step where)
    (if (>= where to)
	'()
	(cons where (step (+ where increment)))))
  (if (default-object? from) (set! from 0))
  (if (default-object? increment) (set! increment 1))
  (step from))
|#
