Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns bb-clojure.core)
- (defn proto-get [obj key]
- (cond
- (contains? obj key) (obj key)
- (contains? obj :prototype) (proto-get (obj :prototype) key)))
- (defn proto-call [obj f & args] (apply (proto-get obj f) obj args))
- (defn field [x] (fn [obj] (proto-get obj x)))
- (defn method [x] (fn [obj & args] (apply proto-call obj x args)))
- (def toHtml (method :toHtml))
- (def Node)
- (def TextPrototype
- (let [text (field :text)]
- {:toHtml text
- :prototype Node}))
- (defn Text [text]
- {:text text
- :prototype TextPrototype})
- (def Tag
- (let [start (method :start) end (method :end) children (field :children)]
- {:toHtml (fn [obj]
- (str
- (start obj)
- (clojure.string/join "" (map toHtml (children obj)))
- (end obj)))
- :prototype Node}))
- (defn make-tag [name]
- (fn [& args]
- {:start (constantly (str "<" name ">"))
- :end (constantly (str "</" name ">"))
- :children args
- :prototype Tag}))
- (def Strong (make-tag "b"))
- (def Em (make-tag "i"))
- (def Span (make-tag "u"))
- (def Del (make-tag "s"))
- (def Code (make-tag "code"))
- (def Code (make-tag "code"))
- (def ConcatNode
- (fn [& args]
- {:start (constantly "")
- :end (constantly "")
- :children args
- :prototype Tag}))
- (definterface WriterI
- (write []))
- (deftype HtmlWriter
- [node filename]
- WriterI
- (write [this] (spit filename (toHtml node))))
- (def hello (Text "hello"))
- (def world (Text "worldd"))
- (def underline (Em hello))
- (def underline_bold (Strong underline))
- (def span (Span world))
- (def span_code (Code span))
- (def result (ConcatNode span_code underline_bold))
- (.write (HtmlWriter. result "result.html"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement