summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/clarktown/parsers/link_and_image.clj21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/clarktown/parsers/link_and_image.clj b/src/clarktown/parsers/link_and_image.clj
new file mode 100644
index 0000000..5ef8857
--- /dev/null
+++ b/src/clarktown/parsers/link_and_image.clj
@@ -0,0 +1,21 @@
+(ns clarktown.parsers.link-and-image
+ (:require
+ [clojure.string :as string]))
+
+
+(defn render
+ "Renders all occuring links and images."
+ [block]
+ (loop [block block
+ matches (-> (re-seq #"\!?\[(.*?)\]\((.*?)\)" block)
+ distinct)]
+ (if (empty? matches)
+ block
+ (let [[whole-match label href] (first matches)
+ image? (string/starts-with? whole-match "!")
+ image (str "<img src=\"" href "\" alt=\"" label "\">")
+ link (str "<a href=\"" href "\">" label "</a>")]
+ (recur (if image?
+ (string/replace block whole-match image)
+ (string/replace block whole-match link))
+ (drop 1 matches))))))