├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── dev ├── dev.clj └── user.clj ├── doc └── intro.md ├── project.clj ├── resources └── tron.html ├── src └── datascript_dom │ └── core.clj └── test └── datascript_dom └── core_test.clj /.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 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | ## [Unreleased][unreleased] 5 | ### Changed 6 | - Add a new arity to `make-widget-async` to provide a different widget shape. 7 | 8 | ## [0.1.1] - 2015-10-14 9 | ### Changed 10 | - Documentation on how to make the widgets. 11 | 12 | ### Removed 13 | - `make-widget-sync` - we're all async, all the time. 14 | 15 | ### Fixed 16 | - Fixed widget maker to keep working when daylight savings switches over. 17 | 18 | ## 0.1.0 - 2015-10-14 19 | ### Added 20 | - Files from the new template. 21 | - Widget maker public API - `make-widget-sync`. 22 | 23 | [unreleased]: https://github.com/your-name/datascript-dom/compare/0.1.1...HEAD 24 | [0.1.1]: https://github.com/your-name/datascript-dom/compare/0.1.0...0.1.1 25 | -------------------------------------------------------------------------------- /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 tocontrol, 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 | # datascript-dom 2 | 3 | A Clojure experiment to use datascript to perform DOM-like queries on 4 | HTML. 5 | 6 | This is not a library, just a monolithic namespace that defines some 7 | functions and also contains a huge comment section with expressions 8 | that are meant to be eval'ed one by one in the REPL for 9 | experimentation. git clone it and have a look. There are also a couple 10 | of unit tests. 11 | 12 | For further explanation, see [this blog post](http://blog.juxt.pro/posts/datascript-dom.html). 13 | 14 | ## License 15 | 16 | Copyright © 2015 Stathis Sideris 17 | 18 | Distributed under the Eclipse Public License either version 1.0 or (at 19 | your option) any later version. 20 | -------------------------------------------------------------------------------- /dev/dev.clj: -------------------------------------------------------------------------------- 1 | (ns dev 2 | (:require [clojure.tools.namespace.repl :refer [clear refresh-all]])) 3 | 4 | (defn refresh [] 5 | (clojure.tools.namespace.repl/refresh)) 6 | -------------------------------------------------------------------------------- /dev/user.clj: -------------------------------------------------------------------------------- 1 | (ns user) 2 | 3 | (defn load-dev [] 4 | (require 'dev) 5 | (in-ns 'dev)) 6 | 7 | (def dev load-dev) 8 | -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to datascript-dom 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject datascript-dom "0.1.0-SNAPSHOT" 2 | :description "FIXME: write description" 3 | :url "http://example.com/FIXME" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.7.0"] 7 | [datascript "0.13.1"] 8 | [clj-tagsoup "0.3.0"]] 9 | 10 | :profiles {:dev {:source-paths ["dev"] 11 | :dependencies [[org.clojure/tools.namespace "0.2.11"]]}}) 12 | -------------------------------------------------------------------------------- /src/datascript_dom/core.clj: -------------------------------------------------------------------------------- 1 | (ns datascript-dom.core 2 | (:require [datascript.core :as d] 3 | [pl.danieljanus.tagsoup :as html] 4 | [clojure.java.io :as io] 5 | [clojure.zip :as zip] 6 | [clojure.string :as string])) 7 | 8 | (def schema 9 | {:child {:db/valueType :db.type/ref 10 | :db/cardinality :db.cardinality/many}}) 11 | 12 | (def rules 13 | '[[(root ?node) 14 | [?node _ _] 15 | [(missing? $ ?node :_child)]] 16 | 17 | ;;ancestor 18 | [(anc ?par ?child) 19 | (?par :child ?child)] 20 | [(anc ?anc ?child) 21 | (?par :child ?child) 22 | (anc ?anc ?par)] 23 | 24 | ;;sibling 25 | [(siblings ?a ?b) 26 | [?p :child ?a] 27 | [?p :child ?b] 28 | [(!= ?a ?b)]] 29 | 30 | [(prev-sibling ?this ?sib) 31 | (siblings ?sib ?this) 32 | [?this :dom/index ?i1] 33 | [?sib :dom/index ?i2] 34 | [(- ?i2 ?i1) ?diff] 35 | [(= ?diff -1)]] 36 | 37 | [(next-sibling ?this ?sib) 38 | (siblings ?sib ?this) 39 | [?this :dom/index ?i1] 40 | [?sib :dom/index ?i2] 41 | [(- ?i2 ?i1) ?diff] 42 | [(= ?diff 1)]] 43 | 44 | ;;get all text of text nodes in container 45 | [(text ?container ?text) 46 | [?container :child ?text-node] 47 | [?text-node :dom/tag :text-node] 48 | [?text-node :text ?text]] 49 | 50 | [(text ?container ?text) 51 | [?container :dom/tag :text-node] 52 | [?container :text ?text]] 53 | 54 | [(path2 ?a ?b) 55 | [?a :child ?b]] 56 | [(path3 ?a ?b ?c) 57 | (path2 ?a ?b) 58 | (path2 ?b ?c)]]) 59 | 60 | (defn parse-html-file [filename] 61 | (html/parse (io/file filename))) 62 | 63 | (defn- to-entity [node] 64 | (if-not (string? node) 65 | (merge 66 | {:dom/tag (html/tag node)} 67 | 68 | ;;include attributes, but not :id 69 | (dissoc (html/attributes node) :id) 70 | 71 | ;;id will ne given a special name 72 | (when-let [id (:id node)] {:dom/id id}) 73 | 74 | ;;assoc children -- if present 75 | (when-let [children (html/children node)] 76 | {:child children})) 77 | 78 | ;;if it's a string, encode as text-node 79 | {:dom/tag :text-node 80 | :text node})) 81 | 82 | (defn- replace-node [zipper] 83 | (let [dom-index (or (some-> zipper zip/left zip/node :dom/index inc) 0)] 84 | (zip/replace 85 | zipper 86 | (-> zipper 87 | zip/node 88 | to-entity 89 | (assoc :dom/index dom-index))))) 90 | 91 | (defn dom-zipper [dom] 92 | (zip/zipper 93 | (fn [node] (not-empty (:child node))) ;;branch? 94 | :child 95 | (fn [node children] (assoc node :child children)) ;;make-node 96 | dom)) 97 | 98 | (defn dom->transaction [dom] 99 | (loop [zipper (dom-zipper dom)] 100 | (if (zip/end? zipper) 101 | [(zip/root zipper)] 102 | (recur (zip/next (replace-node zipper)))))) 103 | 104 | (defn- dump [dom] 105 | (let [z (dom-zipper dom)] 106 | (loop [z z] 107 | (prn (zip/node z)) 108 | (if-not (zip/end? z) (recur (zip/next z)))))) 109 | 110 | (defn get-touch [db id] 111 | (d/touch (d/entity db id))) 112 | 113 | (comment 114 | 115 | ;;small toy DOM 116 | (def small-dom 117 | (html/parse-string "

