Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clojerl support #185

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/codox
/classes
/checkouts
/ebin
pom.xml
pom.xml.asc
*.jar
Expand Down
13 changes: 13 additions & 0 deletions src/hiccup.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ application
, hiccup,
[ {description, "A fast library for rendering HTML in Clojure"}
, {vsn, git}
, {registered, []}
, {applications, [kernel, stdlib]}
, {env,[]}
, {modules, []}
, {maintainers, []}
, {licenses, []}
, {links, []}
]
}.
82 changes: 57 additions & 25 deletions src/hiccup/compiler.clj → src/hiccup/compiler.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
"Internal functions for compilation."
(:require [hiccup.util :as util]
[clojure.string :as str])
(:import [clojure.lang IPersistentVector ISeq Named]
[hiccup.util RawString]))
#?(:clj
(:import [clojure.lang IPersistentVector ISeq Named]
[hiccup.util RawString])
:clje
(:import [clojerl Vector List INamed String Integer Float]
[hiccup.util RawString])))

(defn- xml-mode? []
(#{:xml :xhtml} util/*html-mode*))
Expand Down Expand Up @@ -99,7 +103,8 @@
(defn- normalize-element*
[[tag & content] merge-attributes-fn]
(when (not (or (keyword? tag) (symbol? tag) (string? tag)))
(throw (IllegalArgumentException. (str tag " is not a valid element name."))))
(throw #?(:clj (IllegalArgumentException. (str tag " is not a valid element name."))
:clje (str tag " is not a valid element name."))))
(let [[_ tag id class] (re-matches re-tag (util/as-str tag))
classes (if class (str/replace class "." " "))
map-attrs (first content)]
Expand Down Expand Up @@ -130,25 +135,46 @@
"</" tag ">")
(str "<" tag (render-attr-map attrs) (end-tag)))))

(extend-protocol HtmlRenderer
IPersistentVector
(render-html [this]
(render-element this))
ISeq
(render-html [this]
(apply str (map render-html this)))
RawString
(render-html [this]
(str this))
Named
(render-html [this]
(escape-html (name this)))
Object
(render-html [this]
(escape-html (str this)))
nil
(render-html [this]
""))
#?(:clj
(extend-protocol HtmlRenderer
IPersistentVector
(render-html [this]
(render-element this))
ISeq
(render-html [this]
(apply str (map render-html this)))
RawString
(render-html [this]
(str this))
Named
(render-html [this]
(escape-html (name this)))
Object
(render-html [this]
(escape-html (str this)))
nil
(render-html [this]
""))
:clje
(extend-protocol HtmlRenderer
Vector
(render-html [this]
(render-element this))
RawString
(render-html [this]
(str this))
default
(render-html [this]
(cond
(seq? this)
(apply str (map render-html this))
(satisfies? clojerl.INamed this)
(escape-html (name this))
:else
(escape-html (str this))))
nil
(render-html [this]
"")))

(defn- unevaluated?
"True if the expression has not been evaluated."
Expand Down Expand Up @@ -214,7 +240,10 @@
[x]
(or (= (form-name x) "for")
(not (unevaluated? x))
(not-hint? x java.util.Map)))
#?(:clj
(not-hint? x java.util.Map)
:clje
(not-hint? x clojerl.Map))))

(defn- element-compile-strategy
"Returns the compilation strategy to use for a given element."
Expand Down Expand Up @@ -296,7 +325,9 @@
(util/raw-string? expr) expr
(literal? expr) (escape-html expr)
(hint? expr String) `(escape-html ~expr)
(hint? expr Number) expr
#?@(:clj [(hint? expr Number) expr]
:clje [(or (hint? expr Float)
(hint? expr Integer)) expr])
(seq? expr) (compile-form expr)
:else `(render-html ~expr)))))

Expand Down Expand Up @@ -337,7 +368,8 @@
(= (set vals) #{true false})
`(if ~var-sym ~(compiled-forms true) ~(compiled-forms false))
:else
`(case ~var-sym ~@(apply concat distinct-forms)))))
#?(:clj `(case ~var-sym ~@(apply concat distinct-forms))
:clje `(case ~var-sym ~@(apply concat compiled-forms))))))

(defn compile-html-with-bindings
"Pre-compile data structures into HTML where possible, while taking into
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions src/hiccup/def.clj → src/hiccup/def.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
passed to the resulting function is a map, it merges it with the attribute
map of the returned element value."
[name & fdecl]
`(do (defn ~name ~@fdecl)
(alter-meta! (var ~name) update-in [:arglists] #'update-arglists)
(alter-var-root (var ~name) wrap-attrs)))
#?(:clj
`(do (defn ~name ~@fdecl)
(alter-meta! (var ~name) update-in [:arglists] #'update-arglists)
(alter-var-root (var ~name) wrap-attrs))
:clje
`(defn ~name ~@fdecl)))
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
123 changes: 0 additions & 123 deletions src/hiccup/util.clj

This file was deleted.

Loading