-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
(ns nextjournal.markdown.parser2.formulas | ||
(:import (nextjournal.markdown.parser2.types InlineFormula) | ||
(org.commonmark.node Node) | ||
(org.commonmark.internal InlineParserImpl) | ||
(org.commonmark.internal.inline InlineContentParser InlineParserState ParsedInline) | ||
(org.commonmark.parser InlineParserFactory Parser Parser$ParserExtension Parser$Builder))) | ||
|
||
(defn inline-formula-parser [] | ||
(proxy [InlineContentParser] [] | ||
(tryParse [^InlineParserState parser-state] | ||
|
||
(let [scanner (.scanner parser-state) | ||
dollars-open (.matchMultiple scanner \$) | ||
after-opening (.position scanner)] | ||
|
||
(if (< 0 (.find scanner \$)) | ||
(let [before-closing (.position scanner) | ||
dollars-close (.matchMultiple scanner \$)] | ||
(if (= dollars-open dollars-close) | ||
(let [^String source (.getContent (.getSource scanner after-opening before-closing))] | ||
(prn :source source) | ||
(ParsedInline/of (new InlineFormula source) (.position scanner))))) | ||
(ParsedInline/none)))))) | ||
|
||
(defn extension [] | ||
(proxy [Object Parser$ParserExtension] [] | ||
(extend [^Parser$Builder pb] | ||
(.inlineParserFactory pb (proxy [InlineParserFactory] [] | ||
(create [ctx] | ||
(.addInlineParser (new InlineParserImpl ctx) | ||
\$ (list (inline-formula-parser))))))))) | ||
|
||
(comment | ||
|
||
(nextjournal.markdown.parser2/parse " | ||
# Ok | ||
Aloha, that costs | ||
* a $\\int_a^b\\phi(t)dt$ with discount | ||
* and what")) |