Last active
December 4, 2017 07:54
-
-
Save rauhs/9bbad2dd5a3a00d54a21f89e86838539 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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