Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Парсим простой человеколюбивый файл конфигурации.
(require '[clojure.string :as cs])
(def conf
"key1=val1
key2 = val2
key3 = \\
val3
key\\
4=val4
key5=\\
val5
key6=\\
val\\
6
keyemp=
ke\\
y\\
7=\\
va\\
l7
")
(defn parse-conf [text]
; [^\\]\n is a separator between two key-value pairs
(let [p (cs/replace text #"([^\\])\n" "$1\n\n")
sp (cs/split p #"\n\n")]
(->> sp
(map #(cs/replace % #"[\\|\n|\ ]" ""))
(map cs/trim)
(filter #(not (cs/blank? %)))
(map #(cs/split % #"="))
(map #(if (= (count %) 1) [(first %) nil] %)) ; Handle empty value
(flatten)
(apply hash-map))))
(println (parse-conf conf))
> {keyemp nil, key1 val1, key2 val2, key3 val3, key4 val4, key5 val5, key6 val6, key7 val7}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.