| 1 |
;;; |
| 2 |
;;; sxml.tools - utility to manipulate SXML |
| 3 |
;;; |
| 4 |
;;; This file is mechanically translated for Gauche from |
| 5 |
;;; Kirill Lisovsky's sxml-tools.scm CVS revision 3.13 |
| 6 |
;;; Public domain. |
| 7 |
;;; |
| 8 |
|
| 9 |
(define-module sxml.tools |
| 10 |
(use scheme.list) |
| 11 |
(use srfi.13) |
| 12 |
(use text.parse) |
| 13 |
(use sxml.adaptor) |
| 14 |
(export sxml:attr-list-node |
| 15 |
sxml:attr-as-list |
| 16 |
sxml:aux-list-node |
| 17 |
sxml:aux-as-list |
| 18 |
sxml:find-name-separator |
| 19 |
sxml:error |
| 20 |
sxml:empty-element? |
| 21 |
sxml:shallow-normalized? |
| 22 |
sxml:normalized? |
| 23 |
sxml:shallow-minimized? |
| 24 |
sxml:minimized? |
| 25 |
sxml:name |
| 26 |
sxml:element-name |
| 27 |
sxml:node-name |
| 28 |
sxml:ncname |
| 29 |
sxml:name->ns-id |
| 30 |
sxml:content |
| 31 |
sxml:content-raw |
| 32 |
sxml:attr-list-u |
| 33 |
sxml:aux-list |
| 34 |
sxml:aux-list-u |
| 35 |
sxml:aux-node |
| 36 |
sxml:aux-nodes |
| 37 |
sxml:attr |
| 38 |
sxml:num-attr |
| 39 |
sxml:attr-u |
| 40 |
sxml:ns-list |
| 41 |
sxml:ns-id->nodes |
| 42 |
sxml:ns-id->uri |
| 43 |
sxml:ns-uri->nodes |
| 44 |
sxml:ns-uri->id |
| 45 |
sxml:ns-id |
| 46 |
sxml:ns-uri |
| 47 |
sxml:ns-prefix |
| 48 |
sxml:change-content! |
| 49 |
sxml:change-content |
| 50 |
sxml:change-attrlist |
| 51 |
sxml:change-attrlist! |
| 52 |
sxml:change-name! |
| 53 |
sxml:change-name |
| 54 |
sxml:add-attr |
| 55 |
sxml:add-attr! |
| 56 |
sxml:change-attr |
| 57 |
sxml:change-attr! |
| 58 |
sxml:set-attr |
| 59 |
sxml:set-attr! |
| 60 |
sxml:add-aux |
| 61 |
sxml:add-aux! |
| 62 |
sxml:squeeze! |
| 63 |
sxml:squeeze |
| 64 |
sxml:clean |
| 65 |
select-first-kid |
| 66 |
sxml:node-parent |
| 67 |
sxml:add-parents |
| 68 |
sxml:lookup |
| 69 |
sxml:clean-feed |
| 70 |
sxml:attr->xml |
| 71 |
sxml:sxml->xml |
| 72 |
sxml:attr->html |
| 73 |
sxml:string->html |
| 74 |
sxml:string->xml |
| 75 |
sxml:non-terminated-html-tag? |
| 76 |
sxml:sxml->html |
| 77 |
)) |
| 78 |
(select-module sxml.tools) |
| 79 |
|
| 80 |
;; Override these by Gauche-specific versions. |
| 81 |
|
| 82 |
;; We can use srfi-13 fn |
| 83 |
(define-macro (sxml:find-name-separator len) |
| 84 |
`(string-index-right name #\:)) |
| 85 |
|
| 86 |
;; Error function. |
| 87 |
(define (sxml:error . messages) |
| 88 |
(apply error "SXML Error: " messages)) |
| 89 |
|
| 90 |
;; nl is used in the arg of sxml:error to insert a newline, but |
| 91 |
;; we don't really need it. |
| 92 |
(define nl "") |
| 93 |
|
| 94 |
;; These are defined in sxml.sxpath. Since sxml.sxpath depends on |
| 95 |
;; sxml.tools, we can't simply 'use' it. |
| 96 |
|
| 97 |
(autoload sxml.sxpath |
| 98 |
sxml:attr-list nodeset? ntype-names?? ntype?? |
| 99 |
sxml:filter select-kids sxml:element? sxml:element?) |
| 100 |
|
| 101 |
;#include-body "src/sxml-tools.scm" (sxml:attr->xml sxml:attr->html sxml:sxml->xml sxml:sxml->html) |
| 102 |
|
| 103 |
;; Override these two to emit attribute value properly escaped. |
| 104 |
;; NB: For HTML, we don't need to recognize XML namespace prefix, so |
| 105 |
;; we simply do symbol->string on the attribute name. |
| 106 |
;; (Cf. https://github.com/shirok/Gauche-makiki/issues/11) |
| 107 |
(define (sxml:attr->xml attr :optional (namespace-prefix-alist '())) |
| 108 |
(let1 name (name-assign-prefix (car attr) namespace-prefix-alist) |
| 109 |
`(" " ,name "=\"" ,(sxml:string->xml (x->string (cadr attr))) "\""))) |
| 110 |
(define (sxml:attr->html attr) |
| 111 |
(if (equal? "" (cadr attr)) |
| 112 |
`(" " ,(symbol->string (car attr))) |
| 113 |
`(" " ,(symbol->string (car attr)) "=\"" |
| 114 |
,(sxml:string->html (x->string (cadr attr))) "\""))) |
| 115 |
|
| 116 |
;; The original sxml:sxml->xml doesn't consider namespaces much. |
| 117 |
;; It keeps namespace prefix in the node name, but drops it from the |
| 118 |
;; attribute name. |
| 119 |
;; We override it to keep prefix in namespace attributes as well. |
| 120 |
;; Furthermore, the optional namespace-prefix-assig to reassign |
| 121 |
;; the prefix. namespace-prefix-assig is the same as ssax:xml->sxml, |
| 122 |
;; e.g. ((PREFIX . URI-STRING) ...) |
| 123 |
(define (sxml:sxml->xml sxml :optional (namespace-prefix-alist '())) |
| 124 |
(define (rec tree) |
| 125 |
(cond |
| 126 |
[(nodeset? tree) (map rec tree)] |
| 127 |
[(pair? tree) |
| 128 |
(let* ([name (sxml:name tree)] |
| 129 |
[sname (name-assign-prefix name namespace-prefix-alist)] |
| 130 |
[content (sxml:content-raw tree)] |
| 131 |
[attrs (if (eq? sxml tree) |
| 132 |
`(,@(map ns->attr namespace-prefix-alist) |
| 133 |
,@(sxml:attr-list tree)) |
| 134 |
(sxml:attr-list tree))]) |
| 135 |
`("<" ,sname ,@(map (cut sxml:attr->xml <> namespace-prefix-alist) attrs) |
| 136 |
,@(if (null? content) |
| 137 |
'("/>") |
| 138 |
`(">" ,@(rec content) "</" ,sname ">"))))] |
| 139 |
[(string? tree) (sxml:string->xml tree)] ; *text* |
| 140 |
[else (sxml:error "sxml->html - unexpected type of node: " tree)])) |
| 141 |
(define (ns->attr prefix&uri) |
| 142 |
`(,(symbol-append 'xmlns: (car prefix&uri)) ,(cdr prefix&uri))) |
| 143 |
(rec sxml)) |
| 144 |
|
| 145 |
;; returns string |
| 146 |
(define (name-assign-prefix name prefix-alist) |
| 147 |
(let* ([sname (symbol->string name)] |
| 148 |
[sep-index (string-index-right sname #\:)]) |
| 149 |
(or (and-let* ([ sep-index ] |
| 150 |
[prefix-uri (string-copy sname 0 sep-index)] |
| 151 |
[alias (alist-key prefix-alist prefix-uri)]) |
| 152 |
#"~|alias|~(string-copy sname sep-index)") |
| 153 |
sname))) |
| 154 |
|
| 155 |
;; We also need to override sxml:sxml->html. The attempt to merge XML |
| 156 |
;; and HTML is dead. HTML is not XML. |
| 157 |
(define (sxml:sxml->html tree) |
| 158 |
(cond |
| 159 |
[(nodeset? tree) (map sxml:sxml->html tree)] |
| 160 |
[(pair? tree) |
| 161 |
(let* ([name (sxml:name tree)] |
| 162 |
[nm (symbol->string name)] |
| 163 |
[content (sxml:content-raw tree)] |
| 164 |
[rendered-attrs (map sxml:attr->html (sxml:attr-list tree))]) |
| 165 |
(if (and (null? content) |
| 166 |
(sxml:non-terminated-html-tag? name)) |
| 167 |
`("<" ,nm ,@rendered-attrs ">") |
| 168 |
`("<" ,nm ,@rendered-attrs ">" |
| 169 |
,@(sxml:sxml->html content) "</" ,nm ">")))] |
| 170 |
[(string? tree) (sxml:string->html tree)] ; *text* |
| 171 |
[else (sxml:error "sxml->html - unexpected type of node: " tree)])) |
| 172 |
|
| 173 |
;; Local variables: |
| 174 |
;; mode: scheme |
| 175 |
;; end: |