split util.marker & util.priority lib

This commit is contained in:
rcmerci
2021-05-10 15:49:39 +08:00
committed by Tienson Qin
parent ac7f6e1967
commit 09b2ea368d
7 changed files with 77 additions and 66 deletions

View File

@@ -1019,64 +1019,6 @@
(if-let [c (gobj/get js/window "clipboardData")]
[(.getData c "Text") (.getData c "Text")])))))
(defn marker?
[s]
(contains?
#{"NOW" "LATER" "TODO" "DOING"
"DONE" "WAIT" "WAITING" "CANCELED" "CANCELLED" "STARTED" "IN-PROGRESS"}
(string/upper-case s)))
(def marker-pattern
#"^(NOW|LATER|TODO|DOING|DONE|WAITING|WAIT|CANCELED|CANCELLED|STARTED|IN-PROGRESS)?\s?")
(def bare-marker-pattern
#"^(NOW|LATER|TODO|DOING|DONE|WAITING|WAIT|CANCELED|CANCELLED|STARTED|IN-PROGRESS){1}\s+")
#?(:cljs
(defn add-or-update-marker
[content format marker]
(let [[re-pattern new-line-re-pattern]
(if (= :org format)
[#"\*+\s" #"\n\*+\s"]
[#"#+\s" #"\n#+\s"])
pos
(if-let [matches (seq (re-pos new-line-re-pattern content))]
(let [[start-pos content] (last matches)]
(+ start-pos (count content)))
(count (re-find re-pattern content)))
new-content
(str (subs content 0 pos)
(string/replace-first (subs content pos)
marker-pattern
(str marker " ")))]
new-content)))
#?(:cljs
(defn add-or-update-priority
[content format priority]
(let [priority-pattern #"(\[#[ABC]\])?\s?"
[re-pattern new-line-re-pattern]
(if (= :org format)
[#"\*+\s" #"\n\*+\s"]
[#"#+\s" #"\n#+\s"])
skip-hash-pos
(if-let [matches (seq (re-pos new-line-re-pattern content))]
(let [[start-pos content] (last matches)]
(+ start-pos (count content)))
(count (re-find re-pattern content)))
skip-marker-pos
(if-let [matches (seq (re-pos bare-marker-pattern (subs content skip-hash-pos)))]
(let [[start-pos content] (last matches)]
(+ start-pos (count content)))
0)
pos (+ skip-hash-pos skip-marker-pos)
new-content
(str (subs content 0 pos)
(string/replace-first (subs content pos)
priority-pattern
(str priority " ")))]
new-content)))
(defn pp-str [x]
(with-out-str (clojure.pprint/pprint x)))