This commit is contained in:
2026-03-10 14:30:29 -06:00
parent e20dcf591d
commit a35453c2be
4 changed files with 81 additions and 48 deletions

View File

@@ -37,10 +37,12 @@
(if (zero? (:exit r)) (if (zero? (:exit r))
(-> r :out (json/parse-string (comp keyword camel->kebab)))))) (-> r :out (json/parse-string (comp keyword camel->kebab))))))
(declare gather-first-section) (declare gather-first-section gather-latex-paragraphs)
(defn read-string [s & {:keys [post-processors] (defn read-string
:or {post-processors [gather-first-section]}}] [s & {:keys [post-processors]
:or {post-processors [gather-first-section
gather-latex-paragraphs]}}]
(let [apply-post-processors (apply comp (reverse post-processors))] (let [apply-post-processors (apply comp (reverse post-processors))]
(with-in-str s (with-in-str s
(-> (uniorg :in *in*) (-> (uniorg :in *in*)
@@ -212,25 +214,6 @@
rest)] rest)]
(assoc node :children new-children))) (assoc node :children new-children)))
(defn- neighbourly-mapcat [coll f]
(let [rest-coll (rest coll)]
(map f
coll
rest-coll
(concat (rest rest-coll) [nil]))))
(defn- =>> [wa-loc wa-loc->b]
(if-some [loc (z/down wa-loc)]
(loop [loc-read loc
loc-write loc]
(let [loc-write' (z/replace loc-write (wa-loc->b loc-read))
next-loc-read (z/next loc-read)
next-loc-write (z/next loc-write')]
(if (z/end? next-loc-read)
(z/up loc-write')
(recur next-loc-read next-loc-write))))
wa-loc))
(defn separated-by-explicit-paragraph-break? (defn separated-by-explicit-paragraph-break?
"Returh truthy if each successive pair of elements is separated by "Returh truthy if each successive pair of elements is separated by
at least one explicit paragraph break; i.e. a blank line." at least one explicit paragraph break; i.e. a blank line."
@@ -257,30 +240,43 @@
(def doc (read-string (slurp some-org-file))) (def doc (read-string (slurp some-org-file)))
(let [r (atom []) (let [r (atom [])
blah blah]
@r))
(defn gather-latex-paragraphs [node]
(->> node
(sp/transform (sp/transform
[postorder-walker [postorder-walker (sp/must :children)]
(sp/must :children)]
(fn [children] (fn [children]
(loop [acc [] (loop [acc []
cs (vec children)] cs (vec children)]
(match cs (match cs
;; CASE: A paragraph followed by a LaTeX environment
;; followed by a paragraph. If there are no blank lines
;; separating the three elements, absorb them into a
;; single paragraph spanning the sum of their parts.
([(para :guard #(of-type? % "paragraph")) ([(para :guard #(of-type? % "paragraph"))
(tex :guard #(of-type? % "latex-environment")) (tex :guard #(of-type? % "latex-environment"))
(para :guard #(of-type? % "paragraph")) (para :guard #(of-type? % "paragraph"))
& rest] & rest]
:guard #(apply separated-by-explicit-paragraph-break? %)) :guard #(apply separated-by-explicit-paragraph-break? %))
(recur (conj acc (swallow para tex para)) rest) (recur (conj acc
;; Swallow para₂'s /children/,
;; not para₂ itself. Nested
;; paragraphs are not supported
;; by HTML.
(apply swallow para tex (:children para)))
rest)
;; CASE: A paragraph followed by a LaTeX environment.
;; If there are no blank lines separating the paragraph
;; from the LaTeX environment, the LaTeX environment
;; shall become a child of the paragraph.
([(para :guard #(of-type? % "paragraph")) ([(para :guard #(of-type? % "paragraph"))
(tex :guard #(of-type? % "latex-environment")) (tex :guard #(of-type? % "latex-environment"))
& rest] & rest]
:guard #(apply separated-by-explicit-paragraph-break? %)) :guard #(apply separated-by-explicit-paragraph-break? %))
(recur (conj acc (swallow para tex)) rest) (recur (conj acc (swallow para tex)) rest)
;; CASE: Irrelevant or empty!
[c & rest] [c & rest]
(recur (conj acc c) rest) (recur (conj acc c) rest)
[] acc))) [] acc))))))
doc)]
@r))
(defn gather-latex-paragraphs [node]
())

View File

@@ -49,3 +49,26 @@
first-paragraph-belongs-to-first-section?)) first-paragraph-belongs-to-first-section?))
(t/is (not (-> (parse-resource "first-paragraph-under-heading.org") (t/is (not (-> (parse-resource "first-paragraph-under-heading.org")
first-paragraph-belongs-to-first-section?))))) first-paragraph-belongs-to-first-section?)))))
(defn- first-paragraph-ends-with-latex? [doc]
(-> (sp/select-first [sut/postorder-walker
#(sut/of-type? % "paragraph")
(sp/must :children)
sp/LAST]
doc)
(sut/of-type? "latex-environment")))
(defn- first-paragraph-has-latex? [doc]
(sp/select-first [sut/postorder-walker
#(sut/of-type? % "paragraph")
(sp/must :children)
#(sut/of-type? % "latex-environment")]
doc))
(t/deftest paragraph-separation
(t/testing "paragraph ending with latex"
(t/is (->> (parse-resource "paragraph-ending-with-latex.org")
first-paragraph-ends-with-latex?)))
(t/testing "paragraph surrounding latex"
(t/is (->> (parse-resource "paragraph-surrounding-latex.org")
first-paragraph-has-latex?))))

View File

@@ -0,0 +1,7 @@
#+title: paragraph ending with latex
here is the paragraph,
\begin{align*}
\text{and here} &
\\ & \text{is the \LaTeX}
\end{align*}

View File

@@ -0,0 +1,7 @@
#+title: paragraph surrounding latex
first part of paragraph
\begin{equation*}
\text{some \LaTeX \}:)}
\end{equation*}
last part of paragraph