/
utils.clj
49 lines (39 loc) · 1.44 KB
/
utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
(ns tower.utils
{:author "Peter Taoussanis"}
(:require [clojure.string :as str]))
(defn leaf-paths
"Takes a nested map and squashes it into a sequence of paths to leaf nodes.
Based on 'flatten-tree' by James Reaves on Google Groups.
(leaf-map {:a {:b {:c \"c-data\" :d \"d-data\"}}}) =>
((:a :b :c \"c-data\") (:a :b :d \"d-data\"))"
[map]
(if (map? map)
(for [[k v] map
w (leaf-paths v)]
(cons k w))
(list (list map))))
(defn escape-html
"Changes some common special characters into HTML character entities."
[s]
(-> (str s)
(str/replace "&" "&")
(str/replace "<" "<")
(str/replace ">" ">")
(str/replace "\"" """)))
(comment (escape-html "\"Word\" & <tag>"))
(defn inline-markdown->html
"Uses regex to parse given reduced-feature-set inline markdown string into
HTML string. Supports bold, italic, and a context-specific alternative style
tag.
Doesn't do any escaping."
[markdown]
(-> markdown
;; Strong
(str/replace #"\*\*(.+?)\*\*" "<strong>$1</strong>")
(str/replace #"__(.+?)__" "<strong>$1</strong>")
;; Emph
(str/replace #"\*(.+?)\*" "<emph>$1</emph>")
(str/replace #"_(.+?)_" "<emph>$1</emph>")
;; Special context-specific style (define in surrounding CSS scope!)
(str/replace #"~~(.+?)~~" "<span class=\"alt\">$1</span>")))
(comment (inline-markdown->html "Maybe **this** and ~~that~~ <tag> & thing?"))