tags are separated from plaintext.
There is an implementation featured here:
http://newlispfanclub.alh.net/forum/vie ... =16&t=3386
But it doesn't handle javascript code.
The function works for me. I expect, that once I put it to work, it
will need some tweaking. However, I have thus far used newlisp only intermittenly and I would deeply
appreciate it if some of you newlisp veterans would review this code and suggest optimizations. I have
based this on a function that I wrote for python (rebol has this feature builtin), but I would like suggestions as
to how to make the code more "newlispish". Such suggestions would certainly contribute to my overall
grasp of newlisp.
And also, I want to wish you all the best for this holiday season and for the New Year.
code follows:
Code: Select all
;; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;; @syntax [-parse-markup <str>-]
;; @Description Parse 'str' into alternating plain text and markup elements
;; @Returns a list. Adjacent tags are seperate elements.
;; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
(define (parse-markup str)
(let ((res '())(buf "")(inTag)(inScript)(chr)(nxt)(ndx -1)(endp (- (length str) 1))
;; "Private" functions
(data (fn()(not(empty? buf)))) ;; test for data in temporary buffer
(add2buf (fn()(set 'buf(append buf chr)))) ;; append char to buffer
(sflag (fn()(set 'inScript (if (find "javascript" (lower-case buf))))))
(add2res
(fn(c) ;; Add buffer and re-initialize. Set 'inScript flag
(sflag) ;; set/unset 'inScript
(push buf res) ;; Add buffer to results
(set 'buf "") ;; Reinitialize the buffer
(if c (add2buf))))) ;; End 'let initialization form
(dostring (c str) ;; scan string char-by-char
(inc ndx) ;; position of char
(set 'chr (char c)) ;; one-char string
(if (< ndx endp) ;; keep track of next char to process
(set 'nxt (str (+ ndx 1))))
(cond
((= chr "<") ;; Begin a tag insertion if not javascript
(cond
(inScript ;; Still processing javascript code
(cond
((= nxt "/") ;; Finishing javascript code block.
(set 'inTag true
'inScript nil) ;; set boolean flags
(cond
((data) ;; if buffer has data, push and clear
(add2res chr)) ;; Add buffer to results and re-initialize with char
(true (add2buf)))) ;; add char to empty buffer
(true (add2buf)))) ;; Keep filling 'buf
(true ;; Not in javascript code block. Starting new tag
(set 'inTag true)
(cond
((data) ;; If 'buf has data.
(add2res chr)) ;; push and re-initialize with char
(true (add2buf)))))) ;; Buffer is empty, keep filling 'buf
((and (= chr ">") (not inScript)) ;; finishing a tag.
(set 'inTag nil)
(sflag) ;; set flags
(add2buf) ;; add char to buffer
(add2res)) ;; Push buffer and reinitialize
(true ;; still in script block
(add2buf)))) ;; just add to 'buf, end dostring/outermost cond
(if (data) ;; If data in 'buf, add to result
(add2res))
(reverse res)))