From 7805de06f2f440235f331facabafe8940874444e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madeleine=20Sydney=20=C5=9Alaga?= Date: Tue, 17 Mar 2026 21:24:36 -0600 Subject: [PATCH] fix: latex paragraphs again lol --- .../net/deertopia/doerg/tuftesque.css | 7 ++ doerg/src/net/deertopia/doerg/element.clj | 64 ++++++++++----- doerg/src/net/deertopia/doerg/render.clj | 2 - .../test/net/deertopia/doerg/element_test.clj | 80 ++++++++++++------- ...graph-ending-in-bold-surrounding-latex.org | 7 ++ .../paragraph-surrounding-separate-latex.org | 9 +++ .../paragraph-with-separate-latex.org | 7 ++ 7 files changed, 127 insertions(+), 49 deletions(-) create mode 100644 doerg/test/net/deertopia/doerg/element_test/paragraph-ending-in-bold-surrounding-latex.org create mode 100644 doerg/test/net/deertopia/doerg/element_test/paragraph-surrounding-separate-latex.org create mode 100644 doerg/test/net/deertopia/doerg/element_test/paragraph-with-separate-latex.org diff --git a/doerg/resources/net/deertopia/doerg/tuftesque.css b/doerg/resources/net/deertopia/doerg/tuftesque.css index 6fa781d..3291395 100644 --- a/doerg/resources/net/deertopia/doerg/tuftesque.css +++ b/doerg/resources/net/deertopia/doerg/tuftesque.css @@ -554,4 +554,11 @@ figure.fullwidth figcaption { ; align-items: center ; justify-content: center ; display: flex +; max-width: 55% +; width: 55% +} + +p > .latex-fragment.display-math +{ max-width: 100% +; width: 100% } diff --git a/doerg/src/net/deertopia/doerg/element.clj b/doerg/src/net/deertopia/doerg/element.clj index 7a03077..c14ca51 100644 --- a/doerg/src/net/deertopia/doerg/element.clj +++ b/doerg/src/net/deertopia/doerg/element.clj @@ -232,23 +232,35 @@ ;; TODO: Construct `:contents-begin` and `:contents-end` data ;; by spanning the children. first-section (merge {:type "section" - :children first-section-nodes} + :children (vec first-section-nodes)} (apply element-bounds first-section-nodes)) - new-children (concat top-level-nodes - (list first-section) - rest)] + new-children (vec (concat top-level-nodes + (list first-section) + rest))] (assoc node :children new-children))) -(defn separated-by-explicit-paragraph-break? - "Returh truthy if each successive pair of elements is separated by - at least one explicit paragraph break; i.e. a blank line." +(defn- newline-final-paragraph? + "Is `e` a paragraph, and does it end with a newline?" + [e] + (and (of-type? e "paragraph") + (some-> (-> e :position :end :column) + (= 1)))) + +(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] (match elements - [e₁ e₂ & es] - (and (< (-> e₁ :position :end :line) + ([(e₁ :guard newline-final-paragraph?) e₂ & es] :seq) + (and (= (-> e₁ :position :end :line) (-> e₂ :position :start :line)) (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 ([predator prey] @@ -259,6 +271,22 @@ ([predator 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] (->> node (sp/transform @@ -271,16 +299,14 @@ ;; 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")) - (tex :guard #(of-type? % "latex-environment")) - & rest] - :guard #(apply separated-by-explicit-paragraph-break? %)) + ([para tex & rest] :guard paragraph-followed-by-tex?) (recur acc (vec (cons (swallow para tex) rest))) - ;; CASE: - ([(para₁ :guard #(of-type? % "paragraph")) - (para₂ :guard #(of-type? % "paragraph")) - & rest] - :guard #(apply separated-by-explicit-paragraph-break? %)) + ;; CASE: Similar to the paragraph-followed-by-tex case, + ;; but instead of swallowing the entire second element, + ;; we swallow the /children/ of the second element, + ;; since paragraphs cannot be nested. + ([para₁ para₂ & rest] + :guard paragraph-followed-by-paragraph?) (recur acc (vec (cons (apply swallow para₁ (:children para₂)) rest))) ;; CASE: Irrelevant or empty! diff --git a/doerg/src/net/deertopia/doerg/render.clj b/doerg/src/net/deertopia/doerg/render.clj index c025f7a..1ec5d96 100644 --- a/doerg/src/net/deertopia/doerg/render.clj +++ b/doerg/src/net/deertopia/doerg/render.clj @@ -9,9 +9,7 @@ [net.deertopia.doerg.html :as doerg-html] [hiccup2.core :as hiccup] [clojure.pprint] - ;; #_ [net.deertopia.doerg.tex :as tex] - ;; [net.deertopia.doerg.tex.native :as tex-native] [net.deertopia.doerg.tex.temml :as tex-temml] [clojure.zip :as z] [babashka.fs :as fs])) diff --git a/doerg/test/net/deertopia/doerg/element_test.clj b/doerg/test/net/deertopia/doerg/element_test.clj index a275b9a..c917aad 100644 --- a/doerg/test/net/deertopia/doerg/element_test.clj +++ b/doerg/test/net/deertopia/doerg/element_test.clj @@ -11,7 +11,8 @@ (defn- parse-resource [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/testing "known greater elements satisfy `greater-element?`" @@ -44,32 +45,47 @@ 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?))))) + (t/is (-> (parse-resource "first-paragraph-under-first-section.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)]) -(defn- paragraph-ends-with-latex? [doc] - (let [type (-> (sp/select-first [(walk-types "paragraph") +(t/deftest paragraph-ending-with-latex + (let [doc (parse-resource "paragraph-ending-with-latex.org") + type (-> (sp/select-first [(walk-types "paragraph") (sp/must :children) sp/LAST] doc) sut/type)] - (t/is type "latex-environment"))) + (t/is (= "latex-environment" type)))) -(defn- paragraph-has-latex? [doc] - (t/is (sp/select-first [(walk-types "paragraph") - (sp/must :children) - sp/ALL - #(sut/of-type? % "latex-environment")] - doc))) +(t/deftest paragraph-surrounding-latex + (let [doc (parse-resource "paragraph-surrounding-latex.org") + children (->> doc + (sp/select-first [(walk-types "paragraph")]) + :children + (map sut/type))] + (t/is (= ["text" "latex-environment" "text"] + children)))) -(defn- paragraph-has-multiple-latex? [doc] - (let [paragraphs (sp/select (walk-types "paragraph") doc)] +(t/deftest paragraph-ending-in-bold-surrounding-latex + (let [doc (parse-resource "paragraph-ending-in-bold-surrounding-latex.org") + children (->> doc + (sp/select-first [(walk-types "paragraph")]) + :children + (map sut/type))] + (t/is (= ["text" "bold" "latex-environment" "text"] + children)))) + +(t/deftest paragraph-with-multiple-latex + (let [doc (parse-resource "paragraph-with-multiple-latex.org") + paragraphs (sp/select (walk-types "paragraph") doc)] (t/is (= 2 (count paragraphs))) (let [[p₁ p₂] paragraphs] (doseq [[p ts] [[p₁ ["text" "latex-environment" @@ -79,13 +95,21 @@ (t/is (= ts (sp/select [(sp/must :children) sp/ALL (sp/view sut/type)] p))))))) -(t/deftest paragraph-separation - (t/testing "paragraph ending with latex" - (-> (parse-resource "paragraph-ending-with-latex.org") - paragraph-ends-with-latex?)) - (t/testing "paragraph surrounding latex" - (-> (parse-resource "paragraph-surrounding-latex.org") - paragraph-has-latex?)) - (t/testing "paragraph with interleaved latex" - (-> (parse-resource "paragraph-with-multiple-latex.org") - paragraph-has-multiple-latex?))) +(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)))) + diff --git a/doerg/test/net/deertopia/doerg/element_test/paragraph-ending-in-bold-surrounding-latex.org b/doerg/test/net/deertopia/doerg/element_test/paragraph-ending-in-bold-surrounding-latex.org new file mode 100644 index 0000000..f0731d6 --- /dev/null +++ b/doerg/test/net/deertopia/doerg/element_test/paragraph-ending-in-bold-surrounding-latex.org @@ -0,0 +1,7 @@ +#+title: bold-final paragraph surrounding latex + +first part of *paragraph* +\begin{equation*} +\text{some \LaTeX \}:)} +\end{equation*} +last part of paragraph diff --git a/doerg/test/net/deertopia/doerg/element_test/paragraph-surrounding-separate-latex.org b/doerg/test/net/deertopia/doerg/element_test/paragraph-surrounding-separate-latex.org new file mode 100644 index 0000000..024e968 --- /dev/null +++ b/doerg/test/net/deertopia/doerg/element_test/paragraph-surrounding-separate-latex.org @@ -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 diff --git a/doerg/test/net/deertopia/doerg/element_test/paragraph-with-separate-latex.org b/doerg/test/net/deertopia/doerg/element_test/paragraph-with-separate-latex.org new file mode 100644 index 0000000..08c3fda --- /dev/null +++ b/doerg/test/net/deertopia/doerg/element_test/paragraph-with-separate-latex.org @@ -0,0 +1,7 @@ +#+title: paragraph with separate latex + +a paragraph! + +\begin{gather*} +\text{and now, an unrelated latex fragment} +\end{gather*}