Skip to content

Instantly share code, notes, and snippets.

@rauhs
Last active December 4, 2017 07:54
Show Gist options
  • Save rauhs/9bbad2dd5a3a00d54a21f89e86838539 to your computer and use it in GitHub Desktop.
Save rauhs/9bbad2dd5a3a00d54a21f89e86838539 to your computer and use it in GitHub Desktop.
(defn simple-md-parser
"Simple markdown parser."
[s special]
(let [it (iter s)
out-state (fn [out text state]
(conj out {:text text
:state (into [] (keep (fn [[k v]]
(when v k))) state)}))]
(loop [out []
state {}
text ""]
(if ^boolean (.hasNext it)
(let [ch (.next it)]
(if (contains? special ch)
(recur (if (not (empty? text)) (out-state out text state) out)
(update state ch not) "")
(recur out state (str text ch))))
(if (not (empty? text))
(out-state out text state)
out)))))
(simple-md-parser "A *bold_bold+under*_ _underline /italics+underline"
#{"*" "_" "/"})
[{:text "A ", :state []}
{:text "bold", :state ["*"]}
{:text "bold+under", :state ["*" "_"]}
{:text " ", :state []}
{:text "underline ", :state ["_"]}
{:text "italics+underline", :state ["_" "/"]}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment