选择 div.content 中的所有内容,然后根据标签对它们进行分区。
这里有一个更一般的概念,通过识别哪些事物是分隔符而哪些不是,将一系列事物分成段:
(defn separate*
"Produces a sequence of (parent child*)*, coll must start with a parent"
[child? coll]
(lazy-seq
(when-let [s (seq coll)]
(let [run (cons (first s)
(take-while child? (next s)))]
(cons run (separate* child? (drop (count run) s)))))))
与 非常相似partition-by
,但总是在父节点上分裂:
(partition-by keyword? [:foo 1 2 3 :bar :baz 4 5])
;; => ((:foo) (1 2 3) (:bar :baz) (4 5))
(separate* (compliment keyword?) [:foo 1 2 3 :bar :baz 4 5])
;; => ((:foo 1 2 3) (:bar) (:baz 4 5))
如果要在没有前导标题时处理:
(defn separate
[parent? coll]
(when-let [s (seq coll)]
(if (parent? (first coll))
(separate* (complement parent?) coll)
(let [child? (complement parent?)
run (take-while child? s)]
(cons (cons nil run)
(separate* child? (drop (count run) s)))))))
(separate keyword? [1 2 :foo 3 4])
;; => ((nil 1 2) (:foo 3 4))
回到手头的问题:
(def x [{:tag :h3 :content "1"}
{:tag :div :content "A"}
{:tag :div :content "B"}
{:tag :h3 :content "2"}
{:tag :div :content "C"}
{:tag :div :content "D"}])
(def sections (separate #(= :h3 (:tag %)) x))
=> (({:content "1", :tag :h3}
{:content "A", :tag :div
{:content "B", :tag :div})
({:content "2", :tag :h3}
{:content "C", :tag :div}
{:content "D", :tag :div}))
如果我们不想保留 h3 标题的内容:
(map rest sections)
=> (({:content "A", :tag :div} {:content "B", :tag :div})
({:content "C", :tag :div} {:content "D", :tag :div}))