3 Final Comments

This approach is not new: it turns out that McMicMac [2], [1] in (older versions of) PLT Scheme used this same method to pass parameters between its "micros". This method of passing parameters can be used as well with the lower-level Expansion-Passing Style "expanders" in WebIt!. (After all, micros are just syntactic sugar on top of EPS macros.) And this method could be used to good effect with pre-order traversals in Oleg's pre-post-order XML transformations, since micros and pre-order traversals are really the "same thing"1.

The full code for this example is given below. This code requires the new WebIt! version 0.7.

; section formatting
(require (lib "xml-strict.ss" "webit") (lib "html.ss" "webit"))
(define-element section)
(define-attribute title)
(define-attribute num)
(define-element p)
(define sect-ss
  (stylesheet
    (define (next-level hl) (if (null? (cdr hl)) hl (cdr hl)))
    (xml-micro *text* (lambda (n) (lambda (prefix hl) n)))
    (xml-micro
      *element*
      (lambda (n)
        (lambda (prefix hl)
          ((xml-type n)
           (xml-element-attributes n)
           (map
            (lambda (i) ((xml-expand i) prefix hl))
            (xml-element-contents n))))))
    (xml-micro
      section
      (xml-rules
        ((_ num: n title: t c ...)
         (lambda (prefix hl)
           (h4:div
             (xml-template ((car hl) prefix n ". " t))
             (h4:p)
             (map
              (lambda (i)
                ((xml-expand i)
                 (string-append prefix (xml-template n) ".")
                 (next-level hl)))
              (xml-template (list c ...))))))))
    (xml-micro
      p
      (xml-rules
        ((_) (lambda (prefix hl) (h4:p)))
        ((_ c ...) (lambda (prefix hl) (xml-template (h4:p c ...))))))))
(define ex
  (h4:html
    (h4:body
      (section
        num:
        "1"
        title:
        "First Section"
        (p "This is the intro section."))
      (section
        num:
        "2"
        title:
        "Second Section"
        (section num: "1" title: "A sub-section" (p "This is section 2.1."))
        (section
          num:
          "2"
          title:
          "Another sub-section"
          (p "This is section 2.2.")))
      (section
        num:
        "3"
        title:
        "Last major section"
        (p "This is the third section")))))
(define trans (stylesheet->expander sect-ss))
(write-xml ((trans ex) "" (list h4:h1 h4:h2 h4:h3 h4:h4 h4:h5)))

1 In a sense, I wouldn't be surprised to see something similar already in Kirill Lisovsky's SXT (even if only "behind the scenes").

Last modified: Sunday, February 27th, 2005 4:37:51pm
HTML generated using WebIt!.