;; @module Element ;; @author Jeff Ober , Kanen Flowers ;; @version 1.0 ;; @location http://static.artfulcode.net/newlisp/element.lsp ;; @package http://static.artfulcode.net/newlisp/element.qwerty ;; @description A simple way of generating valid XML content (requires newlisp 10). ;; Often, XML modules attempt to provide everything but the kitchen sink when ;; all that you want is to write valid XML simply and quickly. Element ;; is a purely practical module that lets you write XML fast and efficiently. It ;; performs no validation and does nothing apart from serialize data to XML. If ;; you need extra functionality like indentation or serialization of SXML, look ;; at the XML module. ;;

Version history

;; 1.0 ;; • initial release ;;

Bugs and todos

;; • XML declaration function with encoding (context 'Element) (constant 'xml-entity-encode-re (regex-comp (string "(" (join (map (fn (i) (format {\x%x} i)) '(34 38 39 60 62)) "|") ")"))) ;; @syntax (Element:encode ) ;; @param a string ;;

Encodes characters in a string to be valid for XML.

(define (encode str) (replace xml-entity-encode-re (string str) (string "&#" (char $1) ";") 0x10000)) (define (serialize-attributes attributes) (cond ((list? attributes) (join (map (fn (pair) (format " %s=\"%s\"" (map encode pair))) attributes) "")) ((string? attributes) (string " " attributes)) (true ""))) (define (opening-tag tag attributes) (string "<" tag (serialize-attributes attributes) ">")) (define (closing-tag tag) (string "")) (define (empty-tag tag attributes) (string "<" tag (serialize-attributes attributes) " />")) ;; @syntax (Element:Element [ ...]) ;; @param the tag name ;; @param an association list of key/value pairs, a string, or nil ;; @param any number of strings ;;

Builds an element tag with the name and attributes ;; around the content , which generally is also built using this ;; function. If is an association list, it is converted to a series ;; of key="value" pairs. If it is a string, it is inserted as-is. Otherwise, it ;; is ignored. may be strings generated by XmlBuilder:element or ;; otherwise (in which case it is inserted as text content). Attribute values are ;; encoded as needed (if an association list); must be encoded if ;; a text value.

;; @example ;; (Element "div" '(("class" "content")) ;; (Element "h2" nil (Element:encode "Welcome")) ;; (Element "p" {class="message"} (Element:encode "Hello world."))) ;; ;; =>

Welcome

Hello world.

(define (Element:Element tag attributes) (if (empty? (args)) (empty-tag tag attributes) (string (opening-tag tag attributes) (join (map (fn (a) (if (list? a) (join (map string a)) (string a))) (args))) (closing-tag tag)))) ;; @syntax (Element:doc [ ...]) ;; @param encoding for XML declaration ;; @param any number of strings ;;

Adds an XML declaration to a string. If encoding is nil, defaults to UTF-8 ;; when newlisp is compiled with UTF-8 support, ASCII otherwise.

;; @example ;; (Element:doc "UTF-8" (Element "root" nil "Hello world.")) ;; => ;; ;; Hello world. (define (doc encoding) (unless encoding (setq encoding (if utf8 "UTF-8" "ASCII"))) (string {} "\n" (join (map string (args))))) (context 'MAIN)