Skip to content

Instantly share code, notes, and snippets.

@augustl augustl/test.clj
Last active Aug 29, 2015

Embed
What would you like to do?
(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
You can’t perform that action at this time.