Skip to content

Commit

Permalink
Prepare for footnote refs
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Apr 5, 2024
1 parent 60f5236 commit 567ca17
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 34 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified classes/nextjournal/markdown/parser2/types/Footnote.class
Binary file not shown.
Binary file not shown.
Binary file modified classes/nextjournal/markdown/parser2/types/InlineFormula.class
Binary file not shown.
Binary file modified classes/nextjournal/markdown/parser2/types__init.class
Binary file not shown.
5 changes: 1 addition & 4 deletions src/nextjournal/markdown/parser2.clj
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@
And what.
[^note1]: the _what_
* and new text[^endnote] at the end.
* the
* hell^[that warm place]
Expand All @@ -230,6 +229,4 @@ And what.

;; inline footnotes (might be handled via a delimited processor)
(md/parse "some text with^[ and not without] a footnote")
(parse "some text with^[ and not without] a footnote")
(parse "some text with $and not
without$ a footnote"))
(parse "some text with^[ and not without] a footnote"))
57 changes: 30 additions & 27 deletions src/nextjournal/markdown/parser2/footnotes.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
(ns nextjournal.markdown.parser2.footnotes
(:require [nextjournal.markdown.parser2.types])
(:import (nextjournal.markdown.parser2.types Footnote)
(org.commonmark.parser Parser$ParserExtension Parser$Builder SourceLine)
(org.commonmark.parser.block AbstractBlockParser BlockContinue BlockParserFactory BlockStart ParserState BlockParser)))
(:require [clojure.string :as str]
[nextjournal.markdown.parser2.types])
(:import (nextjournal.markdown.parser2.types Footnote FootnoteRef)
(org.commonmark.node Text Node Nodes)
(org.commonmark.parser Parser$ParserExtension Parser$Builder PostProcessor SourceLine)
(org.commonmark.parser.block AbstractBlockParser BlockContinue BlockParserFactory BlockStart ParserState BlockParser)
(org.commonmark.parser.delimiter DelimiterProcessor)))

(def footnote-id-regex (re-pattern "\\[\\^\\s*(.*)\\s*\\]"))
(def footnote-def-regex (re-pattern "^\\[\\^\\s*(.*)\\s*\\]:"))
Expand All @@ -23,32 +26,32 @@
(BlockContinue/atIndex non-space)))))))

(comment
(nextjournal.markdown.parser2/parse "what the
(nextjournal.markdown.parser2/parse "init [^label] end
[^what]: * what _the_ heck
* the
[^label]: * this is nice _and_ nice
* and so so
"))

what a what"))
(defn block-parser-factory []
(proxy [Object BlockParserFactory] []
(tryStart [^ParserState state _matchedBlockParser]
(if (<= 4 (.getIndent state))
(BlockStart/none)
(let [^SourceLine line (.getLine state)
line-content (.getContent line)
next-non-space (.getNextNonSpaceIndex state)
candidate-content (subs line-content next-non-space)
m (re-matcher footnote-def-regex candidate-content)]
(if (re-find m)
(let [block-index (+ next-non-space (.end m))
label (subs line-content (+ next-non-space 2)
(+ next-non-space (- (.end m) 2)))
^BlockParser bp (block-parser label)]
(.atIndex (BlockStart/of (into-array [bp]))
block-index))
(BlockStart/none)))))))

(defn extension []
(proxy [Object Parser$ParserExtension] []
(extend [^Parser$Builder pb]
(.customBlockParserFactory
pb (proxy [Object BlockParserFactory] []
(tryStart [^ParserState state _matchedBlockParser]
(if (<= 4 (.getIndent state))
(BlockStart/none)
(let [^SourceLine line (.getLine state)
line-content (.getContent line)
next-non-space (.getNextNonSpaceIndex state)
candidate-content (subs line-content next-non-space)
m (re-matcher footnote-def-regex candidate-content)]
(if (re-find m)
(let [block-index (+ next-non-space (.end m))
label (subs line-content (+ next-non-space 2)
(+ next-non-space (- (.end m) 2)))
^BlockParser bp (block-parser label)]
(println :match label (.start m) (.end m))
(.atIndex (BlockStart/of (into-array [bp]))
block-index))
(BlockStart/none))))))))))
(.customBlockParserFactory pb (block-parser-factory)))))
16 changes: 13 additions & 3 deletions src/nextjournal/markdown/parser2/types.clj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,17 @@
(defn footnote-init [label] [[] (ref {:label label})])
(defn footnote-getLabel [this] (:label @(.state this)))

(comment
(compile 'nextjournal.markdown.parser2.types)
(gen-class
:name nextjournal.markdown.parser2.types.FootnoteRef
:extends org.commonmark.node.CustomNode
:constructors {[String] []}
:init init
:state state
:methods [[getLabel [] String]]
:prefix "footnote-ref-")

)
(defn footnote-ref-init [label] [[] (ref {:label label})])
(defn footnote-ref-getLabel [this] (:label @(.state this)))

(comment
(compile 'nextjournal.markdown.parser2.types))

0 comments on commit 567ca17

Please sign in to comment.