ABCDE

hohoho

")) 118 | ;; [:html {} [:body {} [:p {} "hehe" [:b {} "he"]] [:p {} "hohoho"]]] 119 | (def small-conn (d/create-conn schema)) 120 | (d/transact small-conn (dom->transaction small-dom)) 121 | 122 | ;;way more serious IMDB DOM 123 | (def dom (html/parse (io/file "resources/tron.html"))) 124 | (def conn (d/create-conn schema)) 125 | (def _ (d/transact conn (dom->transaction dom))) 126 | 127 | 128 | ;; THE FOLLOWING QUERIES ARE BEST APPLIED TO THE SMALL DOM (see parse-string above) 129 | 130 | ;;get the tag via its attribute 131 | (d/q '[:find [(pull ?node [*]) ...] 132 | :where 133 | [?node :class "test"]] 134 | @small-conn) 135 | 136 | ;;find root 137 | (d/q '[:find (pull ?node [:dom/tag]) . 138 | :where 139 | [?node _] 140 | [(missing? $ ?node :_child)]] 141 | @small-conn) 142 | 143 | ;;find root with a rule 144 | (d/q '[:find (pull ?node [:dom/tag]) . 145 | :in $ % 146 | :where (root ?node)] 147 | @small-conn 148 | '[[(root ?node) 149 | [?node _] 150 | [(missing? $ ?node :_child)]]]) 151 | 152 | (->> r (d/entity @small-conn) :child first ;;gets tag 153 | :child (map d/touch)) ;;gets the two paragraph tags 154 | 155 | ;;get all ancestors of 156 | (d/q '[:find [(pull ?anc [:dom/tag]) ...] 157 | :in $ % 158 | :where 159 | [?node :dom/tag :b] 160 | (anc ?anc ?node)] 161 | @small-conn rules) 162 | 163 | ;;get all siblings of 164 | (d/q '[:find (pull ?sib [*]) 165 | :in $ % 166 | :where 167 | [?node :dom/tag :b] 168 | (siblings ?node ?sib)] 169 | @small-conn rules) 170 | 171 | ;;get previous sibling of 172 | (d/q '[:find (pull ?sib [:dom/tag :text]) . 173 | :in $ % 174 | :where 175 | [?node :dom/tag :b] 176 | (prev-sibling ?node ?sib)] 177 | @small-conn rules) 178 | 179 | ;;get next sibling of 180 | (d/q '[:find (pull ?sib [:dom/tag :text]) . 181 | :in $ % 182 | :where 183 | [?node :dom/tag :b] 184 | (next-sibling ?node ?sib)] 185 | @small-conn rules) 186 | 187 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 188 | ;; ;; 189 | ;; THE FOLLOWING QUERIES ARE DESIGNED FOR THE IMDB DOM ;; 190 | ;; ;; 191 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 192 | 193 | ;;find title 194 | (def r 195 | (d/q '[:find ?text . 196 | :where 197 | [?h1 :dom/tag :h1] 198 | [?h1 :child ?node] 199 | [?node :dom/tag :span] 200 | [?node :itemprop "name"] 201 | [?node :child ?text-node] 202 | [?text-node :text ?text]] 203 | @conn)) 204 | 205 | ;;find title and year with text extractor rule 206 | (let [[title year] 207 | (d/q '[:find [?title ?year] 208 | :in $ % 209 | :where 210 | [?h1 :dom/tag :h1] 211 | [?h1 :child ?name-node] 212 | [?name-node :dom/tag :span] 213 | [?name-node :itemprop "name"] 214 | (text ?name-node ?title) 215 | 216 | (next-sibling ?name-node ?year-node) ;;year-node is the next sibling of name-node 217 | (?year-node :child ?a-node) ;;...and it contains an tag 218 | (?a-node :dom/tag :a) 219 | (text ?a-node ?year)] ;;and the tag contains the year 220 | @conn rules)] 221 | {:title title 222 | :year year}) 223 | 224 | ;;overview 225 | (d/q '[:find ?overview . 226 | :in $ % 227 | :where 228 | [?node :itemprop "description"] 229 | (text ?node ?overview)] 230 | @conn rules) 231 | 232 | ;;ratings 233 | (d/q '[:find [?rating ?count] 234 | :in $ % 235 | :where 236 | [?star-node :class "titlePageSprite star-box-giga-star"] 237 | (text ?star-node ?rating) 238 | 239 | [?rating-count :itemprop "ratingCount"] 240 | (text ?rating-count ?count)] 241 | @conn rules) 242 | 243 | ;;reviews 244 | (d/q '[:find ?reviews 245 | :in $ % 246 | :where 247 | [?review-node :itemprop "reviewCount"] 248 | (text ?review-node ?reviews)] 249 | @conn rules) 250 | 251 | ;;Extract Cast - part 1 252 | ;;The important guys follow this pattern (actors whose names have 253 | ;;links). Note that you get one entry per character, the same actor 254 | ;;can depict more than one character. 255 | (clojure.pprint/pprint 256 | (let [cast 257 | (d/q '[:find ?index ?actor-name ?link ?character-name ?character-link 258 | :in $ % 259 | :where 260 | 261 | ;;extract the index of the row so that we know the order 262 | [?tr :child ?actor-n] 263 | [?tr :dom/index ?index] 264 | 265 | ;;extract the name of the actor and the link to their profile 266 | [?actor-n :itemprop "actor"] 267 | (path3 ?actor-n ?actor-link-n ?actor-name-n) 268 | (text ?actor-name-n ?actor-name) 269 | (?actor-link-n :href ?link) 270 | 271 | ;;extract the name of the character(s) played by the actor and link(s) 272 | (siblings ?character-n ?actor-n) 273 | [?character-n :class "character"] 274 | (path3 ?character-n ?div ?character-link-n) 275 | (?character-link-n :href ?character-link) 276 | (text ?character-link-n ?character-name)] 277 | @conn rules)] 278 | (->> (map (partial zipmap [:index :actor :link :character :character-link]) cast) 279 | (sort-by :index)))) 280 | 281 | ;;Extract Cast - part 2 282 | ;;the unimportant guys follow this pattern 283 | ;;(actors whose names don't have links) 284 | (def r 285 | (sort-by first 286 | (d/q '[:find ?index ?actor-name ?link ?character-name 287 | :in $ % ?trim 288 | :where 289 | [?tr :child ?actor-n] 290 | [?tr :dom/index ?index] 291 | 292 | [?actor-n :itemprop "actor"] 293 | (path3 ?actor-n ?actor-link-n ?actor-name-n) 294 | (text ?actor-name-n ?actor-name) 295 | (?actor-link-n :href ?link) 296 | 297 | (siblings ?character-n ?actor-n) 298 | [?character-n :class "character"] 299 | (path2 ?character-n ?div) 300 | 301 | ;;the above matches the important characters as well, so we 302 | ;;clean the name and if we matched the "/" between the 303 | ;;character links, we filter those entries out 304 | (text ?div ?character-name-dirty) 305 | [(?trim ?character-name-dirty) ?character-name] 306 | [(!= "/" ?character-name)]] 307 | @conn rules (fn [x] (.trim (string/replace x #"\s+" " ")))))) 308 | 309 | ;;Extract genres 310 | (d/q '[:find [?g ...] 311 | :in $ % 312 | :where 313 | [?div :itemprop "genre"] 314 | [?div :child ?a] 315 | [?a :dom/tag :a] 316 | (text ?a ?g)] 317 | @conn rules) 318 | 319 | ;;Duration 320 | (d/q '[:find ?duration . 321 | :in $ % 322 | :where 323 | [?node :itemprop "duration"] 324 | (text ?node ?duration)] 325 | @conn rules) 326 | 327 | (defn tech-spec [conn label] 328 | (->> 329 | (d/q '[:find ?index ?value-text 330 | :in $ % ?label-text 331 | :where 332 | [?label :dom/tag :h4] 333 | [?label :class "inline"] 334 | (text ?label ?label-text) 335 | 336 | (siblings ?label ?value) 337 | (text ?value ?value-text) 338 | [(!= "|" ?value-text)] 339 | (?value :dom/index ?index)] 340 | @conn rules label) 341 | (sort-by first) 342 | (map (comp (fn [x] (.trim x)) second)))) 343 | 344 | (tech-spec conn "Runtime:") 345 | (tech-spec conn "Color:") 346 | (tech-spec conn "Sound Mix:") 347 | (tech-spec conn "Aspect Ratio:") 348 | 349 | ;;also works on other labelled fields! 350 | (tech-spec conn "Budget:") 351 | (tech-spec conn "Gross:") 352 | (tech-spec conn "Language:") 353 | 354 | ) 355 | 356 | -------------------------------------------------------------------------------- /test/datascript_dom/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns datascript-dom.core-test 2 | (:require [clojure.test :refer :all] 3 | [datascript-dom.core :refer :all] 4 | [datascript.core :as d] 5 | [pl.danieljanus.tagsoup :as html] 6 | [clojure.zip :as zip])) 7 | 8 | (deftest test-dom->transaction 9 | (is (= 10 | [{:dom/tag :html, 11 | :child 12 | [{:dom/tag :body, 13 | :class "test", 14 | :child 15 | [{:dom/tag :p, 16 | :child 17 | [{:dom/tag :text-node, :text "A", :dom/index 0} 18 | {:dom/tag :b, 19 | :child [{:dom/tag :text-node, :text "B", :dom/index 0}], 20 | :dom/index 1} 21 | {:dom/tag :text-node, :text "C", :dom/index 2} 22 | {:dom/tag :i, 23 | :child [{:dom/tag :text-node, :text "D", :dom/index 0}], 24 | :dom/index 3} 25 | {:dom/tag :text-node, :text "E", :dom/index 4}], 26 | :dom/index 0} 27 | {:dom/tag :p, 28 | :child [{:dom/tag :text-node, :text "hohoho", :dom/index 0}], 29 | :dom/index 1}], 30 | :dom/index 0}], 31 | :dom/index 0}] 32 | (dom->transaction 33 | (html/parse-string "

