Skip to content

Commit

Permalink
added type hints to avoid reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
yogthos committed Aug 26, 2013
1 parent e16e726 commit e7ff252
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject markdown-clj "0.9.29"
(defproject markdown-clj "0.9.30"
:clojurescript? true
:description "Markdown parser"
:url "https://github.com/yogthos/markdown-clj"
Expand Down
44 changes: 22 additions & 22 deletions src/markdown/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@
(:require [clojure.java.io :as io])
(:import [java.io StringReader StringWriter]))

(defn- write [writer text]
(defn- write [^java.io.Writer writer ^String text]
(doseq [c text] (.write writer (int c))))
(defn- init-transformer [writer {:keys [replacement-transformers custom-transformers]}]
(fn [line next-line state]

(defn- init-transformer [writer {:keys [replacement-transformers custom-transformers]}]
(fn [line next-line state]
(binding [*next-line* next-line]
(let [[text new-state]
(reduce
(fn [[text, state] transformer]
(fn [[text, state] transformer]
(transformer text state))
[line state]
[line state]
(or replacement-transformers
(into transformer-vector custom-transformers)))]
(into transformer-vector custom-transformers)))]
(write writer text)
new-state))))

(defn md-to-html
(defn md-to-html
"reads markdown content from the input stream and writes HTML to the provided output stream"
[in out & params]
(binding [markdown.transformers/*substring* (fn [s n] (.substring s n))]
(with-open [rdr (io/reader in)
wrt (io/writer out)]
(let [transformer (init-transformer wrt params)]
(loop [line (.readLine rdr)
[in out & params]
(binding [markdown.transformers/*substring* (fn [^String s n] (.substring s n))]
(with-open [^java.io.BufferedReader rdr (io/reader in)
^java.io.BufferedWriter wrt (io/writer out)]
(let [transformer (init-transformer wrt params)]
(loop [^String line (.readLine rdr)
next-line (.readLine rdr)
state (apply (partial assoc {} :last-line-empty? true) params)]
(let [state (if (:buf state)
(transformer (:buf state) next-line (-> state (dissoc :buf :lists) (assoc :last-line-empty? true)))
state)]
(if line
state (apply (partial assoc {} :last-line-empty? true) params)]
(let [state (if (:buf state)
(transformer (:buf state) next-line (-> state (dissoc :buf :lists) (assoc :last-line-empty? true)))
state)]
(if line
(recur next-line
(.readLine rdr)
(assoc (transformer line next-line state)
(assoc (transformer line next-line state)
:last-line-empty? (empty? (.trim line))))
(transformer "" nil (assoc state :eof true))))))
(.flush wrt))))

(defn md-to-html-string
"converts a markdown formatted string to an HTML formatted string"
[text & params]
[text & params]
(when text
(let [input (new StringReader text)
output (new StringWriter)]
output (new StringWriter)]
(apply (partial md-to-html input output) params)
(.toString output))))

7 changes: 5 additions & 2 deletions test/benchmark.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@



(deftest ^:benchmark bench []
(deftest ^:benchmark bench-string []
(criterium/bench
(markdown/md-to-html-string
(markdown/md-to-html-string
"\nLorem ipsum **dolor** sit amet, consectetur _adipisicing elit_, sed do eiu^smod tem^por incididunt ut labore")))

(deftest ^:benchmark bench-file []
(criterium/bench
(markdown/md-to-html "test.md" (java.io.StringWriter.))))

0 comments on commit e7ff252

Please sign in to comment.