-
Notifications
You must be signed in to change notification settings - Fork 52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Select all between two things? #82
Comments
I made a couple functions to help with this task, based on Hickory's (defn select-locs-between
"Given a start selector function, an end selector function, a
filter selector function, and a hickory data structure, returns a
vector containing all of the locs between the loc selected by
the start selector and the loc selected by the end selector where
the filter selector function returned a loc."
[start-selector-fn end-selector-fn filter-selector-fn hickory-tree]
(loop [loc (->> hickory-tree
(hzip/hickory-zip)
(sel/select-next-loc start-selector-fn)
(zip/right)
(sel/select-next-loc filter-selector-fn))
selected-nodes (transient [])]
(if (nil? loc)
(persistent! selected-nodes)
(recur (sel/select-next-loc filter-selector-fn (zip/right loc) zip/right end-selector-fn)
(conj! selected-nodes loc)))))
(defn select-between
"Given a start selector function, an end selector function, a
filter selector function, and a hickory data structure, returns a
vector containing all of the nodes between the loc selected by
the start selector and the loc selected by the end selector where
the filter selector function returned a loc."
[start-selector-fn end-selector-fn selector-fn hickory-tree]
(mapv zip/node (select-locs-between start-selector-fn end-selector-fn selector-fn hickory-tree))) As mentioned above, Here is an example: (require '[hickory.core :as h]
'[hickory.select :as s])
(def html
"<ul class=\"pagination\">
<li class=\"prevnext\"><a href=\"#\" onclick=\"return false;\" class=\"disablelink\"><</a></li>
<li class=\"current\"><a href=\"/pc-game-pass/games\">1</a></li>
<li><a href=\"/pc-game-pass/games?page=2\" rel=\"nofollow\">2</a></li>
<li class=\"h-ellip\"><span>...</span></li>
<li><a href=\"/pc-game-pass/games?page=3\" rel=\"nofollow\">3</a></li>
<li class=\"l\"><a href=\"/pc-game-pass/games?page=4\" rel=\"nofollow\">4</a></li>
<li class=\"prevnext\"><a href=\"/pc-game-pass/games?page=2\" rel=\"nofollow\">></a></li>
</ul>")
(def htree
(-> html
(h/parse)
(h/as-hickory)))
(select-between (s/class "current")
(s/class "l")
s/element
htree) Returns: [{:type :element,
:attrs nil,
:tag :li,
:content
[{:type :element,
:attrs {:href "/pc-game-pass/games?page=2", :rel "nofollow"},
:tag :a,
:content ["2"]}]}
{:type :element,
:attrs {:class "h-ellip"},
:tag :li,
:content
[{:type :element, :attrs nil, :tag :span, :content ["..."]}]}
{:type :element,
:attrs nil,
:tag :li,
:content
[{:type :element,
:attrs {:href "/pc-game-pass/games?page=3", :rel "nofollow"},
:tag :a,
:content ["3"]}]}] The question is likely a common one, I had it myself when I started using Hickory recently, so I wonder if it would be worth putting this somewhere. Since its a little less general than the typical selectors perhaps adding an |
I struggle to figure out how to select all elements between two elements such as:
And I want to select all
li
elements between the one of classcurrent
and the one of classl
using hickory selectors, so that I get back:How do you do that?
Thank You
The text was updated successfully, but these errors were encountered: