Enlive: select a comment, drop all subsequent nodes?

102 views
Skip to first unread message

Jakub Holý

unread,
Dec 13, 2019, 6:49:05 AM12/13/19
to clo...@googlegroups.com
Hello! I'd really appreciate help with this problem: I have a blog post containing `<!--more-->` to mark the (prior) part of the text that should be used in previews etc. Given an Enlive DOM tree, how do I get just the preview?

I guess I want to
1. Create a selector for the comment node (`{:type :comment, :text "more"})` `(pred #(= % {:type :comment..})) ` seems useful to start turning this into a selector but isn't enough. So what else?
2. How to drop all nodes after the comment? Perhaps transform with this selector + somehow use `rights` to select all nodes after it and somehow remove them?

Thanks a lot for any tips! I'm new to Enlive and it is daunting. 

Matching Socks

unread,
Dec 15, 2019, 6:44:50 PM12/15/19
to Clojure
Indeed "lefts" or "rights" will be the key.  A transformation is just a function (open up enlive's html.clj to see how it works), so you can sanity-check yourself by providing a "transformation" that prints out the matched nodes.  Feel free to regard Enlive's own transformations as mere samples.  You could match some node (the comment's parent) and replace its contents with 2 divs, one selecting content-before the marker comment and the other the content-after.

Alan Thompson

unread,
Dec 16, 2019, 3:18:22 PM12/16/19
to clojure
Quick & dirty technique:

(ns tst.demo.core
(:use demo.core tupelo.core tupelo.test)
(:require
[clojure.java.io :as io]
[clojure.walk :as walk]
[tupelo.parse.tagsoup :as tagsoup] ))

(dotest
(let [txt (slurp (io/resource "test.html"))
>> (println txt)
enlive-data (tagsoup/parse txt)
>> (spyx-pretty enlive-data)
found-comment? (atom false)
tx-fn (fn [item]
(when (and (map? item)
(= :comment (:type item))
(= "more" (:data item)))
(reset! found-comment? true))
(if @found-comment?
nil
item))
enlive-keep (walk/prewalk tx-fn enlive-data) ]
(newline)
(spyx-pretty enlive-keep)
) )


with result:

-------------------------------
   Clojure 1.10.1    Java 13
-------------------------------

Testing tst.demo.core
<html>
  <h1>This is a test </h1>
  <!--more-->
  <h2>This is only a test </h2>
</html>


enlive-data =>
{:tag :html,
 :attrs {},
 :content
 [{:tag :body,
   :attrs {},
   :content
   [{:tag :h1, :attrs {}, :content ["This is a test "]}
    {:type :comment, :data "more"}
    {:tag :h2, :attrs {}, :content ["This is only a test "]}]}]}

enlive-keep =>
{:tag :html,
 :attrs {},
 :content
 [{:tag :body,
   :attrs {},
   :content
   [{:tag :h1, :attrs {}, :content ["This is a test "]} nil nil]}]}





On Sun, Dec 15, 2019 at 3:44 PM Matching Socks <phill...@gmail.com> wrote:
Indeed "lefts" or "rights" will be the key.  A transformation is just a function (open up enlive's html.clj to see how it works), so you can sanity-check yourself by providing a "transformation" that prints out the matched nodes.  Feel free to regard Enlive's own transformations as mere samples.  You could match some node (the comment's parent) and replace its contents with 2 divs, one selecting content-before the marker comment and the other the content-after.

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/9da515ab-d685-4dbb-91cd-ffdfc29ee23b%40googlegroups.com.

Alan Thompson

unread,
Dec 16, 2019, 3:21:49 PM12/16/19
to clojure
You can be more precise if you use the `tupelo.forest` library for processing tree-like data structures (this will allow you to avoid the trailing `nil` values, for example).  Please see the docs, and also the numerous examples.
Alan
Reply all
Reply to author
Forward
0 new messages