-
-
Save tonsky/6243291 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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