Last active
August 29, 2015 13:57
-
-
Save augustl/9512228 to your computer and use it in GitHub Desktop.
This file contains 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 re-seq-with-pos | |
[re str] | |
(let [*matcher* (re-matcher re str)] | |
(loop [match (re-find *matcher*) | |
matches []] | |
(if match | |
(let [start (.start *matcher*)] | |
(recur (re-find *matcher*) | |
(conj matches {:start start :end (+ start (count (first match))) :match match}))) | |
matches)))) | |
(re-seq-with-pos #"(?ms)\{\{(.*?)\}\}" "Test {{wat}} hello {{wut}}") | |
;; [{:start 5, :end 12, :match ["{{wat}}" "wat"]} {:start 19, :end 26, :match ["{{wut}}" "wut"]}] | |
(defn highlight-code | |
[html] | |
(loop [curr 0 | |
matches (->> (re-seq-with-pos #"(?ms)\<code(.*?)\>(.*?)\<\/code\>" html) | |
(pmap #(assoc % :highlighted | |
(perform-highlight | |
(get-language-from-code-tag-attrs (nth (:match %) 1)) | |
(nth (:match %) 2))))) | |
res []] | |
(if (empty? matches) | |
(clojure.string/join (conj res (subs html curr))) | |
(let [match (first matches)] | |
(recur (:end match) (rest matches) (-> res (conj (subs html curr (:start match))) (conj (:highlighted match))))))) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment