wip: fix: latex paragraphs again lol
All checks were successful
build / build (push) Successful in 1m8s

This commit is contained in:
2026-03-17 21:24:36 -06:00
parent 1629efb378
commit c7067b867c
4 changed files with 112 additions and 39 deletions

View File

@@ -232,23 +232,34 @@
;; TODO: Construct `:contents-begin` and `:contents-end` data ;; TODO: Construct `:contents-begin` and `:contents-end` data
;; by spanning the children. ;; by spanning the children.
first-section (merge {:type "section" first-section (merge {:type "section"
:children first-section-nodes} :children (vec first-section-nodes)}
(apply element-bounds first-section-nodes)) (apply element-bounds first-section-nodes))
new-children (concat top-level-nodes new-children (vec (concat top-level-nodes
(list first-section) (list first-section)
rest)] rest))]
(assoc node :children new-children))) (assoc node :children new-children)))
(defn separated-by-explicit-paragraph-break? (defn- newline-final-paragraph?
"Returh truthy if each successive pair of elements is separated by "Is `e` a paragraph, and does it end with a newline?"
at least one explicit paragraph break; i.e. a blank line." [e]
(and (of-type? e "paragraph")
(some-> e :children last :value (str/ends-with? "\n"))))
(defn consequtive-elements?
"Returh truthy if each successive pair of elements is NOT separated
by at least one explicit paragraph break; i.e. a blank line."
[& elements] [& elements]
(match elements (match elements
[e e & es] ([(e :guard newline-final-paragraph?) e & es] :seq)
(and (< (-> e :position :end :line) (and (= (-> e :position :end :line)
(-> e :position :start :line)) (-> e :position :start :line))
(recur es)) (recur es))
:else true)) ([e e & es] :seq)
(and (= (-> e :position :end :line inc)
(-> e :position :start :line))
(recur es))
([_] :seq) true
([] :seq) true))
(defn swallow (defn swallow
([predator prey] ([predator prey]
@@ -259,6 +270,22 @@
([predator prey & more-prey] ([predator prey & more-prey]
(reduce swallow predator (cons prey more-prey)))) (reduce swallow predator (cons prey more-prey))))
(defn- paragraph-followed-by-tex? [children]
(match children
[(para :guard #(of-type? % "paragraph"))
(tex :guard #(of-type? % "latex-environment"))
& _]
(consequtive-elements? para tex)
:else false))
(defn- paragraph-followed-by-paragraph? [children]
(match children
[(para :guard #(of-type? % "paragraph"))
(para :guard #(of-type? % "paragraph"))
& _]
(consequtive-elements? para para)
:else false))
(defn gather-latex-paragraphs [node] (defn gather-latex-paragraphs [node]
(->> node (->> node
(sp/transform (sp/transform
@@ -271,16 +298,14 @@
;; If there are no blank lines separating the paragraph ;; If there are no blank lines separating the paragraph
;; from the LaTeX environment, the LaTeX environment ;; from the LaTeX environment, the LaTeX environment
;; shall become a child of the paragraph. ;; shall become a child of the paragraph.
([(para :guard #(of-type? % "paragraph")) ([para tex & rest] :guard paragraph-followed-by-tex?)
(tex :guard #(of-type? % "latex-environment"))
& rest]
:guard #(apply separated-by-explicit-paragraph-break? %))
(recur acc (vec (cons (swallow para tex) rest))) (recur acc (vec (cons (swallow para tex) rest)))
;; CASE: ;; CASE: Similar to the paragraph-followed-by-tex case,
([(para :guard #(of-type? % "paragraph")) ;; but instead of swallowing the entire second element,
(para :guard #(of-type? % "paragraph")) ;; we swallow the /children/ of the second element,
& rest] ;; since paragraphs cannot be nested.
:guard #(apply separated-by-explicit-paragraph-break? %)) ([para para & rest]
:guard paragraph-followed-by-paragraph?)
(recur acc (vec (cons (apply swallow para (:children para)) (recur acc (vec (cons (apply swallow para (:children para))
rest))) rest)))
;; CASE: Irrelevant or empty! ;; CASE: Irrelevant or empty!

View File

@@ -11,7 +11,8 @@
(defn- parse-resource [path] (defn- parse-resource [path]
(-> (str "net/deertopia/doerg/element_test/" path) (-> (str "net/deertopia/doerg/element_test/" path)
io/resource slurp sut/read-string)) io/resource slurp
(sut/read-string)))
(t/deftest known-greater-elements (t/deftest known-greater-elements
(t/testing "known greater elements satisfy `greater-element?`" (t/testing "known greater elements satisfy `greater-element?`"
@@ -44,41 +45,66 @@
true))) true)))
(t/deftest first-paragraph-under-first-section (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")
(t/is (-> (parse-resource "first-paragraph-under-first-section.org") first-paragraph-belongs-to-first-section?)))
first-paragraph-belongs-to-first-section?))
(t/is (not (-> (parse-resource "first-paragraph-under-heading.org")
first-paragraph-belongs-to-first-section?)))))
(defn walk-types [type & types] (t/deftest first-paragraph-under-heading
(t/is (-> (parse-resource "first-paragraph-under-heading.org")
first-paragraph-belongs-to-first-section?
not)))
(defn- walk-types [type & types]
[sut/postorder-walker #(apply sut/of-type? % type types)]) [sut/postorder-walker #(apply sut/of-type? % type types)])
(defn- paragraph-ends-with-latex? [doc] (t/deftest paragraph-ending-with-latex
(let [type (-> (sp/select-first [(walk-types "paragraph") (let [doc (parse-resource "paragraph-ending-with-latex.org")
type (-> (sp/select-first [(walk-types "paragraph")
(sp/must :children) (sp/must :children)
sp/LAST] sp/LAST]
doc) doc)
sut/type)] sut/type)]
(t/is type "latex-environment"))) (t/is type "latex-environment")))
(defn- paragraph-has-latex? [doc] (t/deftest paragraph-surrounding-latex
(t/is (sp/select-first [(walk-types "paragraph") (let [doc (parse-resource "paragraph-surrounding-latex.org")]
(sp/must :children) (t/is (sp/select-first [(walk-types "paragraph")
sp/ALL (sp/must :children)
#(sut/of-type? % "latex-environment")] sp/ALL
doc))) #(sut/of-type? % "latex-environment")]
doc))))
(defn- paragraph-has-multiple-latex? [doc] (t/deftest paragraph-with-multiple-latex
(let [paragraphs (sp/select (walk-types "paragraph") doc)] (let [doc (parse-resource "paragraph-with-multiple-latex.org")
paragraphs (sp/select (walk-types "paragraph") doc)]
(t/is (= 2 (count paragraphs))) (t/is (= 2 (count paragraphs)))
(let [[p p] paragraphs] (let [[p p] paragraphs]
(doseq [[p ts] [[p ["text" "latex-environment" (doseq [[p ts] [[p ["text" "latex-environment"
"text" "latex-environment"]] "text" "latex-environment"]]
[p ["text" "latex-environment" [p ["text" "latex-environment"
"text" "latex-environment" "text"]]]] "text" "latex-environment" "text"]]]]
(t/is (= ts (sp/select [(sp/must :children) (t/is (= (sp/select [(sp/must :children)
sp/ALL (sp/view sut/type)] p))))))) sp/ALL (sp/view sut/type)] p)
ts))))))
(t/deftest paragraph-with-separate-latex
(let [doc (parse-resource "paragraph-with-separate-latex.org")
cs (sp/select [(walk-types "section")
(sp/must :children)
sp/ALL
(sp/view sut/type)]
doc)]
(t/is (= ["paragraph" "latex-environment"] cs))))
(t/deftest paragraph-surrounding-separate-latex
(let [doc (parse-resource "paragraph-surrounding-separate-latex.org")
cs (sp/select [(walk-types "section")
(sp/must :children)
sp/ALL
(sp/view sut/type)]
doc)]
(t/is (= ["paragraph" "latex-environment" "paragraph"] cs))))
#_
(t/deftest paragraph-separation (t/deftest paragraph-separation
(t/testing "paragraph ending with latex" (t/testing "paragraph ending with latex"
(-> (parse-resource "paragraph-ending-with-latex.org") (-> (parse-resource "paragraph-ending-with-latex.org")
@@ -88,4 +114,10 @@
paragraph-has-latex?)) paragraph-has-latex?))
(t/testing "paragraph with interleaved latex" (t/testing "paragraph with interleaved latex"
(-> (parse-resource "paragraph-with-multiple-latex.org") (-> (parse-resource "paragraph-with-multiple-latex.org")
paragraph-has-multiple-latex?))) paragraph-has-multiple-latex?))
(t/testing "paragraph with separate latex"
(-> (parse-resource "paragraph-with-separate-latex.org")
paragraph-has-separate-latex?))
#_(t/testing "paragraphs surrounding separate latex"
(-> (parse-resource "paragraph-surrounding-separate-latex.org")
paragraph-surrounding-separate-latex?)))

View File

@@ -0,0 +1,9 @@
#+title: paragraphs surrounding separate latex
a paragraph!
\begin{gather*}
\text{and now, an unrelated latex fragment}
\end{gather*}
more unrelated text

View File

@@ -0,0 +1,7 @@
#+title: paragraph with separate latex
a paragraph!
\begin{gather*}
\text{and now, an unrelated latex fragment}
\end{gather*}