From de7cf921f8965fbe67782cab9543005f0763fe23 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Mon, 2 Oct 2017 22:11:08 +0200 Subject: [PATCH 1/5] Adapt code for Clojerl --- src/hiccup/{compiler.clj => compiler.cljc} | 0 src/hiccup/{core.clj => core.cljc} | 0 src/hiccup/{def.clj => def.cljc} | 0 src/hiccup/{element.clj => element.cljc} | 0 src/hiccup/{form.clj => form.cljc} | 0 src/hiccup/{middleware.clj => middleware.cljc} | 0 src/hiccup/{page.clj => page.cljc} | 0 src/hiccup/{util.clj => util.cljc} | 0 src/hiccup2/{core.clj => core.cljc} | 0 9 files changed, 0 insertions(+), 0 deletions(-) rename src/hiccup/{compiler.clj => compiler.cljc} (100%) rename src/hiccup/{core.clj => core.cljc} (100%) rename src/hiccup/{def.clj => def.cljc} (100%) rename src/hiccup/{element.clj => element.cljc} (100%) rename src/hiccup/{form.clj => form.cljc} (100%) rename src/hiccup/{middleware.clj => middleware.cljc} (100%) rename src/hiccup/{page.clj => page.cljc} (100%) rename src/hiccup/{util.clj => util.cljc} (100%) rename src/hiccup2/{core.clj => core.cljc} (100%) diff --git a/src/hiccup/compiler.clj b/src/hiccup/compiler.cljc similarity index 100% rename from src/hiccup/compiler.clj rename to src/hiccup/compiler.cljc diff --git a/src/hiccup/core.clj b/src/hiccup/core.cljc similarity index 100% rename from src/hiccup/core.clj rename to src/hiccup/core.cljc diff --git a/src/hiccup/def.clj b/src/hiccup/def.cljc similarity index 100% rename from src/hiccup/def.clj rename to src/hiccup/def.cljc diff --git a/src/hiccup/element.clj b/src/hiccup/element.cljc similarity index 100% rename from src/hiccup/element.clj rename to src/hiccup/element.cljc diff --git a/src/hiccup/form.clj b/src/hiccup/form.cljc similarity index 100% rename from src/hiccup/form.clj rename to src/hiccup/form.cljc diff --git a/src/hiccup/middleware.clj b/src/hiccup/middleware.cljc similarity index 100% rename from src/hiccup/middleware.clj rename to src/hiccup/middleware.cljc diff --git a/src/hiccup/page.clj b/src/hiccup/page.cljc similarity index 100% rename from src/hiccup/page.clj rename to src/hiccup/page.cljc diff --git a/src/hiccup/util.clj b/src/hiccup/util.cljc similarity index 100% rename from src/hiccup/util.clj rename to src/hiccup/util.cljc diff --git a/src/hiccup2/core.clj b/src/hiccup2/core.cljc similarity index 100% rename from src/hiccup2/core.clj rename to src/hiccup2/core.cljc From 57fea6f04d1893f519e33307689cbf3917ecfee6 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Tue, 3 Oct 2017 00:19:10 +0200 Subject: [PATCH 2/5] Add app.src --- src/hiccup.app.src | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/hiccup.app.src diff --git a/src/hiccup.app.src b/src/hiccup.app.src new file mode 100644 index 00000000..176f085a --- /dev/null +++ b/src/hiccup.app.src @@ -0,0 +1,13 @@ +{ application +, hiccup, + [ {description, "Compile clojerl projects"} + , {vsn, git} + , {registered, []} + , {applications, [kernel, stdlib]} + , {env,[]} + , {modules, []} + , {maintainers, []} + , {licenses, []} + , {links, []} + ] +}. From 9d377430299afdd565c0b91e5afdfdf2cfd27ded Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Tue, 3 Oct 2017 23:17:07 +0200 Subject: [PATCH 3/5] Add reader conditionals for clojerl --- .gitignore | 1 + src/hiccup/compiler.cljc | 82 ++++++++++++++++-------- src/hiccup/def.cljc | 9 ++- src/hiccup/util.cljc | 134 +++++++++++++++++++++++++-------------- 4 files changed, 151 insertions(+), 75 deletions(-) diff --git a/.gitignore b/.gitignore index 296ef057..170826d7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ /codox /classes /checkouts +/ebin pom.xml pom.xml.asc *.jar diff --git a/src/hiccup/compiler.cljc b/src/hiccup/compiler.cljc index 791c11ce..5a8fa743 100644 --- a/src/hiccup/compiler.cljc +++ b/src/hiccup/compiler.cljc @@ -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*)) @@ -88,7 +92,8 @@ "Ensure an element vector is of the form [tag-name attrs content]." [[tag & content]] (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)] @@ -110,25 +115,46 @@ "") (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." @@ -194,7 +220,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." @@ -276,7 +305,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))))) @@ -317,7 +348,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 diff --git a/src/hiccup/def.cljc b/src/hiccup/def.cljc index 0df3941e..5ed3112d 100644 --- a/src/hiccup/def.cljc +++ b/src/hiccup/def.cljc @@ -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))) diff --git a/src/hiccup/util.cljc b/src/hiccup/util.cljc index 833eac5c..5a111b0c 100644 --- a/src/hiccup/util.cljc +++ b/src/hiccup/util.cljc @@ -1,8 +1,9 @@ (ns hiccup.util "Utility functions for Hiccup." (:require [clojure.string :as str]) - (:import java.net.URI - java.net.URLEncoder)) + #?(:clj (:import java.net.URI + java.net.URLEncoder) + :clje (:import clojerl.String))) (def ^:dynamic ^:no-doc *html-mode* :xhtml) @@ -23,46 +24,74 @@ (defprotocol ToString (^String to-str [x] "Convert a value into a string.")) -(extend-protocol ToString - clojure.lang.Keyword - (to-str [k] (name k)) - clojure.lang.Ratio - (to-str [r] (str (float r))) - java.net.URI - (to-str [u] - (if (or (.getHost u) - (nil? (.getPath u)) - (not (-> (.getPath u) (.startsWith "/")))) - (str u) - (let [base (str *base-url*)] - (if (.endsWith base "/") - (str (subs base 0 (dec (count base))) u) - (str base u))))) - Object - (to-str [x] (str x)) - nil - (to-str [_] "")) +#?(:clj + (extend-protocol ToString + clojure.lang.Keyword + (to-str [k] (name k)) + clojure.lang.Ratio + (to-str [r] (str (float r))) + java.net.URI + (to-str [u] + (if (or (.getHost u) + (nil? (.getPath u)) + (not (-> (.getPath u) (.startsWith "/")))) + (str u) + (let [base (str *base-url*)] + (if (.endsWith base "/") + (str (subs base 0 (dec (count base))) u) + (str base u))))) + Object + (to-str [x] (str x)) + nil + (to-str [_] "")) + + :clje + (extend-protocol ToString + clojerl.Keyword + (to-str [k] (name k)) + default + (to-str [x] (str x)) + nil + (to-str [_] ""))) (defn ^String as-str "Converts its arguments into a string using [[to-str]]." [& xs] (apply str (map to-str xs))) -(defprotocol ToURI - (^java.net.URI to-uri [x] "Convert a value into a URI.")) - -(extend-protocol ToURI - java.net.URI - (to-uri [u] u) - String - (to-uri [s] (URI. s))) - -(deftype RawString [^String s] - Object - (^String toString [this] s) - (^boolean equals [this other] - (and (instance? RawString other) - (= s (.toString other))))) +#?(:clj + (defprotocol ToURI + (^java.net.URI to-uri [x] "Convert a value into a URI.")) + :clje + (defprotocol ToURI + (^clojerl.String to-uri [x] "Convert a value into a URI."))) + +#?(:clj + (extend-protocol ToURI + java.net.URI + (to-uri [u] u) + String + (to-uri [s] (URI. s))) + :clje + (extend-protocol ToURI + String + (to-uri [s] s))) + +#?(:clj + (deftype RawString [^String s] + Object + (^String toString [this] s) + (^boolean equals [this other] + (and (instance? RawString other) + (= s (.toString other))))) + :clje + (deftype RawString [^String s] + clojerl.IStringable + (^String str [this] s) + clojerl.IEquiv + (^clojerl.Boolean equiv [this other] + (and (instance? RawString other) + (= s (str other)))))) (defn raw-string "Converts one or more strings into an object that will not be escaped when @@ -96,16 +125,28 @@ (defprotocol URLEncode (url-encode [x] "Turn a value into a URL-encoded string.")) -(extend-protocol URLEncode - String - (url-encode [s] (URLEncoder/encode s *encoding*)) - java.util.Map - (url-encode [m] - (str/join "&" - (for [[k v] m] - (str (url-encode k) "=" (url-encode v))))) - Object - (url-encode [x] (url-encode (to-str x)))) +#?(:clj + (extend-protocol URLEncode + String + (url-encode [s] (URLEncoder/encode s *encoding*)) + java.util.Map + (url-encode [m] + (str/join "&" + (for [[k v] m] + (str (url-encode k) "=" (url-encode v))))) + Object + (url-encode [x] (url-encode (to-str x)))) + :clje + (extend-protocol URLEncode + String + (url-encode [s] (http_uri/encode.e s)) + clojerl.Map + (url-encode [m] + (str/join "&" + (for [[k v] m] + (str (url-encode k) "=" (url-encode v))))) + default + (url-encode [x] (url-encode (to-str x))))) (defn url "Creates a URI instance from a variable list of arguments and an optional @@ -120,4 +161,3 @@ (if (map? params) (str "?" (url-encode params)) params))))) - From 958d51f5bab0259729f1e40c47bc94b1c576ef74 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Thu, 12 Oct 2017 22:40:35 +0200 Subject: [PATCH 4/5] Change project description --- src/hiccup.app.src | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hiccup.app.src b/src/hiccup.app.src index 176f085a..6b262b0a 100644 --- a/src/hiccup.app.src +++ b/src/hiccup.app.src @@ -1,6 +1,6 @@ { application , hiccup, - [ {description, "Compile clojerl projects"} + [ {description, "A fast library for rendering HTML in Clojure"} , {vsn, git} , {registered, []} , {applications, [kernel, stdlib]} From d35e32fbf4d44a6a48981fe6c1a1179fd2a4fdb3 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Thu, 26 Oct 2017 01:22:27 +0200 Subject: [PATCH 5/5] Remove .e suffix --- src/hiccup/util.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hiccup/util.cljc b/src/hiccup/util.cljc index 5a111b0c..ac446b10 100644 --- a/src/hiccup/util.cljc +++ b/src/hiccup/util.cljc @@ -139,7 +139,7 @@ :clje (extend-protocol URLEncode String - (url-encode [s] (http_uri/encode.e s)) + (url-encode [s] (http_uri/encode s)) clojerl.Map (url-encode [m] (str/join "&"