| 1 |
;;; |
| 2 |
;;; html-lite.scm - lightweight HTML construction |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module text.html-lite |
| 35 |
(use text.tree) |
| 36 |
(use scheme.list) |
| 37 |
(export html-escape html-escape-string html-doctype) |
| 38 |
) |
| 39 |
(select-module text.html-lite) |
| 40 |
|
| 41 |
;; Escaping --------------------------------------------- |
| 42 |
(define (html-escape) |
| 43 |
(generator-for-each (^c (case c |
| 44 |
[(#\<) (display "<")] |
| 45 |
[(#\>) (display ">")] |
| 46 |
[(#\&) (display "&")] |
| 47 |
[(#\") (display """)] |
| 48 |
[else (display c)])) |
| 49 |
read-char)) |
| 50 |
|
| 51 |
(define (html-escape-string string) |
| 52 |
(with-string-io (x->string string) html-escape)) |
| 53 |
|
| 54 |
;; Doctype ---------------------------------------------- |
| 55 |
|
| 56 |
;; Doctype database |
| 57 |
;; (type ...) => (xml? doctype) |
| 58 |
(define-constant *doctype-alist* |
| 59 |
'(((:strict :html :html-strict :html-4.01 :html-4.01-strict) |
| 60 |
#f |
| 61 |
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" |
| 62 |
\"http://www.w3.org/TR/html4/strict.dtd\">\n") |
| 63 |
((:transitional :html-transitional :html-4.01-transitional) |
| 64 |
#f |
| 65 |
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" |
| 66 |
\"http://www.w3.org/TR/html4/loose.dtd\">\n") |
| 67 |
((:frameset :html-frameset :html-4.01-frameset) |
| 68 |
#f |
| 69 |
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Frameset//EN\" |
| 70 |
\"http://www.w3.org/TR/html4/frameset.dtd\">\n") |
| 71 |
((:xhtml-1.0-strict :xhtml-1.0) |
| 72 |
#t |
| 73 |
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" |
| 74 |
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n") |
| 75 |
((:xhtml-1.0-transitional) |
| 76 |
#t |
| 77 |
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" |
| 78 |
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n") |
| 79 |
((:xhtml-1.0-frameset) |
| 80 |
#t |
| 81 |
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" |
| 82 |
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">\n") |
| 83 |
((:xhtml-1.1) |
| 84 |
#t |
| 85 |
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" |
| 86 |
\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n") |
| 87 |
((:html-5) |
| 88 |
#t |
| 89 |
"<!DOCTYPE html>") |
| 90 |
)) |
| 91 |
|
| 92 |
(define (html-doctype :key (type :html-4.01-strict)) |
| 93 |
(cond ((find (^e (memq type (car e))) *doctype-alist*) |
| 94 |
=> caddr ) |
| 95 |
(else (error "Unknown doctype type spec" type)))) |
| 96 |
|
| 97 |
;; Elements ------------------------------------------------ |
| 98 |
|
| 99 |
(define (make-html-element name . args) |
| 100 |
(let ((empty? (get-keyword :empty? args #f))) |
| 101 |
(define (K k) (keyword->string k)) ;; we don't need leading colon |
| 102 |
(define (get-attr args attrs) |
| 103 |
(cond ((null? args) (values (reverse attrs) args)) |
| 104 |
((keyword? (car args)) |
| 105 |
(cond ((null? (cdr args)) |
| 106 |
(values (reverse (list* (K (car args)) " " attrs)) args)) |
| 107 |
((eq? (cadr args) #f) |
| 108 |
(get-attr (cddr args) attrs)) |
| 109 |
((eq? (cadr args) #t) |
| 110 |
(get-attr (cddr args) (list* (K (car args)) " " attrs))) |
| 111 |
(else |
| 112 |
(get-attr (cddr args) |
| 113 |
(list* (format #f "=\"~a\"" |
| 114 |
(html-escape-string (x->string (cadr args)))) |
| 115 |
(K (car args)) |
| 116 |
" " |
| 117 |
attrs))))) |
| 118 |
(else (values (reverse attrs) args)))) |
| 119 |
|
| 120 |
(if empty? |
| 121 |
(lambda args |
| 122 |
(receive (attr args) (get-attr args '()) |
| 123 |
(unless (null? args) |
| 124 |
(errorf "element ~s can't have content: ~s" name args)) |
| 125 |
(list "<" name attr " />"))) |
| 126 |
(lambda args |
| 127 |
(receive (attr args) (get-attr args '()) |
| 128 |
(list "<" name attr ">" args "</" name "\n>")))))) |
| 129 |
|
| 130 |
(define-macro (define-html-elements . elements) |
| 131 |
(define (make-scheme-name name) |
| 132 |
(string->symbol (format #f "html:~a" name))) |
| 133 |
(let loop ((elements elements) |
| 134 |
(r '())) |
| 135 |
(cond ((null? elements) `(begin ,@(reverse r))) |
| 136 |
((and (pair? (cdr elements)) (eqv? (cadr elements) :empty)) |
| 137 |
(loop (cddr elements) |
| 138 |
(list* `(define ,(make-scheme-name (car elements)) |
| 139 |
(make-html-element ',(car elements) :empty? #t)) |
| 140 |
`(export ,(make-scheme-name (car elements))) |
| 141 |
r))) |
| 142 |
(else |
| 143 |
(loop (cdr elements) |
| 144 |
(list* `(define ,(make-scheme-name (car elements)) |
| 145 |
(make-html-element ',(car elements))) |
| 146 |
`(export ,(make-scheme-name (car elements))) |
| 147 |
r)))) |
| 148 |
)) |
| 149 |
|
| 150 |
;; http://www.w3.org/TR/html4/sgml/dtd.html |
| 151 |
|
| 152 |
;; TEXT MARKUP |
| 153 |
|
| 154 |
;; %fontstyle |
| 155 |
(define-html-elements tt i b big small) |
| 156 |
|
| 157 |
;; %phrase |
| 158 |
(define-html-elements em strong dfn code samp kbd var cite abbr acronym) |
| 159 |
|
| 160 |
(define-html-elements sub sup span bdo br :empty) |
| 161 |
|
| 162 |
;; HTML CONTENT MODELS |
| 163 |
|
| 164 |
;; DOCUMENT BODY |
| 165 |
|
| 166 |
(define-html-elements body address div) |
| 167 |
|
| 168 |
;; THE ANCHOR ELEMENT |
| 169 |
(define-html-elements a) |
| 170 |
|
| 171 |
;; CLIENT-SIDE IMAGE MAPS |
| 172 |
(define-html-elements map area :empty) |
| 173 |
|
| 174 |
;; THE LINK EKEMENT |
| 175 |
(define-html-elements link :empty) |
| 176 |
|
| 177 |
;; IMAGES |
| 178 |
(define-html-elements img :empty) |
| 179 |
|
| 180 |
;; OBJECT |
| 181 |
(define-html-elements object param :empty) |
| 182 |
|
| 183 |
;; HORIZONTAL RULE |
| 184 |
(define-html-elements hr :empty) |
| 185 |
|
| 186 |
;; PARAGRAPHS |
| 187 |
(define-html-elements p) |
| 188 |
|
| 189 |
;; HEADINGS |
| 190 |
(define-html-elements h1 h2 h3 h4 h5 h6) |
| 191 |
|
| 192 |
;; PREFORMATTED |
| 193 |
(define-html-elements pre) |
| 194 |
|
| 195 |
;; INLINE QUOTES |
| 196 |
(define-html-elements q) |
| 197 |
|
| 198 |
;; BLOCK-LIKE QUOTES |
| 199 |
(define-html-elements blockquote) |
| 200 |
|
| 201 |
;; INSERTED/DELETED TEXT |
| 202 |
(define-html-elements ins del) |
| 203 |
|
| 204 |
;; LISTS |
| 205 |
(define-html-elements dl dt dd ol ul li) |
| 206 |
|
| 207 |
;; FORMS |
| 208 |
(define-html-elements form label input :empty select optgroup option |
| 209 |
textarea fieldset legend button) |
| 210 |
|
| 211 |
;; TABLES |
| 212 |
(define-html-elements table caption thead tfoot tbody colgroup |
| 213 |
col :empty tr th td) |
| 214 |
|
| 215 |
;; DOCUMENT HEAD |
| 216 |
(define-html-elements head title base :empty meta :empty |
| 217 |
style script noscript) |
| 218 |
|
| 219 |
;; DOCUMENT STRUCTURE |
| 220 |
(define-html-elements html) |
| 221 |
|
| 222 |
;; FRAMES |
| 223 |
(define-html-elements frameset frame noframes iframe) |
| 224 |
|
| 225 |
;; Elements available since HTML5, in the order they are defined in |
| 226 |
;; Section "4. The elements of HTML" of <https://www.w3.org/TR/html5/>. |
| 227 |
(define-html-elements article section nav aside |
| 228 |
header footer |
| 229 |
figure figcaption |
| 230 |
main |
| 231 |
ruby rb rt rtc rp |
| 232 |
data time |
| 233 |
mark bdi bdo |
| 234 |
wbr :empty |
| 235 |
picture source |
| 236 |
embed :empty |
| 237 |
video audio track :empty |
| 238 |
datalist |
| 239 |
output |
| 240 |
progress |
| 241 |
meter |
| 242 |
details summary dialog |
| 243 |
template |
| 244 |
canvas) |