| 1 |
; HTML Authoring in SXML |
| 2 |
; |
| 3 |
; The present file defines and demonstrates a function SXML->HTML, the |
| 4 |
; most generic transformation of SXML into the corresponding HTML |
| 5 |
; document. The SXML tree is traversed post-order and converted into |
| 6 |
; another tree, which, written in a depth-first fashion, results in a |
| 7 |
; HTML document. The function SXML->HTML can generate an arbitrary |
| 8 |
; HTML markup, for any existing or yet to be introduced HTML |
| 9 |
; tag. Furthermore, the function supports one higher-level tag, |
| 10 |
; 'html:begin'. As the source code below indicates, SXML->HTML can be |
| 11 |
; trivially extended to support other higher-level tags. |
| 12 |
; |
| 13 |
; The proper HTML markup is being created by a set of node |
| 14 |
; handlers. An iterator 'post-order' executes these functions while it |
| 15 |
; traverses an SXML tree. |
| 16 |
; |
| 17 |
; Each node handler takes a tag (the head of an SXML node) and the |
| 18 |
; list of children nodes, if any. A handler returns a fragment or a |
| 19 |
; list of HTML fragments -- which become arguments to a handler of a |
| 20 |
; parent SXML node. A function SRV:send-reply takes the resulting |
| 21 |
; tree of fragments and writes out the fragments in a depth-first |
| 22 |
; order. The output is an HTML document that corresponds to the |
| 23 |
; original SXML tree. |
| 24 |
; |
| 25 |
; This pretty-printing operation makes it possible to author and |
| 26 |
; compose HTML documents in their SXML form. SXML is more concise and |
| 27 |
; expressive than a raw markup language. SXML representing regular |
| 28 |
; Scheme code can be entered in any Scheme-sensitive editor. SXML as a |
| 29 |
; data structure -- a list -- can likewise be composed as a literal or |
| 30 |
; quasi-literal expression. Furthermore, SXML can be produced by regular |
| 31 |
; Scheme functions, which may make authoring more succinct, advanced, |
| 32 |
; and less tedious, as the code below illustrates. |
| 33 |
; |
| 34 |
; IMPORT |
| 35 |
; A prelude appropriate for your Scheme system |
| 36 |
; (myenv-bigloo.scm, myenv-mit.scm, etc.) |
| 37 |
; util.scm for make-char-quotator |
| 38 |
; SXML-tree-trans.scm for post-order |
| 39 |
; |
| 40 |
; EXPORT |
| 41 |
; SXML->HTML enattr entag string->goodHTML |
| 42 |
; |
| 43 |
; All these files are available in the same directory as this file. |
| 44 |
; See vSXML-to-HTML.scm for the validation code, which also |
| 45 |
; serves as usage examples. |
| 46 |
; |
| 47 |
; See http://pobox.com/~oleg/ftp/Scheme/xml.html#XML-authoring |
| 48 |
; for more examples and explanation. |
| 49 |
; |
| 50 |
; $Id: SXML-to-HTML.scm,v 1.2 2003-07-21 12:19:43 shirok Exp $ |
| 51 |
|
| 52 |
|
| 53 |
; The following procedure is the most generic transformation of SXML |
| 54 |
; into the corresponding HTML document. The SXML tree is traversed |
| 55 |
; post-oder (depth-first) and transformed into another tree, which, |
| 56 |
; written in a depth-first fashion, results in an HTML document. |
| 57 |
|
| 58 |
(define (SXML->HTML tree) |
| 59 |
(SRV:send-reply |
| 60 |
(pre-post-order tree |
| 61 |
; Universal transformation rules. Work for every HTML, |
| 62 |
; present and future |
| 63 |
`((@ |
| 64 |
((*default* ; local override for attributes |
| 65 |
. ,(lambda (attr-key . value) (enattr attr-key value)))) |
| 66 |
. ,(lambda (trigger . value) (cons '@ value))) |
| 67 |
(*default* . ,(lambda (tag . elems) (entag tag elems))) |
| 68 |
(*text* . ,(lambda (trigger str) |
| 69 |
(if (string? str) (string->goodHTML str) str))) |
| 70 |
|
| 71 |
; Handle a nontraditional but convenient top-level element: |
| 72 |
; (html:begin title <html-body>) element |
| 73 |
(html:begin . ,(lambda (tag title . elems) |
| 74 |
(list "Content-type: text/html" ; HTTP headers |
| 75 |
nl nl ; two nl end the headers |
| 76 |
"<HTML><HEAD><TITLE>" title "</TITLE></HEAD>" |
| 77 |
elems |
| 78 |
"</HTML>")))) |
| 79 |
|
| 80 |
))) |
| 81 |
|
| 82 |
; The following two functions create the HTML markup for tags and attributes. |
| 83 |
; They are being used in the node handlers for the post-order function, see |
| 84 |
; above. |
| 85 |
|
| 86 |
(define (entag tag elems) |
| 87 |
(if (and (pair? elems) (pair? (car elems)) (eq? '@ (caar elems))) |
| 88 |
(list #\newline #\< tag (cdar elems) #\> |
| 89 |
(and (pair? (cdr elems)) |
| 90 |
(list (cdr elems) "</" tag #\>))) |
| 91 |
(list #\newline #\< tag #\> (and (pair? elems) (list elems "</" tag #\>)) |
| 92 |
))) |
| 93 |
|
| 94 |
(define (enattr attr-key value) |
| 95 |
(if (null? value) (list #\space attr-key) |
| 96 |
(list #\space attr-key "=\"" value #\"))) |
| 97 |
|
| 98 |
|
| 99 |
; Given a string, check to make sure it does not contain characters |
| 100 |
; such as '<' or '&' that require encoding. Return either the original |
| 101 |
; string, or a list of string fragments with special characters |
| 102 |
; replaced by appropriate character entities. |
| 103 |
|
| 104 |
(define string->goodHTML |
| 105 |
(make-char-quotator |
| 106 |
'((#\< . "<") (#\> . ">") (#\& . "&") (#\" . """)))) |