Skip to content

Commit

Permalink
Don't use AOT-ed classes
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Sep 23, 2024
1 parent d7cec58 commit 3ce92ed
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .dir-locals.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
((clojure-mode
(cider-clojure-cli-aliases . ":test:repl")))
8 changes: 5 additions & 3 deletions src/nextjournal/markdown.cljc
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
13 changes: 7 additions & 6 deletions src/nextjournal/markdown/impl.clj
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 8 additions & 9 deletions src/nextjournal/markdown/impl/extensions.clj
Original file line number Diff line number Diff line change
@@ -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)))
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
72 changes: 41 additions & 31 deletions src/nextjournal/markdown/impl/types.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
)

0 comments on commit 3ce92ed

Please sign in to comment.