; bbcode.nlsp (inb4 "newlisp is teh gay")
; An attempt to minimize BBCode failures.
; example:
; (bbcode "This is normal text," (b (u (i "THIS IS ENTERPRISE TEXT."))))
; =>
; "This is normal text, [b][u][i]THIS IS ENTERPRISE TEXT.[/i][/u][/b]"
; bbcode.nlsp (inb4 "newlisp is teh gay")
; An attempt to minimize BBCode failures.
; example:
; (bbcode "This is normal text," (b (u (i "THIS IS ENTERPRISE TEXT."))))
; =>
; "This is normal text, THIS IS ENTERPRISE TEXT."
>>9
People who dislike dynamic scope are likely the same people who dislike non-garbage-collection; they're too lazy to keep track of their variables. Dynamic scope can actually be very powerful if used correctly. newLISP has contexts, too, if you really want to modularize things.
;; @module bbcode
;; @author Anonymous
;; @description An attempt to minimize BBCode failures.
;; @version 2008-04-15
;;
;; The idea behind this is similar to the idea of representing XML as
;; S-Expressions. BBCode is basically just a bastardized form of HTML, so it can
;; be easily transformed to and from an S-Expression.
;;
;; BBCode tags must be LIFO, and some (idiots) accidentally place their tags
;; out-of-order. This simple module helps ensure that tags are placed correctly
;; by taking an S-Expression of the desired BBCode, and outputing the BBCode.
(context 'bbcode)
;; @syntax (bbcode:to-bbcode <arg>)
;; @param <arg> A list or single argument to convert into a BBCode string.
;; @return A BBCode string.
;;
;; If <arg> is a list, then the first item of that list is taken to be a BBCode
;; tag, and the rest of the list is joined together with 'bbcode-join', which
;; calls 'to-bbcode' on each item.
;;
;; If <arg> is not a list, then the string form of it is simply returned.
(define (to-bbcode arg)
(if (and (list? arg)
(> (length arg) 0))
(string "[" (sym (first arg)) "]"
(bbcode-join (rest arg))
"[/" (sym (first arg)) "]")
(string arg)))
;; @syntax (bbcode:bbcode-join <lst>)
;; @param <lst> A list of items, possibly even other lists.
;; @return A space-separated string of the BBCode string values of the list.
;;
;; 'to-bbcode' is called on each item to get the BBCode representation of an
;; item.
(define (bbcode-join lst)
(join (map to-bbcode lst) " "))
;; @syntax (bbcode <args>)
;; @param <args> Any number of arguments.
;; @return A BBCode representation of an S-Expression.
;;
;; @example
;; (bbcode (b (u (i "ENTERPRISE TEXT."))))
;; =>
;; "[b][u][i]ENTERPRISE TEXT.[/i][/u][/b]"
(define-macro (bbcode:bbcode)
(bbcode-join (args)))