Skip to content

Commit

Permalink
Cleanup classes
Browse files Browse the repository at this point in the history
  • Loading branch information
zampino committed Aug 13, 2024
1 parent 020a93e commit b73dd6f
Show file tree
Hide file tree
Showing 53 changed files with 47 additions and 16 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

:test
{:extra-paths ["test"]
:jvm-opts ["-Dclojure.main.report=stderr"]
:extra-deps {nubank/matcher-combinators {:mvn/version "3.8.3"}}
:exec-fn test-runner/run}

Expand Down
19 changes: 8 additions & 11 deletions src/nextjournal/markdown/parser/impl.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
(ns nextjournal.markdown.parser.impl
(:require [clojure.zip :as z]
[nextjournal.markdown.parser :as parser]
[nextjournal.markdown.parser.impl.types]
[nextjournal.markdown.parser.impl.formulas :as formulas])
[nextjournal.markdown.parser.impl.formulas :as formulas]
[nextjournal.markdown.parser.impl.utils :as u])
(:import (org.commonmark.parser Parser)
(org.commonmark.ext.task.list.items TaskListItemsExtension TaskListItemMarker)
(org.commonmark.ext.footnotes FootnotesExtension FootnoteReference FootnoteDefinition InlineFootnote)
(org.commonmark.node Node AbstractVisitor
;;;;;;;;;; node types ;;;;;;;;;;;;;;;;;;
Document
BlockQuote
BulletList
Expand All @@ -29,8 +28,8 @@
SoftLineBreak
HardLineBreak
Image)
;; custom types
(nextjournal.markdown.parser2.types InlineFormula BlockFormula)))
(nextjournal.markdown.parser.impl.types InlineFormula
BlockFormula)))

(set! *warn-on-reflection* true)
;; TODO:
Expand Down Expand Up @@ -131,7 +130,7 @@
(-> loc (z/append-child (merge {:type :code
:info (.getInfo node)
:content [{:text (.getLiteral node)}]}
(parser/parse-fence-info (.getInfo node)))) z/down z/rightmost))))
(u/parse-fence-info (.getInfo node)))) z/down z/rightmost))))

(defmethod open-node Image [ctx ^Image node]
(update-current ctx (fn [loc] (-> loc (z/append-child {:type :image
Expand Down Expand Up @@ -177,8 +176,8 @@
z/down z/rightmost))

(defn node->data [{:as _ctx :keys [content footnotes label->footnote-ref]} ^Node node]
(let [!ctx (atom {:doc (parser/->zip {:type :doc :content (or content [])})
:footnotes (parser/->zip {:type :footnotes :content (or footnotes [])})
(let [!ctx (atom {:doc (u/->zip {:type :doc :content (or content [])})
:footnotes (u/->zip {:type :footnotes :content (or footnotes [])})
:root :doc
:label->footnote-ref (or label->footnote-ref {})})]
(.accept node
Expand Down Expand Up @@ -268,10 +267,8 @@ And what.
[^note3]: this should just be ignored
")

(require '[nextjournal.markdown :as md])
(md/parse text-with-footnotes)

(parse (slurp "../clerk-px23/README.md"))
;; => :ref 27

(parse "Knuth's _Literate Programming_[^literateprogramming][^knuth84] emphasized the importance of focusing on human beings as consumers of computer programs. His original implementation involved authoring files that combine source code and documentation, which were then divided into two derived artifacts: source code for the computer and a typeset document in natural language to explain the program.
Expand Down
2 changes: 1 addition & 1 deletion src/nextjournal/markdown/parser/impl/formulas.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
(:require [clojure.string :as str])
(:import (java.util Set)
(java.util.regex Matcher)
(nextjournal.markdown.parser2.types InlineFormula BlockFormula)
(nextjournal.markdown.parser.impl.types InlineFormula BlockFormula)
(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
6 changes: 3 additions & 3 deletions src/nextjournal/markdown/parser/impl/types.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
;; 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.parser2.types.InlineFormula
:name nextjournal.markdown.parser.impl.types.InlineFormula
:extends org.commonmark.node.CustomNode
:constructors {[String] []}
:init init
Expand All @@ -16,7 +16,7 @@
(defn inline-formula-getLiteral [this] (:literal @(.state this)))

(gen-class
:name nextjournal.markdown.parser2.types.BlockFormula
:name nextjournal.markdown.parser.impl.types.BlockFormula
:extends org.commonmark.node.CustomBlock
:constructors {[] []}
:init init
Expand All @@ -30,4 +30,4 @@
(defn block-formula-setLiteral [this val] (reset! (.state this) val))

(comment
(compile 'nextjournal.markdown.parser2.types))
(compile 'nextjournal.markdown.parser.impl.types))
35 changes: 34 additions & 1 deletion src/nextjournal/markdown/parser/impl/utils.cljc
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(ns nextjournal.markdown.parser.impl.utils)
(ns nextjournal.markdown.parser.impl.utils
(:require [clojure.string :as str]
[clojure.zip :as z]))

(def empty-doc {:type :doc
:content []
Expand All @@ -19,9 +21,17 @@
:cljs (let [rex (js/RegExp. (.-source re) "g")]
(take-while some? (repeatedly #(when-some [m (.exec rex text)] [(vec m) (.-index m) (.-lastIndex rex)]))))))

;; zipper utils

(defn ->zip [doc]
(z/zipper (every-pred map? :type) :content
(fn [node cs] (assoc node :content (vec cs)))
doc))

;; TODO: rewrite in terms of zippers
(def ppop (comp pop pop))

;; TODO: rewrite in terms of zippers
(defn current-ancestor-nodes
"Given an open parsing context `doc`, returns the list of ancestors of the node last parsed into the document, up to but
not including the top document."
Expand All @@ -42,7 +52,30 @@
:pred #(every? (complement #{:link}) (map :type (current-ancestor-nodes %)))
:handler (fn [match] {:type :internal-link :text (match 1)})})

(defn parse-fence-info [info-str]
(try
(when (string? info-str)
(let [tokens (-> info-str
str/trim
(str/replace #"[\{\}\,]" "") ;; remove Pandoc/Rmarkdown brackets and commas
(str/replace "." "") ;; remove dots
(str/split #" "))] ;; split by spaces
(reduce
(fn [{:as info-map :keys [language]} token]
(let [[_ k v] (re-matches #"^([^=]+)=([^=]+)$" token)]
(cond
(str/starts-with? token "#") (assoc info-map :id (str/replace token #"^#" "")) ;; pandoc #id
(and k v) (assoc info-map (keyword k) v)
(not language) (assoc info-map :language token) ;; language is the first simple token which is not a pandoc's id
:else (assoc info-map (keyword token) true))))
{}
tokens)))
(catch #?(:clj Throwable :cljs :default) _ {})))


(comment
(->zip {:type :doc :content []})


(re-idx-seq #"\{\{([^{]+)\}\}" "foo {{hello}} bar")
(re-idx-seq #"\{\{[^{]+\}\}" "foo {{hello}} bar {{what}} the"))

0 comments on commit b73dd6f

Please sign in to comment.