Commits

Dominic Kexel committed 3418e19

parse blocks recursive

Comments (0)

Files changed (1)

src/mdclj/blocks.clj

 
 (declare parse-blocks)
 
-(defn- concat-blocks [type content remaining & extra]
-  (concat [(into {:type type :content content} extra)] (parse-blocks remaining)))
+(defn- create-block-map [type content remaining & extra]
+  (list (into {:type type :content content} extra) remaining))
 
 (defn- clean-heading-string [line]
   (-> line (to-string)
                               (when (seq rest) 
                                 [index rest]))) headings)]
     (when (not (nil? rest))
-      (concat-blocks ::heading (parse-spans (clean-heading-string rest)) remaining {:size size}))))
+      (create-block-map ::heading (parse-spans (clean-heading-string rest)) remaining {:size size}))))
 
 (defn- match-underline-heading [[caption underline & remaining :as text]]
   (let [current (set underline)
     (when (and (some #(= % current) markers)
                (some #(startswith underline [%]) marker)
                (< (count (partition-by identity underline)) 3))
-      (concat-blocks ::heading (parse-spans caption) remaining {:size 1}))))
+      (create-block-map ::heading (parse-spans caption) remaining {:size 1}))))
 
 (defn- match-horizontal-rule [[rule & remaining :as text]]
   (let [s (set rule)
 
 (defn- match-codeblock [text]
   (when-let [[code remaining] (collect-prefixed-lines text "    ")]
-    (concat-blocks ::codeblock code remaining)))
+    (create-block-map ::codeblock code remaining)))
 
 (defn- match-blockquote [text]
   (when-let [[quote remaining] (collect-prefixed-lines text "> ")]
-    (concat-blocks ::blockquote (parse-blocks quote) remaining)))
+    (create-block-map ::blockquote (parse-blocks quote) remaining)))
 
 (defn- match-paragraph [text]
   (when-let [[lines remaining] (line-seperated text)]
-    (concat-blocks ::paragraph (parse-spans (clojure.string/join "\n" lines)) remaining)))
+    (create-block-map ::paragraph (parse-spans (clojure.string/join "\n" lines)) remaining)))
 
 (defn- match-empty [[head & remaining :as text]]
   (when (and (blank? head) (seq remaining))
    match-empty])
 
 (defn- parse-blocks [lines]
-  (some #(% lines) block-matcher))
-
+  (lazy-seq
+	  (when-let [[result remaining] (some #(% lines) block-matcher)]
+	    (cons result (parse-blocks remaining)))))
+  
 (defn parse-text [text]
   (parse-blocks (seq (clojure.string/split-lines text))))