Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(require '[clojure.string :as str])
(defn parse-line [line]
(let [line (str/replace line #"[\\]\s*(\n\s*|$)" "")]
(when-not (str/blank? line)
(let [[k v] (str/split line #"=" 2)]
[(str/trim k) (when-not (str/blank? v) (str/trim v))]))))
(defn parse [text]
(let [lines (str/split text #"(?<![\s\\])\s*\n")]
(into {} (map parse-line lines))))
(def conf "key1=val1
key2 = val2
key3=\\
val3
key4=\\
\\
val \\
4
emp1
emp2=
e\\
mp3")
(clojure.test/are [k v] (= (get (parse conf) k) v)
"key1" "val1"
"key2" "val2"
"key3" "val3"
"key4" "val 4"
"emp1" nil
"emp2" nil
"emp3" nil)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment