├── .gitignore ├── LICENSE ├── README.md ├── doc └── intro.md ├── plan.org ├── project.clj ├── src └── levand │ ├── heat.cljc │ └── heat │ └── hiccup_spec.cljc └── test ├── levand └── heat_test.cljc └── resources └── phantomjs-externs.js /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | *.iml 13 | .idea 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor to control, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # H.E.A.T - Hiccup-Enlive Adapter & Transforms 2 | 3 | Provides tools to create Enlive templates from embedded Hiccup 4 | data structures, rather than external HTML files. 5 | 6 | Requires the ClojureScript-compatible refactoring of Enlive, which may 7 | be found on the `cljs-support` branch of 8 | https://github.com/levand/enlive. 9 | 10 | ## Usage 11 | 12 | The `levand.heat/transform` macro is the primary API, and provides a 13 | succinct way to transform hiccup data into other hiccup data using 14 | Enlive transformations. 15 | 16 | It does so by wrapping the 17 | `net.cgrand.enlive/at` macro, along with the Hiccup->Enlive and 18 | Enlive->Hiccup data transformations that are required. 19 | 20 | ```clojure 21 | (def template [:div#foo 22 | [:div.container 23 | [:span "foo"]]]) 24 | 25 | (transform template [:#foo :.container] 26 | [:span] (e/content "bar")) 27 | 28 | ;; => 29 | 30 | [:div.container [:span "bar"]] 31 | ``` 32 | 33 | 34 | Enlive has a built in function, `net.cgrand.enlive/html` to convert from 35 | Hiccup-shaped data structures to Enlive-shaped data. 36 | 37 | This library provides a function to perform the inverse operation, 38 | transforming Enlive data to the Hiccup shape: `levand.heat/hiccup`. 39 | For example: 40 | 41 | ```clojure 42 | 43 | (hiccup {:tag :div, 44 | :attrs {:class "container"}, 45 | :content [{:tag :span, :attrs {}, :content ["bar"]}]}) 46 | 47 | ;; => 48 | 49 | [:div.container [:span "bar"]] 50 | ``` 51 | 52 | ## License 53 | 54 | Copyright © 2017 Luke VanderHart 55 | 56 | Distributed under the Eclipse Public License either version 1.0 or (at 57 | your option) any later version. 58 | -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to heat 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /plan.org: -------------------------------------------------------------------------------- 1 | 2 | (def fruitlist 3 | [:div.fruits 4 | [:table 5 | [:thead [:tr [:th "Name"] [:th "Weight"]]] 6 | [:tbody 7 | [:tr [:td "Lemon"] [:td "2lbs"]]]]]) 8 | 9 | 10 | (my/rumsnippet Snippet fruitlist [:tbody :> first-child] < [:rum/component] 11 | [fruit quantity send version] 12 | {[:tr :> first-child] (content fruit) 13 | [:tr :> last-child] (content (str quantity))}) ;; => A Rum-friendly component 14 | 15 | 16 | 17 | ;; Hiccup data -> Enlive data -> Enlive transform -> Enlive data -> Sablono -> Return from Rum Component 18 | ;; 19 | ;; Work Points: 20 | ;; 1. Hiccup data -> Enlive data (pure transform) 21 | ;; 2. Enlive data -> Sablono data (pure transform) 22 | ;; 3. API (macro + helper fns) 23 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject levand/heat "0.1.0-SNAPSHOT" 2 | :description "H.E.A.T. - Hiccup Enlive Adapter & Transforms" 3 | :url "http://github.com/levand/heat" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.9.0-alpha16"] 7 | [enlive "1.2.0-SNAPSHOT"] ;; from cljs-support branch of https://github.com/levand/enlive 8 | ] 9 | :profiles {:dev {:resource-paths ["test/resources"] 10 | :dependencies [[org.clojure/test.check "0.9.0"] 11 | [com.gfredericks/test.chuck "0.2.7"]]} 12 | :test {:plugins [[lein-cljsbuild "1.1.6"] 13 | [lein-shell "0.4.0"]] 14 | :dependencies [[org.clojure/clojurescript "1.9.562"] 15 | [org.clojure/test.check "0.9.0"] 16 | [com.gfredericks/test.chuck "0.2.7"]] 17 | :resource-paths ["test/resources"] 18 | :aliases {"test-clj" ["run" "-m" "levand.heat-test"] 19 | "test-cljs" ["do" ["clean"] 20 | ["cljsbuild" "once" "advanced"] 21 | ["shell" "phantomjs" "target/js/test-advanced.js"]] 22 | "test-all" ["do" ["test-clj"] 23 | ["test-cljs"]]} 24 | :cljsbuild {:builds {:whitespace {:source-paths ["src" "test"] 25 | :compiler {:output-to "target/js/test-ws.js" 26 | :optimizations :whitespace}} 27 | :advanced {:source-paths ["src" "test"] 28 | :compiler {:output-to "target/js/test-advanced.js" 29 | :externs ["resources/phantomjs-externs.js"] 30 | :optimizations :advanced}}}}}} 31 | ) 32 | -------------------------------------------------------------------------------- /src/levand/heat.cljc: -------------------------------------------------------------------------------- 1 | (ns levand.heat 2 | (:require [net.cgrand.enlive-html :as e] 3 | [clojure.string :as str])) 4 | 5 | (defn- hiccup-name 6 | "Return the hiccup name for a node" 7 | [node] 8 | (let [class (-> node :attrs :class) 9 | id (-> node :attrs :id)] 10 | (keyword 11 | (str (name (:tag node)) 12 | (when id (str "#" id)) 13 | (when class (str "." (str/replace class \space \.))))))) 14 | 15 | (defn- hiccup-node 16 | "Convert a single Enlive node to its Hiccup equivalent" 17 | [node] 18 | (if (map? node) 19 | (let [attrs (-> node :attrs (dissoc :id :class)) 20 | name (hiccup-name node)] 21 | (into (if (empty? attrs) [name] [name attrs]) 22 | (map hiccup-node (:content node)))) 23 | node)) 24 | 25 | (defn hiccup 26 | "Given an Enlive data structure, return the equivalent Hiccup data structure." 27 | [node-or-nodes] 28 | (if (sequential? node-or-nodes) 29 | (map hiccup-node node-or-nodes) 30 | (hiccup-node node-or-nodes))) 31 | 32 | (defmacro transform 33 | "Apply Enlive transformations to a Hiccup-style data structure. 34 | 35 | Takes three arguments: 36 | 37 | 1. The template data structure (which should resolve to Hiccup-style data) 38 | 2. An Enlive selector selecting a subset of the template. 39 | 3. Any number of Enlive selector/transformation pairs. 40 | 41 | For example: 42 | 43 | (def template [:div#foo 44 | [:div.container 45 | [:span \"foo\"]]]) 46 | 47 | (transform template [:#foo :.container] 48 | [:span] (e/content \"bar\")) 49 | 50 | 51 | Note that for performance reasons, the `template` expression is resolved 52 | once, and cached. Do not use this macro if the template is dynamic and may 53 | change between calls. 54 | 55 | Also note that it is possible for the template selector to match multiple 56 | nodes. In this case, only the first node will be transformed and returned." 57 | [template selector & transformations] 58 | (let [cache-symbol (gensym "template-cache__")] 59 | `(do 60 | (defonce ~cache-symbol (first (e/select (e/html ~template) ~selector))) 61 | (first (hiccup 62 | (e/at ~cache-symbol 63 | ~@transformations)))))) 64 | 65 | (comment 66 | 67 | (def template [:div#foo 68 | [:div.container 69 | [:span "foo"]]]) 70 | 71 | (transform template [:#foo :.container] 72 | [:span] (e/content "bar")) 73 | 74 | ;; => 75 | 76 | (e/html [:div.container [:span "bar"]]) 77 | 78 | (hiccup {:tag :div, 79 | :attrs {:class "container"}, 80 | :content [{:tag :span, :attrs {}, :content ["bar"]}]}) 81 | 82 | ;; => 83 | 84 | [:div.container [:span "bar"]] 85 | 86 | 87 | 88 | ) 89 | -------------------------------------------------------------------------------- /src/levand/heat/hiccup_spec.cljc: -------------------------------------------------------------------------------- 1 | (ns levand.heat.hiccup-spec 2 | "Specs for Hiccup-shaped data" 3 | (:require [clojure.spec.alpha :as s])) 4 | 5 | ;; Todo: extend tag to match hiccup tags with IDs ids and classes 6 | (def tag-re #"[a-zA-Z][a-zA-Z0-9]*") 7 | 8 | (s/def ::node (s/or :element ::element 9 | :text string?)) 10 | 11 | (s/def ::tag (s/and simple-keyword? #(re-matches tag-re (name %)))) 12 | 13 | (s/def ::attrs (s/map-of simple-keyword? any?)) 14 | 15 | (s/def ::element 16 | (s/and vector? 17 | (s/cat :tag ::tag 18 | :attrs (s/? ::attrs) 19 | :children (s/* ::node)))) 20 | 21 | (comment 22 | 23 | (s/conform ::node [:foo {:class "foo"} "biz"]) 24 | 25 | (s/assert ::node [:foo.bar {:class "foo"} "biz"]) 26 | 27 | (s/check-asserts true) 28 | 29 | (s/conform ::tag :foo.bar) 30 | (s/assert ::tag :foo.bar) 31 | 32 | 33 | 34 | ) 35 | -------------------------------------------------------------------------------- /test/levand/heat_test.cljc: -------------------------------------------------------------------------------- 1 | (ns levand.heat-test 2 | (:require [net.cgrand.enlive-html :as e] 3 | [levand.heat :as h] 4 | [levand.heat.hiccup-spec :as hs] 5 | [clojure.string :as str] 6 | [clojure.spec.alpha :as s] 7 | [clojure.spec.gen.alpha :as gen] 8 | [com.gfredericks.test.chuck.generators :as gen'] 9 | [clojure.test.check.generators :as tcgen] 10 | [clojure.test.check.clojure-test :as tc] 11 | [clojure.test.check.properties :as prop] 12 | #?(:clj [clojure.test :refer [deftest is are run-tests]] 13 | :cljs [cljs.test :refer-macros [deftest is are run-tests]]))) 14 | 15 | (s/def ::hs/tag (s/with-gen ::hs/tag 16 | #(gen/fmap keyword (gen'/string-from-regex hs/tag-re)))) 17 | 18 | ;; TODO: These are ugly, because we need to repeat the specs instead of "modifying" existing ones 19 | (s/def ::hs/attrs (s/with-gen ::hs/attrs 20 | #(s/gen (s/map-of simple-keyword? any? :min-count 1)))) 21 | 22 | ;; TODO: Gen of vector regular expressions seems like it should be built in? 23 | (s/def ::hs/element (s/with-gen ::hs/element 24 | #(gen/fmap vector (s/gen (s/cat :tag ::hs/tag 25 | :attrs (s/? ::hs/attrs) 26 | :children (s/* ::hs/node)))))) 27 | 28 | ;; TODO: Implement this. Running into multiple bugs(?) with generation 29 | #_(tc/defspec hiccup-data-round-trips 2 30 | (prop/for-all [hiccup (s/gen ::hs/element)] 31 | (= hiccup (h/hiccup (first (e/html hiccup)))))) 32 | 33 | (def template [:div#foo 34 | [:div.container 35 | [:span "foo"]]]) 36 | 37 | (def output [:div.container 38 | [:span "bar"]]) 39 | 40 | 41 | (deftest transform-test 42 | (is (= output (h/transform template [:.container] 43 | [:span] (e/content "bar"))))) 44 | 45 | #?(:cljs (enable-console-print!)) 46 | 47 | (defn -main 48 | "Entry point for running tests (until *.cljc tools catch up)" 49 | [] 50 | #?(:clj 51 | (clojure.test/run-tests 'levand.heat-test) 52 | :cljs 53 | (cljs.test/run-tests 'levand.heat-test))) 54 | 55 | ;; Run tests at the root level, in CLJS 56 | #?(:cljs 57 | (do (-main) 58 | (.exit js/phantom))) -------------------------------------------------------------------------------- /test/resources/phantomjs-externs.js: -------------------------------------------------------------------------------- 1 | var phantom = {}; 2 | 3 | phantom.exit = function(){}; --------------------------------------------------------------------------------