Giter VIP home page Giter VIP logo

Comments (5)

vspinu avatar vspinu commented on June 13, 2024 2

I finally got down to figuring this out:

(defn hickory-remove [selector-fn hickory-tree]
  (loop [zip (hickory.zip/hickory-zip hickory-tree)
         next (hs/select-next-loc selector-fn zip)]
    (if next
      (let [new-zip (zip/remove next)]
        (recur new-zip (hs/select-next-loc selector-fn new-zip)))
      (zip/root zip))))

Usage:

(hickory-remove (hs/node-type :comment) tt)

I think it's a useful utility, but feel free to close if you think it's not worth including into the package.

from hickory.

eigenhombre avatar eigenhombre commented on June 13, 2024 2

An actual example in the docs (such as this one, or even simpler) of combining selectors and zippers to modify some HTML would be helpful!

from hickory.

 avatar commented on June 13, 2024 2

here an even more generic function:

(defn hickory-update [selector-fn hickory-tree zip-fn]
  (loop [zip (hickory.zip/hickory-zip hickory-tree)
         next (hickory.select/select-next-loc selector-fn zip)]
    (if next
      (let [new-zip (zip-fn next)]
        (recur new-zip (hickory.select/select-next-loc selector-fn new-zip)))
      (zip/root zip))))

; example: replace h3 by h2 tag
(defn replace-h3-h2 [t]
  (hickory-update
   (hickory.select/tag :h3)
   t
   #(zip/edit
     %
     (fn [n]
       (assoc n :tag :h2)))))

from hickory.

davidsantiago avatar davidsantiago commented on June 13, 2024

Hickory lets you make any modification you want to the tree using Clojure's Zipper interface, just use select-locs or select-next-loc instead of select, and you'll be returned a Clojure zipper that you can do modifications on.

from hickory.

vspinu avatar vspinu commented on June 13, 2024

and you'll be returned a Clojure zipper that you can do modifications on.

Thanks. I will try it out and will post an example here for the reference.

from hickory.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.