sxmlhtml.lsp 100777 0 0 10163 12252675616 6622 0
; Author: xytroxon 12/13/2013
; File sxmlhtml.lsp -> this file is used to load and insert code into test html file.
(context 'sxml)
;~ (setq sxml:xml-entity-codes '(("&" "&")("<" "<")(">" ">")("'" "'")("\"" """)))
(setq sxml:xml-entity-codes '(("&" "&") ("'" "'") ("\"" """) ("<" "<") (">" ">")))
; Document Type Definition (DTD).
(define (sxml:DTD+ str) ; encode & < > ' "
(dolist (entity xml-entity-codes)
(replace (entity 0) str (entity 1) 4)
)
)
(define (sxml:DTD- str) ;decode & < > ' "
(dolist (entity xml-entity-codes)
(replace (entity 1) str (entity 0) 4)
)
)
(define (sxml:define-element tag_str)
(dolist (tag (parse tag_str " "))
(eval-string
(string
"(define (" (string (context)) ":" tag ") "
"(string \"<" tag ">\" "
"(join (map string (args))) "
"\"" tag ">\"))"
;~ "\"" tag ">\\n\"))"
)
)
)
)
; add attributes
(define-macro (sxml:@=)
(setq str "")
(for (i 1 (length (args)) 2)
(push (string "\032" (args (- i 1)) "=\"" (string (args i)) "\"" ) str -1)
)
(string "\026" str ">") ; mark opening to add attriibutes
)
; comment
(define (sxml:!-- str) (string ""))
(define (sxml:url str uri) (string "" str ""))
(define (sxml:url+ str uri) (string "" str ""))
(define (sxml:?xml str) (string ""))
(define (sxml:xml-document)
(setq xml-string (string (join (args))))
(replace ">\026" xml-string "") ; Fix: @#! attributes> to
)
(context MAIN)
; create an html document
; (url -> shortcut fpr (a href=""
; (url+ -> open url in new browser window /tab
;
;(tag1 (@= attribute1 value1 attribute2 value2 ...)
; (tag2)
; (tag3 string-value 5)
; "some text"
; number
;)
(new sxml 'page1)
; define my xml or html tags first
(setq html5-tags "html head title link style body div span br hr h1 h2 h3 h4 h5 h6 a p ul ol dl li table th tr td b i code pre")
(page1:define-element html5-tags) ; dynamically defined tags
(context page1)
(xml-document
{} ; add "raw" html string
(html
(head
(title "Testing HTML")
)
(body (@= bgcolor "teal")
(h1 "Testing newLISP sxml module.")
(ul
(li "Item: " (inc num))
(li "Item: " (inc num))
(li "Item: " (inc num))
(li (b "Item: " (inc num)))
(li "Item: " (inc num))
(li (i "Item: " (inc num)))
)
(hr)
(!-- "This is a html comment.")
(table (@= border 1 bgcolor "white")
(th (@= colspan 3) "table of vslues...")
(tr (td 1)(td 2) (td 3))
(tr (td 4)(td (DTD+ "< 5 >")) (td 6.0001))
(tr (td 7)(td 8) (td 9))
)
(hr)
(p (@= id "txt1")
(upper-case "change all of these lower case letters to upper case.")
)
(p (@= id "txt2")
(DTD+ "Document Type Definition (DTD) XML chars \"Testing\" won't a < b & c >= 3")
)
(h3 "Test anchor tag...")
(p (@= id "txt3")
; anchor tag style
(a (@= href "http://www.newlisp.org") "newLISP")(br)
; url short
(url "newLISP" "http://www.newlisp.org")(br)
; url short that opens in new pagee
(url+ "newLISP" "http://www.newlisp.org")(br)
)
(h2 "List code file: sxmlhtml.lsp")
(p (@= id "txt3")
(b (pre (replace "\t" (read-file "sxmlhtml.lsp") (dup " " 4))))
)
)
)
)
(context MAIN)
(println page1:xml-string)
(write-file "test.htm" page1:xml-string)
(exit)
;~ entity: The output is converted using the XML/HTML escaping: < is converted to <, > to >, & to &, " to ", and ' to '.
;~ url: All non-alphanumerical characters in the output except -_.!~*'()/ are replaced with a percent sign % followed by two hexadecimal digits. This conversion conforms to the URL encoding scheme as described in RFC1738 and is useful to output well formed URLs.
;~ reverse-url: Sequences made of a percent sign % followed by two hexadecimal digits are decoded according to the URL encoding scheme.
;~ http://www.w3.org/TR/html5/entities.json
;~ http://www.w3.org/TR/html5/syntax.html#named-character-references