Advertisement
Guest User

Untitled

a guest
Dec 10th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns bb-clojure.core)
  2.  
  3. (defn proto-get [obj key]
  4.   (cond
  5.     (contains? obj key) (obj key)
  6.     (contains? obj :prototype) (proto-get (obj :prototype) key)))
  7.  
  8. (defn proto-call [obj f & args] (apply (proto-get obj f) obj args))
  9.  
  10. (defn field [x] (fn [obj] (proto-get obj x)))
  11. (defn method [x] (fn [obj & args] (apply proto-call obj x args)))
  12.  
  13. (def toHtml (method :toHtml))
  14.  
  15. (def Node)
  16.  
  17. (def TextPrototype
  18.   (let [text (field :text)]
  19.     {:toHtml text
  20.      :prototype Node}))
  21.  
  22. (defn Text [text]
  23.   {:text text
  24.    :prototype TextPrototype})
  25.  
  26. (def Tag
  27.   (let [start (method :start) end (method :end) children (field :children)]
  28.     {:toHtml (fn [obj]
  29.                (str
  30.                  (start obj)
  31.                  (clojure.string/join "" (map toHtml (children obj)))
  32.                  (end obj)))
  33.      :prototype Node}))
  34.  
  35. (defn make-tag [name]
  36.   (fn [& args]
  37.     {:start (constantly (str "<" name ">"))
  38.      :end (constantly (str "</" name ">"))
  39.      :children args
  40.      :prototype Tag}))
  41.  
  42. (def Strong (make-tag "b"))
  43. (def Em (make-tag "i"))
  44. (def Span (make-tag "u"))
  45. (def Del (make-tag "s"))
  46. (def Code (make-tag "code"))
  47. (def Code (make-tag "code"))
  48.  
  49. (def ConcatNode
  50.   (fn [& args]
  51.     {:start (constantly "")
  52.      :end (constantly "")
  53.      :children args
  54.      :prototype Tag}))
  55.  
  56. (definterface WriterI
  57.   (write []))
  58.  
  59. (deftype HtmlWriter
  60.   [node filename]
  61.   WriterI
  62.   (write [this] (spit filename (toHtml node))))
  63.  
  64. (def hello (Text "hello"))
  65. (def world (Text "worldd"))
  66. (def underline (Em hello))
  67. (def underline_bold (Strong underline))
  68. (def span (Span world))
  69. (def span_code (Code span))
  70. (def result (ConcatNode span_code underline_bold))
  71. (.write (HtmlWriter. result "result.html"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement