Skip to content

Instantly share code, notes, and snippets.

@augustl
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save augustl/9512228 to your computer and use it in GitHub Desktop.
Save augustl/9512228 to your computer and use it in GitHub Desktop.
(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