From 3ce92ed3bb92ad328ab808ebd1d7e7f94a009f1d Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Mon, 23 Sep 2024 18:18:30 +0200 Subject: [PATCH] Don't use AOT-ed classes --- .dir-locals.el | 2 + src/nextjournal/markdown.cljc | 8 ++- src/nextjournal/markdown/impl.clj | 13 ++-- src/nextjournal/markdown/impl/extensions.clj | 17 +++-- src/nextjournal/markdown/impl/types.clj | 72 +++++++++++--------- 5 files changed, 63 insertions(+), 49 deletions(-) create mode 100644 .dir-locals.el diff --git a/.dir-locals.el b/.dir-locals.el new file mode 100644 index 0000000..8422ce9 --- /dev/null +++ b/.dir-locals.el @@ -0,0 +1,2 @@ +((clojure-mode + (cider-clojure-cli-aliases . ":test:repl"))) diff --git a/src/nextjournal/markdown.cljc b/src/nextjournal/markdown.cljc index 78a4027..9ae0156 100644 --- a/src/nextjournal/markdown.cljc +++ b/src/nextjournal/markdown.cljc @@ -1,8 +1,10 @@ (ns nextjournal.markdown "Markdown as data" - (:require [nextjournal.markdown.impl :as impl] - [nextjournal.markdown.utils :as u] - [nextjournal.markdown.transform :as markdown.transform])) + (:require + [nextjournal.markdown.impl.types] + [nextjournal.markdown.impl :as impl] + [nextjournal.markdown.utils :as u] + [nextjournal.markdown.transform :as markdown.transform])) (def empty-doc u/empty-doc) diff --git a/src/nextjournal/markdown/impl.clj b/src/nextjournal/markdown/impl.clj index 37697c8..c84edfc 100644 --- a/src/nextjournal/markdown/impl.clj +++ b/src/nextjournal/markdown/impl.clj @@ -1,7 +1,7 @@ ;; # 🧩 Parsing (ns nextjournal.markdown.impl (:require [clojure.zip :as z] - [nextjournal.markdown.impl.types] + [nextjournal.markdown.impl.types :as t] [nextjournal.markdown.impl.extensions :as extensions] [nextjournal.markdown.utils :as u]) (:import (org.commonmark.ext.autolink AutolinkExtension) @@ -30,8 +30,7 @@ SoftLineBreak HardLineBreak Image) - (org.commonmark.parser Parser) - (nextjournal.markdown.impl.types BlockFormula InlineFormula ToC))) + (org.commonmark.parser Parser))) (set! *warn-on-reflection* true) ;; TODO: @@ -219,10 +218,12 @@ ThematicBreak (swap! !ctx u/update-current-loc z/append-child {:type :ruler}) SoftLineBreak (swap! !ctx u/update-current-loc z/append-child {:type :softbreak}) HardLineBreak (swap! !ctx u/update-current-loc z/append-child {:type :hardbreak}) - ToC (swap! !ctx u/update-current-loc z/append-child {:type :toc}) TaskListItemMarker (swap! !ctx u/update-current-loc handle-todo-list node) - InlineFormula (swap! !ctx u/update-current-loc z/append-child {:type :formula :text (.getLiteral ^InlineFormula node)}) - BlockFormula (swap! !ctx u/update-current-loc z/append-child {:type :block-formula :text (.getLiteral ^BlockFormula node)}) + nextjournal.markdown.impl.types.CustomNode + (case (t/nodeType node) + :block-formula (swap! !ctx u/update-current-loc z/append-child {:type :block-formula :text (t/getLiteral node)}) + :inline-formula (swap! !ctx u/update-current-loc z/append-child {:type :formula :text (t/getLiteral node)}) + :toc (swap! !ctx u/update-current-loc z/append-child {:type :toc})) FootnoteReference (swap! !ctx (fn [{:as ctx ::keys [label->footnote-ref]}] (let [label (.getLabel ^FootnoteReference node) footnote-ref (or (get label->footnote-ref label) diff --git a/src/nextjournal/markdown/impl/extensions.clj b/src/nextjournal/markdown/impl/extensions.clj index 2db1895..b3bbfaf 100644 --- a/src/nextjournal/markdown/impl/extensions.clj +++ b/src/nextjournal/markdown/impl/extensions.clj @@ -1,8 +1,7 @@ (ns nextjournal.markdown.impl.extensions (:require [clojure.string :as str] - [nextjournal.markdown.impl]) + [nextjournal.markdown.impl.types :as t]) (:import (java.util.regex Matcher Pattern) - (nextjournal.markdown.impl.types InlineFormula BlockFormula ToC) (org.commonmark.parser Parser$ParserExtension Parser$Builder SourceLine) (org.commonmark.parser.beta InlineContentParser InlineContentParserFactory ParsedInline InlineParserState) (org.commonmark.parser.block AbstractBlockParser BlockContinue BlockParserFactory BlockStart ParserState BlockParser))) @@ -31,7 +30,7 @@ (ParsedInline/none) (let [^String content (.getContent (.getSource scanner open-pos (.position scanner)))] (.next scanner) - (ParsedInline/of (new InlineFormula content) (.position scanner)))))))) + (ParsedInline/of (t/->InlineFormula content) (.position scanner)))))))) (defn close-block-formula? [state !lines] ;; we allow 1-liner blocks like A) @@ -48,7 +47,7 @@ #_B (some? (re-find (block-formula-delimiter-matcher state))))) (defn block-formula-parser ^BlockParser [] - (let [block-formula (new BlockFormula) + (let [block-formula (t/->BlockFormula) !lines (atom [])] (proxy [AbstractBlockParser] [] (isContainer [] false) @@ -58,10 +57,10 @@ (when-some [l (not-empty (str/trim (.getContent line)))] (swap! !lines conj l))) (closeBlock [] - (.setLiteral block-formula (let [formula-body (str/join \newline @!lines)] - (cond-> formula-body - (str/ends-with? formula-body "$$") - (subs 0 (- (count formula-body) 2)))))) + (t/setLiteral block-formula (let [formula-body (str/join \newline @!lines)] + (cond-> formula-body + (str/ends-with? formula-body "$$") + (subs 0 (- (count formula-body) 2)))))) (tryContinue [^ParserState state] (let [non-space (.getNextNonSpaceIndex state)] (if (close-block-formula? state !lines) @@ -81,7 +80,7 @@ (BlockStart/none))))))) (defn block-toc-parser ^BlockParser [] - (let [toc (new ToC)] + (let [toc (t/->ToC)] (proxy [AbstractBlockParser] [] (getBlock [] toc) ;; close immediately diff --git a/src/nextjournal/markdown/impl/types.clj b/src/nextjournal/markdown/impl/types.clj index d8426be..6f9f4de 100644 --- a/src/nextjournal/markdown/impl/types.clj +++ b/src/nextjournal/markdown/impl/types.clj @@ -3,36 +3,46 @@ ;; See also ;; https://github.com/noties/Markwon/blob/master/markwon-ext-latex/src/main/java/io/noties/markwon/ext/latex/JLatexMathBlockParser.java -(gen-class - :name nextjournal.markdown.impl.types.InlineFormula - :extends org.commonmark.node.CustomNode - :constructors {[String] []} - :init init - :state state - :methods [[getLiteral [] String]] - :prefix "inline-formula-") - -(defn inline-formula-init [lit] [[] (ref {:literal lit})]) -(defn inline-formula-getLiteral [this] (:literal @(.state this))) - -(gen-class - :name nextjournal.markdown.impl.types.BlockFormula - :extends org.commonmark.node.CustomBlock - :constructors {[] []} - :init init - :state state - :prefix "block-formula-" - :methods [[getLiteral [] String] - [setLiteral [String] String]]) - -(defn block-formula-init [] [[] (atom nil)]) -(defn block-formula-getLiteral [this] @(.state this)) -(defn block-formula-setLiteral [this val] (reset! (.state this) val)) - -(gen-class - :name nextjournal.markdown.impl.types.ToC - :extends org.commonmark.node.CustomBlock - :constructors {[] []}) +(set! *warn-on-reflection* true) + +(definterface CustomNode + (getLiteral []) + (setLiteral [v]) + (nodeType [])) + +(defn ->InlineFormula [lit] + (let [state (atom lit)] + (proxy [org.commonmark.node.CustomNode nextjournal.markdown.impl.types.CustomNode] [] + (getLiteral [] @state) + (nodeType [] :inline-formula)))) + +(defn ->BlockFormula + ([] (->BlockFormula nil)) + ([lit] + (let [state (atom lit)] + (proxy [org.commonmark.node.CustomBlock nextjournal.markdown.impl.types.CustomNode] [] + (getLiteral [] @state) + (setLiteral [v] (do (reset! state v) + this)) + (nodeType [] :block-formula))))) + +(defn ->ToC [] + (proxy [org.commonmark.node.CustomBlock nextjournal.markdown.impl.types.CustomNode] [] + (nodeType [] :toc))) + +(defn setLiteral [^CustomNode n lit] + (.setLiteral n lit)) + +(defn getLiteral [^CustomNode n] + (.getLiteral n)) + +(defn nodeType [^CustomNode n] + (.nodeType n)) (comment - (compile 'nextjournal.markdown.impl.types)) + (def i (->InlineFormula "1+1")) + (instance? nextjournal.markdown.impl.types.CustomNode i) + (let [b (->BlockFormula)] + (-> (setLiteral b "dude") + (getLiteral))) + )