specter and first section

This commit is contained in:
2026-02-06 12:15:49 -07:00
parent 5024b0f078
commit 2735469b07
8 changed files with 151 additions and 44 deletions

View File

@@ -3,7 +3,8 @@
[babashka.process :as p]
[clojure.test :as t]
[clojure.zip :as z]
[clojure.java.io :as io]))
[clojure.java.io :as io]
[com.rpl.specter :as sp]))
(defn sleep-vs-timeout [& {:keys [sleep timeout]}]
(sut/deref-with-timeout
@@ -36,12 +37,17 @@
(defn- first-child-of-type [parent type]
(some #(and (sut/of-type? % type) %) (:children parent)))
(defn- parse-resource [path]
(-> (str "net/deertopia/doerg/element_test/" path)
io/resource slurp sut/read-string))
(t/deftest known-greater-elements
(t/testing "known greater elements satisfy `greater-element?`"
(let [s (-> "net/deertopia/doerg/element_test/greater-elements.org"
io/resource slurp)
root (sut/read-string s)
section (first-child-of-type root "section")
(let [root (parse-resource "greater-elements.org")
section (->> root
(sp/select [sut/children-walker
#(sut/of-type? % "section")])
second)
headline (first-child-of-type section "headline")
headline-text (first-child-of-type headline "text")
paragraph (first-child-of-type section "paragraph")
@@ -52,3 +58,22 @@
(t/is (not (sut/greater-element? headline-text)))
(t/is (sut/greater-element? paragraph))
(t/is (not (sut/greater-element? paragraph-text))))))
(defn- first-paragraph-belongs-to-first-section? [doc]
(let [first-paragraph (sp/select-first [sut/postorder-walker
#(sut/of-type? % "paragraph")]
doc)
first-section (sp/select-first [sut/postorder-walker
#(sut/of-type? % "section")]
doc)]
(if (and first-paragraph first-section)
(boolean (some #(= % first-paragraph)
(:children first-section)))
true)))
(t/deftest first-paragraph-under-first-section
(t/testing "first paragraph should belong to a section"
(t/is (-> (parse-resource "first-paragraph-under-first-section.org")
first-paragraph-belongs-to-first-section?))
(t/is (not (-> (parse-resource "first-paragraph-under-heading.org")
first-paragraph-belongs-to-first-section?)))))

View File

@@ -0,0 +1,7 @@
#+title: first paragraph under first section
first paragraph is here and not under the first heading
* first heading
second paragraph

View File

@@ -0,0 +1,5 @@
#+title: first paragraph under a heading
* first heading
first paragraph is here and not in the first section