Skip to content

Instantly share code, notes, and snippets.

@tonsky
Forked from glorphindale/config-parse.clj
Last active December 21, 2015 03:38
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 tonsky/6243291 to your computer and use it in GitHub Desktop.
Save tonsky/6243291 to your computer and use it in GitHub Desktop.
(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