Compare commits

..

1 Commits

Author SHA1 Message Date
fa42052a6a refactor: split tex modules 2026-02-27 18:21:10 -07:00
3 changed files with 28 additions and 26 deletions

View File

@@ -11,8 +11,8 @@
[clojure.pprint]
#_
[net.deertopia.doerg.tex :as tex]
[net.deertopia.doerg.tex.native :as tex]
[net.deertopia.doerg.tex.temml :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]))
@@ -61,7 +61,7 @@
(defn org-document
"Recursively render an Org-mode document to Hiccup."
[doc]
(tex/binding-temml-worker
(tex-temml/binding-worker
(let [rendered (-> doc gather-footnotes render-tex-snippets
org-element-recursive)]
[:html
@@ -176,11 +176,11 @@
(let [rendered-snippets
(delay (->> @promises
(map #(-> % :node :value))
(apply tex/render-xelatex svg-dir)))]
(apply tex-native/render svg-dir)))]
(doseq [{:keys [promise node]} @promises]
(try (let [{:keys [value]} node
temml (tex/render-temml value)]
(if (tex/erroneous-temml-output? temml)
temml (tex-temml/render value)]
(if (tex-temml/erroneous-output? temml)
(let [tex (get @rendered-snippets value)]
(if (:errors tex)
(deliver promise (hiccup/raw temml))

View File

@@ -43,8 +43,8 @@
(-> r
(assoc ::out out))))
(defn- parse-tightpage [xelatex-out]
(->> (re-find tightpage-regexp xelatex-out)
(defn- parse-tightpage [latex-out]
(->> (re-find tightpage-regexp latex-out)
(drop 1)
(map parse-long)))
@@ -59,7 +59,7 @@
scale-divisor
font-size)}))
(defn- parse-output [out]
(defn- parse-latex-output [out]
(let [tightpage-info (parse-tightpage out)
m-start (re-matcher preview-start-regexp out)
m-end (re-matcher preview-end-regexp out)]
@@ -80,10 +80,11 @@
errors))))))
acc))))
(defn- invoke-xelatex [& {:keys [file output-dir]}]
(defn- invoke-latex [& {:keys [file output-dir latex]
:or {latex "xelatex"}}]
(invoke
{:dir output-dir}
"xelatex" "-no-pdf" "-interaction" "nonstopmode"
latex "-no-pdf" "-interaction" "nonstopmode"
"-output-directory" output-dir file))
(defn- invoke-dvisvgm [& {:keys [file output-dir]}]
@@ -112,25 +113,26 @@
slurp
(str/replace-first "% {{contents}}" contents))))
(defn render-xelatex
"Render a collection of `snippets` to SVGs in `output-dir` using
XeLaTeX and dvisvgm. Returns a map whose keys are `snippets` and
whose values are maps containing geometry info, a string of errors
output by XeLaTeX, and the path to the generated SVG file. Math
delimiters are *not* implicitly added to each snippet."
(defn render
"Render a collection of `snippets` to SVGs in `output-dir` using a
LaTeX engine (XeLaTeX at the moment) and dvisvgm. Returns a map
whose keys are `snippets` and whose values are maps containing
geometry info, a string of errors output by LaTeX, and the path to
the generated SVG file. Math delimiters are *not* implicitly added
to each snippet."
[output-dir & snippets]
(fs/with-temp-dir [dir {:prefix "doerg-xelatex"}]
(fs/with-temp-dir [dir {:prefix "doerg-latex"}]
(let [preview-tex (fs/file dir "preview.tex")
preview-xdv (fs/file dir "preview.xdv")
distinct-snippets (distinct snippets)]
(fs/create-dirs output-dir)
(->> (instantiate-preview-template distinct-snippets)
(spit preview-tex))
(let [dimensions (-> (invoke-xelatex :output-dir dir :file preview-tex)
::out parse-xelatex-output)
(let [dimensions (-> (invoke-latex :output-dir dir :file preview-tex)
::out parse-latex-output)
_ (invoke-dvisvgm :output-dir output-dir :file preview-xdv)]
;; Adorn each snippet with dimensions and errors parsed from
;; XeLaTeX's output, and the paths to SVG files generated by
;; LaTeX's output, and the paths to SVG files generated by
;; dvisvgm.
(assert (= (count distinct-snippets) (count dimensions)))
(->> (map (fn [ix snippet dimensions]
@@ -145,8 +147,8 @@
(into {}))))))
(comment
(render-xelatex "/tmp/doerg-tex-svgs"
"\\(c = \\sqrt{x^2 + y^2}\\)"
"\\(x\\)" "\\(y\\)" "\\(x\\)"
"\\(\\undefinedcommandlol\\)"))
(render "/tmp/doerg-tex-svgs"
"\\(c = \\sqrt{x^2 + y^2}\\)"
"\\(x\\)" "\\(y\\)" "\\(x\\)"
"\\(\\undefinedcommandlol\\)"))

View File

@@ -53,7 +53,7 @@
(defn render-display [s]
(command-worker [s]))
(defn render-temml [s]
(defn render [s]
(if-let [[_ inner] (re-matches #"(?s)\\[(.*)\\]" s)]
(render-display inner)
(if (re-matches #"(?s)\\begin\{.+?}(.*?)\\end\{.+?}" s)