ABCDE

hohoho

"))))) 34 | 35 | (deftest basic-queries 36 | (let [html "

ABCDE

hohoho

" 37 | dom (html/parse-string html) 38 | conn (d/create-conn schema)] 39 | @(d/transact conn (dom->transaction dom)) 40 | 41 | (is (= {:dom/tag :body} 42 | (d/q '[:find (pull ?node [:dom/tag]) . 43 | :where 44 | [?node :class "test"]] @conn))) 45 | 46 | (is (= {:dom/tag :html} 47 | (d/q '[:find (pull ?node [:dom/tag]) . 48 | :where 49 | [?node _] 50 | [(missing? $ ?node :_child)]] @conn))) 51 | 52 | (is (= {:dom/tag :html} 53 | (d/q '[:find (pull ?node [:dom/tag]) . 54 | :in $ % 55 | :where 56 | (root ?node)] 57 | @conn rules))) 58 | 59 | (is (= [{:dom/tag :p} {:dom/tag :body} {:dom/tag :html}] 60 | (d/q '[:find [(pull ?anc [:dom/tag]) ...] 61 | :in $ % 62 | :where 63 | [?node :dom/tag :b] 64 | (anc ?anc ?node)] 65 | @conn rules))) 66 | 67 | (is (= [{:dom/tag :text-node, :text "A"}] 68 | (d/q '[:find [(pull ?sib [:dom/tag :text]) ...] 69 | :in $ % 70 | :where 71 | [?node :dom/tag :b] 72 | (prev-sibling ?node ?sib)] 73 | @conn rules))) 74 | 75 | (is (= [{:dom/tag :text-node, :text "C"}] 76 | (d/q '[:find [(pull ?sib [:dom/tag :text]) ...] 77 | :in $ % 78 | :where 79 | [?node :dom/tag :b] 80 | (next-sibling ?node ?sib)] 81 | @conn rules))))) 82 | --------------------------------------------------------------------------------