diff --git a/doerg/src/net/deertopia/doerg/render.clj b/doerg/src/net/deertopia/doerg/render.clj index 81ccec0..a11abd9 100644 --- a/doerg/src/net/deertopia/doerg/render.clj +++ b/doerg/src/net/deertopia/doerg/render.clj @@ -127,6 +127,28 @@ element/footnotes-section?] sp/NONE)))) +(defn- read-and-patch-generated-svg [{:keys [file height depth]}] + ;; dvisvgm writes standalone SVG files, to which we need to make a + ;; few changes to use them inline within our HTML. + ;; • XML header: Bad syntax when embedded in an HTML doc. Remove + ;; it. + ;; • Width and height: We override these with our own values + ;; computed by `net.deertopia.doerg.tex` to ensure correct + ;; positioning relative to the surrounding text. More + ;; accurately, we remove the height and width attributes from + ;; the SVG tag, and set the new values for height and + ;; vertical-align in the style attribute + ;; • Viewbox: Must be removed entirely for correct positioning. + (-> (slurp file) + (str/replace-first "" "") + (str/replace-first #" height=['\"][^\"']+[\"']" "") + (str/replace-first #" width=['\"][^\"']+[\"']" "") + (str/replace-first + #"viewBox=['\"][^\"']+[\"']" + (format "style=\"%s\"" + (format "height:%.4fem;vertical-align:%.4f;display:inline-block" + height (- depth)))))) + (defn- render-tex-snippets [doc] (let [promises (atom []) r (->> doc (sp/transform @@ -148,11 +170,13 @@ (try (let [{:keys [value contents]} node temml (tex/render-temml (or contents value))] (if (tex/erroneous-temml-output? temml) - (let [{:keys [file]} (get @rendered-snippets value)] - (deliver - promise - [:div.latex-fragment - (hiccup/raw (slurp file))])) + (let [tex (get @rendered-snippets value)] + (if (:errors tex) + (deliver promise (hiccup/raw temml)) + (->> tex + read-and-patch-generated-svg + hiccup/raw + (deliver promise)))) (deliver promise (hiccup/raw temml)))) (catch Exception e (prn e)